高性能的消息框架 go-disruptor

码农软件 · 软件分类 · 并发/并行处理框架 · 2019-09-02 16:11:53

软件介绍

这是Go编程语言里 LMAX Disruptor的接口。 它保留了Disruptor的本质和原理,并利用了很多相同的抽象概念和理论,但不会保持同样的API。

简述:

在我的 MacBook Pro (Intel Core i7-4960HQ CPU @ 2.60GHz) 中,我使用了 Go 1.4.2, 此版本使我能在一秒内发送9亿多份邮件(是的,你没有听错), 从一个goroutine到另一个goroutine. 讯息在两台CPU间的传递很简单。 请注意,您的里程可能会有所不同,通过控制CPU并清除其缓存,不同的操作系统可以添加特定的“jitter”到App中。Linux和Windows系统有给定的进程分配给特定的CPU内核它通过将所有的CPU缓存热显著降低“jitter”的能力。 顺便,当Disruptor代码被编译并在Nexus 5上运行,它可以每秒可推送约15-20万条信息。

一旦被初始化,在运行时,Disruptor杰出设计的考虑因素之一,就是以一个恒定的速率来处理消息。为此,它使用两个主要技术:

1. 它避免了在所有costs上使用锁,costs通常会引起CPU内核间的排斥,影响可测量性。

2. 它允许应用程序预先在一个环形缓冲区分配连续的空间,不产生垃圾。 

通过避免垃圾,垃圾清理站和应用程序暂停的功能可以免去。

示例代码:

Wireup

runtime.GOMAXPROCS(2) // make sure we have enough cores available to execute
const RingBufferCapacity = 1024 // must be a power of 2
const RingBufferMask = RingBufferCapacity - 1
// this instance will be shared among producers and consumers of this application
var ringBuffer = [RingBufferCapacity]MyStruct{}
myDisruptor := disruptor.
    Configure(RingBufferCapacity).
    WithConsumerGroup(MyConsumer{}). // we can have a set of concurrent consumers run first
    // WithConsumerGroup(MyConsumer{}). // and then run this/these consumers after the first set of consumers
    BuildShared() // Build() = single producer vs BuildShared() = multiple producers
myDisruptor.Start()
defer myDisruptor.Stop() // clean shutdown which stops all idling consumers after all published items have been consumed
// application code here, e.g. listen to HTTP, read from a network socket, etc.

生产者

Producer
writer := myDisruptor.Writer()
// for each item received from a network socket, e.g. UDP packets, HTTP request, etc. etc.
sequence := writer.Reserve(1) // reserve 1 slot on the ring buffer and give me the upper-most sequence of the reservation
// this could be written like this: ringBuffer[sequence%RingBufferCapacity] but the Mask and & operator is faster.
ringBuffer[sequence&RingBufferMask].MyImportStructData = ... // data from network stream
writer.Commit(sequence, sequence) // the item is ready to be consumed

消费者

type MyConsumer struct{}
func (m MyConsumer) Consume(lowerSequence, upperSequence int64) {
    for sequence := lowerSequence; sequence <= upperSequence; sequence++ {
        message := ringBuffer[sequence&RingBufferMask] // see performance note on producer sample above
        // handle the incoming message with your application code
    }
}

本文地址:https://www.codercto.com/soft/d/13725.html

因计算机而强大

因计算机而强大

[美]西摩 佩珀特 Seymour Papert / 梁栋 / 新星出版社 / 2019-1 / 38

本书有两个中心主题—— 孩子可以轻松自如地学习使用计算机; 学习使用计算机能够改变他们学习其他知识的方式。 (前苹果公司总裁 约翰·斯卡利) 最有可能带来文化变革的就是计算机的不断普及。 计算机不仅是一个工具,它对我们的心智有着根本和深远的影响。 计算机不仅帮助我们学习 ,还帮助我们学习怎样学习。 计算机是一种调解人与人之间关系的移情对象。 一个数学的头脑......一起来看看 《因计算机而强大》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

在线进制转换器
在线进制转换器

各进制数互转换器

html转js在线工具
html转js在线工具

html转js在线工具