Skip to content

Commit 0816471

Browse files
committed
chore: updated README
1 parent fb5a066 commit 0816471

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

README.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ yarn add redis-semaphore ioredis
3838
- `acquireAttemptsLimit` - _optional_ max number of attempts to be made in `.acquire()` call
3939
- `retryInterval` - _optional_ ms, time between acquire attempts if resource locked
4040
- `refreshInterval` - _optional_ ms, auto-refresh interval; to disable auto-refresh behaviour set `0`
41+
- `externallyAcquiredIdentifier` - _optional_ uuid, previously acquired mutex identifier (useful for lock sharing between processes: acquire in scheduler, refresh and release in handler)
4142
- `onLockLost` - _optional_ function, called when lock loss is detected due refresh cycle; default onLockLost throws unhandled LostLockError
4243

4344
#### Example
@@ -120,14 +121,59 @@ async function doSomething() {
120121
return
121122
}
122123
try {
123-
// critical cycle iteration
124+
// critical cycle iteration
124125
} finally {
125-
// We want to let lock expire over time after operation is finished
126+
// We want to let lock expire over time after operation is finished
126127
await mutex.stopRefresh()
127128
}
128129
}
129130
```
130131

132+
Example with `externallyAcquiredIdentifier`
133+
134+
```javascript
135+
const Mutex = require('redis-semaphore').Mutex
136+
const Redis = require('ioredis')
137+
138+
// TypeScript
139+
// import { Mutex } from 'redis-semaphore'
140+
// import Redis from 'ioredis'
141+
142+
const redisClient = new Redis()
143+
144+
// scheduler app code
145+
async function every10MinutesCronScheduler() {
146+
const mutex = new Mutex(redisClient, 'lockingResource', {
147+
lockTimeout: 30 * 60 * 1e3, // lock for 30min
148+
refreshInterval: 0
149+
})
150+
if (await mutex.tryAcquire()) {
151+
someQueue.publish({ mutexIdentifier: mutex.identifier })
152+
} else {
153+
logger.info('Job already scheduled. Do nothing in current cron cycle')
154+
}
155+
}
156+
157+
// handler app code
158+
async function queueHandler(queueMessageData) {
159+
const { mutexIdentifier } = queueMessageData
160+
const mutex = new Mutex(redisClient, 'lockingResource', {
161+
lockTimeout: 10 * 1e3, // 10sec
162+
externallyAcquiredIdentifier: mutexIdentifier
163+
})
164+
165+
// actually will do `refresh` with new lockTimeout instead of acquire
166+
// if mutex was locked by another process or lock was expired - exception will be thrown (default refresh behavior)
167+
await mutex.acquire()
168+
169+
try {
170+
// critical code
171+
} finally {
172+
await mutex.release()
173+
}
174+
}
175+
```
176+
131177
### Semaphore
132178

133179
> See [RedisLabs: Basic counting sempahore](https://redislabs.com/ebook/part-2-core-concepts/chapter-6-application-components-in-redis/6-3-counting-semaphores/6-3-1-building-a-basic-counting-semaphore/)

0 commit comments

Comments
 (0)