Buffers

Overview

Buffers are a data structures which are used by channels in this library to maintain a queue of take and put requests. There a several different buffer types which will behave differently when an item is being added to a full buffer.

Buffers have isBlocked property which will determine whether an operation on the channel, for example puts or takes, will be blocked or not.

There is one buffer for keeping put requests and one buffer for keeping take requests and both of these buffers are used to determine whether the channel should block put or take respectively.

Fixed buffer

import { makeChannel } from 'csp-coffee/channel';
import { CreatableBufferType } from 'csp-coffee/buffer';

const ch = makeChannel(CreatableBufferType.FIXED);

Fixed buffer is the simplest one and is used by default by the makeChannel function. When it reaches its capacity treshold it will be blocked and any new items are not going to be added.

Dropping buffer

import { makeChannel } from 'csp-coffee/channel';
import { CreatableBufferType } from 'csp-coffee/buffer';

const ch = makeChannel(CreatableBufferType.DROPPING);

Dropping buffer will not be blocked when reaches its capacity treshold but any new items simply will not be added to it e.g will be dropped.

For example, let's say we have a dropping buffer which contains values 1,2,3 and has a capacity of 3. We want to add a new value of 4 to the buffer. Since it is a dropping buffer, it will drop the incoming value of 4, and the result values list will be 1,2,3

Sliding buffer

import { makeChannel } from 'csp-coffee/channel';
import { CreatableBufferType } from 'csp-coffee/buffer';

const ch = makeChannel(CreatableBufferType.SLIDING);

Sliding buffer will not be blocked when reaches its capacity treshold but when new items are being added to a full sliding buffer, it will drop one item starting from the oldest ones e.g from the ones that were added first.

For example, let's say we have a sliding buffer which contains values 1,2,3 and has a capacity of 3. We want to add a new value of 4 to the buffer. Since it is a sliding buffer, it will drop the value of 1, and the result values list will be 2,3,4

Unblocking buffer

import { makeChannel } from 'csp-coffee/channel';
import { CreatableBufferType } from 'csp-coffee/buffer';

const ch = makeChannel(CreatableBufferType.UNBLOCKING);

This type of buffer will never be blocked and has unlimited capacity. I didn't test how this buffer will perform with a really big number of values.

Closed buffer

This one is used internally and you will probably never want to use it yourself. You will only find this type of buffer in a closed channel and it mostly acts as a placeholder. New values are never being added into a closed buffer.

Last updated