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.
- Host: GitHub
- URL: https://github.com/imhype/bounded-buffer
- Owner: ImHype
- License: mit
- Created: 2019-04-07T05:18:05.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-04-10T08:24:54.000Z (about 6 years ago)
- Last Synced: 2025-03-24T10:56:28.723Z (2 months ago)
- Language: TypeScript
- Homepage:
- Size: 92.8 KB
- Stars: 5
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
bounded-buffer
======[](https://travis-ci.com/ImHype/bounded-buffer)
[](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