go-disruptor 高性能的消息框架开源项目

我要开发同款
匿名用户2016年09月19日
98阅读

技术信息

开源地址
https://github.com/smartystreets/go-disruptor
授权协议
Apache

作品详情

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

简述:

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

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

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

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

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

示例代码:

Wireup

rutime.GOMAXPROCS(2) // make sure we have eough cores available to executecost RigBufferCapacity = 1024 // must be a power of 2cost RigBufferMask = RigBufferCapacity - 1// this istace will be shared amog producers ad cosumers of this applicatiovar rigBuffer = [RigBufferCapacity]MyStruct{}myDisruptor := disruptor.    Cofigure(RigBufferCapacity).    WithCosumerGroup(MyCosumer{}). // we ca have a set of cocurret cosumers ru first    // WithCosumerGroup(MyCosumer{}). // ad the ru this/these cosumers after the first set of cosumers    BuildShared() // Build() = sigle producer vs BuildShared() = multiple producersmyDisruptor.Start()defer myDisruptor.Stop() // clea shutdow which stops all idlig cosumers after all published items have bee cosumed// applicatio code here, e.g. liste to HTTP, read from a etwork socket, etc.

生产者

Producerwriter := myDisruptor.Writer()// for each item received from a etwork socket, e.g. UDP packets, HTTP request, etc. etc.sequece := writer.Reserve(1) // reserve 1 slot o the rig buffer ad give me the upper-most sequece of the reservatio// this could be writte like this: rigBuffer[sequece%RigBufferCapacity] but the Mask ad & operator is faster.rigBuffer[sequece&RigBufferMask].MyImportStructData = ... // data from etwork streamwriter.Commit(sequece, sequece) // the item is ready to be cosumed

消费者

type MyCosumer struct{}fuc (m MyCosumer) Cosume(lowerSequece, upperSequece it64) {    for sequece := lowerSequece; sequece <= upperSequece; sequece++ {        message := rigBuffer[sequece&RigBufferMask] // see performace ote o producer sample above        // hadle the icomig message with your applicatio code    }}

功能介绍

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

声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论