Skip to content

Commit d8e98ce

Browse files
committed
feat(redis-sp): add basic synchronization primitives
1 parent 49d1cf9 commit d8e98ce

37 files changed

+8658
-1
lines changed

.editorconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
trim_trailing_whitespace = true
8+
9+
[*.{js, ts, json}]
10+
indent_size = 2
11+
indent_style = space
12+
13+
[*.{js, ts}]
14+
max_line_length = 120
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Publish Package to npmjs
2+
on:
3+
release:
4+
types: [created]
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v3
10+
- uses: actions/setup-node@v3
11+
with:
12+
node-version: '16.x'
13+
registry-url: 'https://registry.npmjs.org'
14+
- run: npm ci
15+
- run: npm run build
16+
- run: npm publish
17+
env:
18+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# JetBrains
2+
.idea/
3+
4+
# Node.js
5+
node_modules/
6+
7+
# Jest
8+
coverage/
9+
10+
#Local
11+
dist/

.prettierrc.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"trailingComma": "all",
3+
"singleQuote": true,
4+
"printWidth": 120
5+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Dmitriy Shiryayev
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,66 @@
1-
# redis-sp
1+
# redis-sp
2+
3+
Redis SP (SP stands for Synchronization Primitives) is a set of synchronization primitives based on
4+
the [Redlock](https://redis.io/docs/reference/patterns/distributed-locks/) algorithm.
5+
6+
## Installation
7+
8+
```
9+
npm install redis-sp
10+
```
11+
12+
## Usage
13+
14+
### Mutex
15+
16+
Mutex implementation based on the [Redlock](https://redis.io/docs/reference/patterns/distributed-locks/) algorithm
17+
18+
```typescript
19+
import RedisClient from 'ioredis';
20+
import { RedisMutex } from 'redis-sp';
21+
22+
async function main() {
23+
const client = new RedisClient();
24+
25+
const mutex = new RedisMutex([client], getResourceIdSomehow());
26+
await mutex.lock();
27+
28+
try {
29+
// critical section of your code
30+
await maybeFails();
31+
} finally {
32+
await mutex.unlock();
33+
}
34+
}
35+
```
36+
37+
### Counting Semaphore
38+
39+
Implementation of a counting semaphore according to
40+
the [Fair semaphores | Redis](https://redis.com/ebook/part-2-core-concepts/chapter-6-application-components-in-redis/6-3-counting-semaphores/6-3-2-fair-semaphores/)
41+
with a slight difference (no race conditions due to the atomicity of Redis Lua scripts).
42+
43+
```typescript
44+
import RedisClient from 'ioredis';
45+
import { RedisCountingSemaphore } from 'redis-sp';
46+
47+
async function main() {
48+
const client = new RedisClient();
49+
50+
const semaphore = new RedisCountingSemaphore(
51+
[client],
52+
getSharedResourceIdSomehow(),
53+
getMaxSharedResourceOwnersSomehow(),
54+
);
55+
await semaphore.acquire();
56+
57+
try {
58+
// critical section of your code
59+
await maybeFails();
60+
} finally {
61+
await semaphore.release();
62+
}
63+
}
64+
```
65+
66+
Stay tuned for RW lock in the next major release!

0 commit comments

Comments
 (0)