Skip to the content.

Welcome to RingBufferPlus

Build License NuGet Downloads

RingBufferPlus a generic circular buffer (ring buffer) in C# with auto-scaler.

RingBufferPlus was developed in C# with the netstandard2.1, .NET 6 , .NET 7 and .NET 8 target frameworks.

Table of Contents

What’s new in the latest version

V3.2.0

Top

Features

Basic concept

Top

A ring buffer is a memory allocation scheme where memory is reused (reclaimed) when an index, incremented modulo the buffer size, writes over a previously used location.

A ring buffer makes a bounded queue when separate indices are used for inserting and removing data. The queue can be safely shared between threads (or processors) without further synchronization so long as one processor enqueues data and the other dequeues it. (Also, modifications to the read/write pointers must be atomic, and this is a non-blocking queue–an error is returned when trying to write to a full queue or read from an empty queue).

Implemented concept

Top

The implementation follows the basic principle. The principle was expanded to have a scale capacity that may or may not be modified to optimize the consumption of the resources used.

Key Features

Top

Installing

Top

Install-Package RingBufferPlus [-pre]
dotnet add package RingBufferPlus [--prerelease]

Note: [-pre]/[–prerelease] usage for pre-release versions

Examples

Top

See folder Samples.

dotnet run --project [name of sample]

Generic Usage

Top

The RingBufferPlus use fluent interface; an object-oriented API whose design relies extensively on method chaining. Its goal is to increase code legibility. The term was coined in 2005 by Eric Evans and Martin Fowler.

Sample-Console Usage (Minimal features with auto-scale)

Random rnd = new();
var rb = RingBuffer<int>.New("MyBuffer", cts.Token)
    .Capacity(8)
    .Factory((cts) => { return rnd.Next(1, 10); })
    .ScaleUnit(ScaleMode.Automatic)
        .MinCapacity(4)
        .MaxCapacity(20)
    .BuildWarmup(out var completed);
using (var buffer = rb.Accquire(cts.Token))
{
    if (bufferedItem.Successful)
    {
        try 
        {
            //do something    
        }
        catch
        {
            buffer.Invalidate();
        }
    }
}

Sample-api/web Usage (Minimal features without auto-scale)

Top

builder.Services.AddRingBuffer<int>("Mybuffer",(ringbuf, _) =>
{
    return ringbuf
        .Capacity(8)
        .Factory((cts) => { return 10; })
        .Build();
});

...
//If you do not use the 'Warmup Ring Buffer' command, the first access to acquire the buffer will be Warmup (not recommended)
app.WarmupRingBuffer<int>("Mybuffer");
[ApiController]
[Route("[controller]")]
public class MyController(IRingBufferService<int> ringBufferService) : ControllerBase
{
    private readonly IRingBufferService<int> _ringBufferService = ringBufferService;

    [HttpGet]
    public ActionResult Get(CancellationToken token)
    {
        using (var buffer = _ringBufferService.Accquire(token))
        {
            if (bufferedItem.Successful)
            {
                try 
                {
                    //do something    
                }
                catch
                {
                    buffer.Invalidate();
                }
            }
        }
    }
}

Sample-api/web Usage (Minimal features with manual scale)

Top

builder.Services.AddRingBuffer<int>("Mybuffer",(ringbuf, _) =>
{
    return ringbuf
        .Capacity(8)
        .Factory((cts) => { return 10; })
        .ScaleUnit(ScaleMode.Manual)
            .MinCapacity(2)
            .MaxCapacity(12)
        .Build();
});

...
//If you do not use the 'Warmup Ring Buffer' command, the first access to acquire the buffer will be Warmup (not recommended)
app.WarmupRingBuffer<int>("Mybuffer");
[ApiController]
[Route("[controller]")]
public class MyController(IRingBufferService<int> ringBufferService) : ControllerBase
{
    private readonly IRingBufferService<int> _ringBufferService = ringBufferService;

    [HttpGet]
    public ActionResult Get(CancellationToken token)
    {
        using (var buffer = _ringBufferService.Accquire(token))
        {
            if (bufferedItem.Successful)
            {
                try 
                {
                    //do something    
                }
                catch
                {
                    buffer.Invalidate();
                }
            }
        }
    }

    [HttpPatch]
    [Route("/ChangeCapacity")]
    public ActionResult ChangeCapacity(ScaleSwith scaleUnit)
    {
        _ringBufferService.SwithTo(scaleUnit);
        return Ok();
    }
}

RabbitMQ Usage

Top

The RingBufferPlus use fluent interface; an object-oriented API whose design relies extensively on method chaining. Its goal is to increase code legibility. The term was coined in 2005 by Eric Evans and Martin Fowler.

RabbitMQ has **AutomaticRecovery** functionality. This feature must be **DISABLED** with RingBufferPlus.

If the AutomaticRecovery functionality is activated, “ghost” buffers may occur (without RingBufferPlus control)

Sample-Console Master-Slave feature using RabbitMq (basic usage)

For more details see Complete-Samples.

ConnectionFactory = new ConnectionFactory()
{
    ...
    AutomaticRecoveryEnabled = false
};
connectionRingBuffer = RingBuffer<IConnection>.New("RabbitCnn")
    .Capacity(2)
    .Logger(applogger!)
    .AccquireTimeout(TimeSpan.FromMilliseconds(500))
    .OnError((log, error) =>
        {
            log?.LogError("{error}", error);
        })
    .Factory((cts) => ConnectionFactory.CreateConnection())
    .ScaleUnit(ScaleMode.Slave)
        .ReportScale((metric, log, cts) =>
            {
                log?.LogInformation($"RabbitCnn Report: [{metric.MetricDate}]  Trigger {metric.Trigger} from {metric.FromCapacity} to {metric.ToCapacity}");
            })
        .MaxCapacity(10)
        .MinCapacity(1)
    .BuildWarmup(out completedCnn);

modelRingBuffer = RingBuffer<IModel>.New("RabbitChanels")
    .Capacity(10)
    .Logger(applogger!)
    .OnError((log, error) =>
        {
            log?.LogError("{error}", error);
        })
    .Factory((cts) => ModelFactory(cts))
    .BufferHealth((buffer) => buffer.IsOpen)
    .ScaleUnit(ScaleMode.Automatic,10,TimeSpan.FromSeconds(10))
        .ReportScale((metric, log, cts) =>
            {
                log?.LogInformation($"RabbitChanels Report: [{metric.MetricDate}]  Trigger {metric.Trigger} from {metric.FromCapacity} to {metric.ToCapacity}");
            })
        .Slave(connectionRingBuffer)
        .MaxCapacity(50)
        .MinCapacity(2)
    .BuildWarmup(out completedChanels);

Performance

Top

The BenchmarkDotNet test (5 x 1000 publish) was done on the local machine, with ‘RabbitMQ’ (over wsl). The measures are about publisher action (Scenario where Ringbuffer makes sense and brings significant performance gains).

The gain can be much greater for real machines in production!

See folder Samples/RingBufferPlusBenchmarkSample.

Notes for WithRingBufferScaler

- Default(02 connections and 10 channel) to Maximum(10 connections and 50 channel)

Notes for WithRingBuffer

- No Scale : Default = 10 connections and 50 channel

Result

BenchmarkDotNet v0.13.10, Windows 10 (10.0.19044.3693/21H2/November2021Update)
Intel Core i7-8565U CPU 1.80GHz (Whiskey Lake), 1 CPU, 8 logical and 4 physical cores
.NET SDK 8.0.100
  [Host]     : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX2
  Job-IMTEVT : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX2
  Dry        : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX2
+--------------------- +-------------+-----------+-------------+-------------+-------------+-------------+-------------+-------------+--------+------|
| Method               | Mean        | StdErr    | StdDev      | Min         | Q1          | Median      | Q3          | Max         | Op/s   | Rank |
|--------------------- |-------------|-----------|-------------|-------------|-------------|-------------|-------------|-------------|--------|------|
| WithRingBuffer       |    422.8 ms |  51.41 ms |    89.05 ms |    323.6 ms |    386.4 ms |    449.1 ms |    472.5 ms |    495.8 ms | 2.3649 |    1 |
| WithRingBufferScaler |    537.9 ms |  81.06 ms |   140.40 ms |    392.1 ms |    470.8 ms |    549.5 ms |    610.8 ms |    672.2 ms | 1.8591 |    2 |
| WithoutRingBuffer    | 84,961.4 ms | 752.59 ms | 1,303.52 ms | 84,198.0 ms | 84,208.8 ms | 84,219.6 ms | 85,343.1 ms | 86,466.5 ms | 0.0118 |    3 |
+--------------------- +-------------+-----------+-------------+-------------+-------------+-------------+-------------+-------------+--------+------|

Legends
-------
Mean   : Arithmetic mean of all measurements
StdErr : Standard error of all measurements
StdDev : Standard deviation of all measurements
Min    : Minimum
Q1     : Quartile 1 (25th percentile)
Median : Value separating the higher half of all measurements (50th percentile)
Q3     : Quartile 3 (75th percentile)
Max    : Maximum
Op/s   : Operation per second
Rank   : Relative position of current benchmark mean among all benchmarks (Arabic style)
1 ms   : 1 Millisecond (0.001 sec)

Credits

Top

This work was inspired by the project by Luis Carlos Farias. My thanks for your great work of bringing knowledge to the community!

API documentation generated by

License

Top

Copyright 2022 @ Fernando Cerqueira

RingBufferPlus is licensed under the MIT license. See LICENSE.