An open API service indexing awesome lists of open source software.

https://github.com/imhype/bounded-buffer

The Node.js SDK for the classic Producer-Consumer problem, using the Record Semaphore solution.
https://github.com/imhype/bounded-buffer

Last synced: about 2 months ago
JSON representation

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

Awesome Lists containing this project

README

        

bounded-buffer
======

[![travis-ci](https://travis-ci.com/ImHype/bounded-buffer.svg)](https://travis-ci.com/ImHype/bounded-buffer)
[![codecov](https://codecov.io/gh/ImHype/bounded-buffer/branch/master/graph/badge.svg)](https://codecov.io/gh/ImHype/bounded-buffer)

The Node.js SDK for the classic [Producer-Consumer](https://en.wikipedia.org/wiki/Producer%E2%80%93consumer_problem) problem, using the `Record Semaphore` solution.

Installation
------------

```
$ npm install bounded-buffer
```

Basic usage
-------------

```javascript
import BoundedBuffer from "bounded-buffer";

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

const boundedBuffer = new BoundedBuffer({
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:

* [Run producer automatically](./examples/consumable.ts)
* [Run consumer automatically](./examples/producable.ts)

License
-------
(MIT)

Copyright (c) 2019 ImHype