Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OneToManyRingBuffer implementation in agrona

Tags:

aeron

This may be a stupid question but there's a OneToOneRingBuffer and a ManyToOneRingBuffer available in aeron (agrona). I have a single producer and many consumers I'm wondering how to achieve the equivalent effect of OneToManyRingBuffer?

like image 518
hawk Avatar asked Dec 06 '25 05:12

hawk


1 Answers

BroadcastTransmitter and BroadcastReceiver in Agrona is one approach that would give you one-to-many semantics.

Worth noting that slow consumers may see messages dropped. If this is not desired then you could also look at Aeron IPC which would exert back-pressure on the publisher in this scenario.

Example

Top-level, here's how to use it:

// Create broadcast buffer
int capacity = 1 << 10; // Must be power of two
int bufferSize = capacity + BroadcastBufferDescriptor.TRAILER_LENGTH;
UnsafeBuffer broadcastBuffer = new UnsafeBuffer(new byte[bufferSize]);

// Create transmitter
BroadcastTransmitter transmitter = new BroadcastTransmitter(broadcastBuffer);

// Create receiver (can create many of these)
BroadcastReceiver broadcastReceiver = new BroadcastReceiver(broadcastBuffer);
CopyBroadcastReceiver copyBroadcastReceiver = new CopyBroadcastReceiver(broadcastReceiver);

// Send message
int msgTypeId = 1;
MutableDirectBuffer msgBuffer = new ExpandableArrayBuffer();
int msgLength = msgBuffer.putStringWithoutLengthAscii(0, "Hello World!");
transmitter.transmit(msgTypeId, msgBuffer, 0, msgLength);

// Receive message
copyBroadcastReceiver.receive(
  (msgType, buffer, offset, length) -> System.out.println(buffer.getStringWithoutLengthAscii(offset, length)));
like image 156
James Avatar answered Dec 09 '25 01:12

James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!