A simple and practical. Net extension written for Disruptor, disruptor.net

Source: Internet
Author: User

A simple and practical. Net extension written for Disruptor, disruptor.net

 

The disruptor user encapsulates its own consumers and injects the consumers into the consumer container. The consumer container automatically creates cache queues and producers;

Disruptor C # source code porting

Https://github.com/bingyang001/disruptor-net-3.3.0-alpha

Author blog http://www.cnblogs.com/liguo/p/3296166.html

 

Consumer container:

/// <Summary> /// consumer manager /// </summary> /// <typeparam name = "TProduct"> product </typeparam> public class Workers <TProduct> where TProduct: producer <TProduct>, new () {private readonly WorkerPool <TProduct> _ workerPool; public Workers (List <IWorkHandler <TProduct> handers, IWaitStrategy waitStrategy = null, int bufferSize = 1024*64) {if (handers = null | handers. count = 0) throw new ArgumentNullExcepti On ("the consumption event processing array is empty! "); If (handers. Count = 1) _ ringBuffer = RingBuffer <TProduct>. CreateSingleProducer () => new TProduct (), bufferSize, waitStrategy ?? New YieldingWaitStrategy (); else {_ ringBuffer = RingBuffer <TProduct>. CreateMultiProducer () => new TProduct (), bufferSize, waitStrategy ?? New YieldingWaitStrategy ();} _ workerPool = new WorkerPool <TProduct> (_ ringBuffer, _ ringBuffer. newBarrier (), new FatalExceptionHandler (), handers. toArray (); _ ringBuffer. addGatingSequences (_ workerPool. getWorkerSequences ();} public void Start () {_ workerPool. start (TaskScheduler. default);} public Producer <TProduct> CreateOneProducer () {return new Producer <TProduct> (this. _ ringBuffer);} public void DrainAndHalt () {_ workerPool. drainAndHalt ();} private readonly RingBuffer <TProduct> _ ringBuffer ;}

 

Producer (product): All products should inherit from the producer

/// <Summary> /// Producer object /// </summary> /// <typeparam name = "TProduct"> product type </typeparam> public class Producer <TProduct> where TProduct: producer <TProduct> {long _ sequence; private RingBuffer <TProduct> _ ringBuffer; public Producer () {} public Producer (RingBuffer <TProduct> ringBuffer) {_ ringBuffer = ringBuffer ;} /// <summary> /// obtain the modifiable product /// </summary> /// <returns> </returns> public Producer <TProduct> Enqueue () {long sequence = _ ringBuffer. next (); Producer <TProduct> producer = _ ringBuffer [sequence]; producer. _ sequence = sequence; if (producer. _ ringBuffer = null) producer. _ ringBuffer = _ ringBuffer; return producer;} // <summary> // submit the product modification /// </summary> public void Commit () {_ ringBuffer. publish (_ sequence );}}

 

--------------------------------------------------------

The above is implemented, and the test code

Create a product object first:

/// <Summary> /// Product/inherited Producer /// </summary> public class Product: Producer <Product >{// the Product can be defined casually, no requirement. You only need to inherit the public long Value {get; set;} public string Guid {get; set ;}} from the producer ;}}

Create a consumer object

/// <Summary> /// consumption processing object /// </summary> public class WorkHandler: IWorkHandler <Product> {public void OnEvent (Product @ event) {// Test is the Test object's data accuracy (data duplication or data loss. updateCacheByOut (@ event. guid); // receives the product and writes the processing code here }}

Test code:

One or more producer objects can be created, and the consumer processes the objects. There may not be too many objects, but they may not be fast. It is recommended that the producer create one and operate one producer object in multiple threads; number of consumer objects that can be created based on actual conditions;

// Create two consumers, two producers, and two consumers. The framework has two threads to process the consumption product.
Workers <Product> workers = new Workers <Product> (new List <IWorkHandler <Product> () {new WorkHandler (), new WorkHandler ()}); producer <Product> producerWorkers = workers. createOneProducer (); Producer <Product> producerWorkers1 = workers. createOneProducer ();
// Start consumption
Workers. Start ();

Product Production:

The product can be put into the queue wherever the producer is referenced. the method for putting data into the queue here is not the same as that in normal time. here, we use a place from the queue and put the product in it. Specifically, we can find the producer, get a product object, modify the product attributes, and finally submit the modifications.

Var obj = producer. Enqueue (); // modify product attributes obj. Commit ();

 

The above is the key code:

Complete Test class: contains test data correctness and performance. When the test data is not verified, the ops is about 10 million per second.

Class Test {public static long PrePkgInCount = 0; public static long PrePkgOutCount = 0; public static long PkgInCount = 0; public static long PkgOutCount = 0; static ConcurrentDictionary <string, string> InCache = new ConcurrentDictionary <string, string> (); static ConcurrentDictionary <string, string> OutCache = new ConcurrentDictionary <string, string> (); private static long Seconds; static void Ma In (string [] args) {Workers <Product> workers = new Workers <Product> (new List <IWorkHandler <Product> () {new WorkHandler (), new WorkHandler ()}); Producer <Product> producerWorkers = workers. createOneProducer (); Producer <Product> producerWorkers1 = workers. createOneProducer (); workers. start (); Task. run (delegate {while (true) {Thread. sleep (1000); Seconds ++; long intemp = PkgInCount; long outemp = Pk GOutCount; Console. writeLine ($ "In ops = {intemp-PrePkgInCount}, out ops = {outemp-PrePkgOutCount}, inCacheCount = {InCache. count}, OutCacheCount = {OutCache. count}, RunningTime = {Seconds} "); PrePkgInCount = intemp; PrePkgOutCount = outemp ;}}); Task. run (delegate {Run (producerWorkers) ;}); Task. run (delegate {Run (producerWorkers) ;}); Task. run (delegate {Run (producerWorkers1) ;}); Console. read ();} publi C static void Run (Producer <Product> producer) {for (int I = 0; I <int. maxValue; I ++) {var obj = producer. enqueue (); CheckRelease (obj as Product); obj. commit () ;}} public static void CheckRelease (Product publisher) {Interlocked. increment (ref PkgInCount); return; // publisher is not checked for correctness. guid = Guid. newGuid (). toString (); InCache. tryAdd (publisher. guid, string. empty);} public static void UpdateCacheBy Out (string guid) {Interlocked. Increment (ref Test. PkgOutCount); if (guid! = Null) if (InCache. containsKey (guid) {string str; InCache. tryRemove (guid, out str);} else {OutCache. tryAdd (guid, string. empty) ;}//< summary> /// Product/inherited producer /// </summary> public class Product: producer <Product >{// the Product can be defined as subordinate. No requirement is required. You only need to inherit from the Producer to implement public long Value {get; set;} public string Guid {get; set ;}//< summary> /// consumption processing object /// </summary> public class WorkHandler: IWorkHandler <Product> {public void OnEvent (Product @ event) {Test. updateCacheByOut (@ event. guid); // receives the product and writes the processing code here }}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.