Skip to content

The Node.js SDK for the classic Producer-Consumer problem, using the Record Semaphore solution.

License

Notifications You must be signed in to change notification settings

ImHype/bounded-buffer

Repository files navigation

bounded-buffer

travis-ci codecov

The Node.js SDK for the classic Producer-Consumer problem, using the Record Semaphore solution.

Installation

$ npm install bounded-buffer

Basic usage

import BoundedBuffer from "bounded-buffer";

const sleep = (n: number) => new Promise((resolve) => setTimeout(resolve, n));

const boundedBuffer = new BoundedBuffer<number>({
    bufferSize: 10,
});

const put = async(n: number) => {
    let i = 0;
    while (i < n) {
        await boundedBuffer.putItem(123);
        i++;
        await sleep(5);
    }
}

const get = async(n: number) => {
    let i = 0;
    while (i < n) {
        await boundedBuffer.getItem();
        i++;
        await sleep(5);
    }
}

Promise.all(
    [
        put(100)
            .catch(e => console.error(e)),
        get(100)
            .catch(e => console.error(e))
    ]
).then(() => {
    boundedBuffer.destroy();
    console.log('bufferSize', boundedBuffer['buffer'].length)
})

Run producer or consumer automatically

Sometimes producers or consumers need to be run automatically:

License

(MIT)

Copyright (c) 2019 ImHype

About

The Node.js SDK for the classic Producer-Consumer problem, using the Record Semaphore solution.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published