Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java] Add tryClaim API to the RingBuffer #199

Merged
merged 1 commit into from
Jan 20, 2020

Conversation

vyazelenko
Copy link
Contributor

@vyazelenko vyazelenko commented Jan 20, 2020

With the new tryClaim API it is possible to implement zero-copy publication by writing directly to the underlying ring-buffer.

Implementation details:

  • tryClaim - reserves space in the underlying buffer and writes a header with the given msgTypeId and a negative record length
  • commit - flips the sign on the record length field
  • abort - writes PADDING_MSG_TYPE_ID into the type field and flips the sign on the record length field

Example of API usage:

final RingBuffer ringBuffer = ...;

final int index = ringBuffer.tryClaim(msgTypeId, messageLength);
if (index >= 0)
{
    try
    {
         final AtomicBuffer buffer = ringBuffer.buffer();
         // Work with the buffer directly using the index
         ...
         ringBuffer.commit(index); // commit message
    }
    catch (final Throwable t)
    {
         ringBuffer.abort(index); // allow consumer to proceed
         ...
    }
}

Closes #197

With the new `tryClaim` API it is possible to implement zero-copy publication by writing directly to the underlying ring-buffer.

Implementation details:
- `tryClaim` - reserves space in the underlying buffer and writes a header with the given `msgTypeId` and a negative record length
- `commit` - flips the sign on the record length field
- `abort` - writes `PADDING_MSG_TYPE_ID` into the type field and flips the sign on the record length field

Example of API usage:
```
final RingBuffer ringBuffer = ...;

final int index = ringBuffer.tryClaim(msgTypeId, messageLength);
if (index >= 0)
{
    try
    {
         final AtomicBuffer buffer = ringBuffer.buffer();
         // Work with the buffer directly using the index
         ...
         ringBuffer.commit(index); // commit message
    }
    catch (final Throwable t)
    {
         ringBuffer.abort(index); // allow consumer to proceed
         ...
    }
}
```
@mjpt777 mjpt777 merged commit 8944ed3 into real-logic:master Jan 20, 2020
mjpt777 added a commit that referenced this pull request Jan 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add "try claim" method to RingBuffer
2 participants