@@ -28,13 +28,14 @@ yarn add redis-semaphore ioredis
2828
2929> See [ RedisLabs: Locks with timeouts] ( https://redislabs.com/ebook/part-2-core-concepts/chapter-6-application-components-in-redis/6-2-distributed-locking/6-2-5-locks-with-timeouts/ )
3030
31- ##### new Mutex(redisClient, key [ , { lockTimeout = 10000, acquireTimeout = 10000, retryInterval = 10, refreshInterval = lockTimeout * 0.8 }] )
31+ ##### new Mutex(redisClient, key [ , { lockTimeout = 10000, acquireTimeout = 10000, acquireAttemptsLimit = Number.POSITIVE_INFINITY, retryInterval = 10, refreshInterval = lockTimeout * 0.8 }] )
3232
3333- ` redisClient ` - ** required** , configured ` redis ` client
3434- ` key ` - ** required** , key for locking resource (final key in redis: ` mutex:<key> ` )
3535- ` options ` - _ optional_
3636 - ` lockTimeout ` - _ optional_ ms, time after mutex will be auto released (expired)
3737 - ` acquireTimeout ` - _ optional_ ms, max timeout for ` .acquire() ` call
38+ - ` acquireAttemptsLimit ` - _ optional_ max number of attempts to be made in ` .acquire() ` call
3839 - ` retryInterval ` - _ optional_ ms, time between acquire attempts if resource locked
3940 - ` refreshInterval ` - _ optional_ ms, auto-refresh interval; to disable auto-refresh behaviour set ` 0 `
4041 - ` onLockLost ` - _ optional_ function, called when lock loss is detected due refresh cycle; default onLockLost throws unhandled LostLockError
@@ -88,7 +89,9 @@ async function doSomething() {
8889
8990``` javascript
9091async function doSomething () {
91- const mutex = new Mutex (redisClient, ' lockingResource' , { acquireTimeout: 0 })
92+ const mutex = new Mutex (redisClient, ' lockingResource' , {
93+ acquireAttemptsLimit: 1
94+ })
9295 const lockAcquired = await mutex .tryAcquire ()
9396 if (! lockAcquired) {
9497 return
@@ -116,7 +119,7 @@ In edge cases (node time difference is greater than `lockTimeout`) both algorith
116119
117120Most reliable way to use: ` lockTimeout ` is greater than possible node clock differences, ` refreshInterval ` is not 0 and is less enough than ` lockTimeout ` (by default is ` lockTimeout * 0.8 ` )
118121
119- ##### new Semaphore(redisClient, key, maxCount [ , { lockTimeout = 10000, acquireTimeout = 10000, retryInterval = 10, refreshInterval = lockTimeout * 0.8 }] )
122+ ##### new Semaphore(redisClient, key, maxCount [ , { lockTimeout = 10000, acquireTimeout = 10000, acquireAttemptsLimit = Number.POSITIVE_INFINITY, retryInterval = 10, refreshInterval = lockTimeout * 0.8 }] )
120123
121124- ` redisClient ` - ** required** , configured ` redis ` client
122125- ` key ` - ** required** , key for locking resource (final key in redis: ` semaphore:<key> ` )
@@ -152,7 +155,7 @@ Same as `Semaphore` with one difference - MultiSemaphore will try to acquire mul
152155
153156` MultiSemaphore ` and ` Semaphore ` shares same key namespace and can be used together (see test/src/RedisMultiSemaphore.test.ts).
154157
155- ##### new MultiSemaphore(redisClient, key, maxCount, permits [ , { lockTimeout = 10000, acquireTimeout = 10000, retryInterval = 10, refreshInterval = lockTimeout * 0.8 }] )
158+ ##### new MultiSemaphore(redisClient, key, maxCount, permits [ , { lockTimeout = 10000, acquireTimeout = 10000, acquireAttemptsLimit = Number.POSITIVE_INFINITY, retryInterval = 10, refreshInterval = lockTimeout * 0.8 }] )
156159
157160- ` redisClient ` - ** required** , configured ` redis ` client
158161- ` key ` - ** required** , key for locking resource (final key in redis: ` semaphore:<key> ` )
@@ -190,7 +193,7 @@ Distributed `Mutex` version
190193
191194> See [ The Redlock algorithm] ( https://redis.io/topics/distlock#the-redlock-algorithm )
192195
193- ##### new RedlockMutex(redisClients, key [ , { lockTimeout = 10000, acquireTimeout = 10000, retryInterval = 10, refreshInterval = lockTimeout * 0.8 }] )
196+ ##### new RedlockMutex(redisClients, key [ , { lockTimeout = 10000, acquireTimeout = 10000, acquireAttemptsLimit = Number.POSITIVE_INFINITY, retryInterval = 10, refreshInterval = lockTimeout * 0.8 }] )
194197
195198- ` redisClients ` - ** required** , array of configured ` redis ` client connected to independent nodes
196199- ` key ` - ** required** , key for locking resource (final key in redis: ` mutex:<key> ` )
@@ -229,7 +232,7 @@ Distributed `Semaphore` version
229232
230233> See [ The Redlock algorithm] ( https://redis.io/topics/distlock#the-redlock-algorithm )
231234
232- ##### new RedlockSemaphore(redisClients, key, maxCount [ , { lockTimeout = 10000, acquireTimeout = 10000, retryInterval = 10, refreshInterval = lockTimeout * 0.8 }] )
235+ ##### new RedlockSemaphore(redisClients, key, maxCount [ , { lockTimeout = 10000, acquireTimeout = 10000, acquireAttemptsLimit = Number.POSITIVE_INFINITY, retryInterval = 10, refreshInterval = lockTimeout * 0.8 }] )
233236
234237- ` redisClients ` - ** required** , array of configured ` redis ` client connected to independent nodes
235238- ` key ` - ** required** , key for locking resource (final key in redis: ` semaphore:<key> ` )
@@ -269,7 +272,7 @@ Distributed `MultiSemaphore` version
269272
270273> See [ The Redlock algorithm] ( https://redis.io/topics/distlock#the-redlock-algorithm )
271274
272- ##### new RedlockMultiSemaphore(redisClients, key, maxCount, permits [ , { lockTimeout = 10000, acquireTimeout = 10000, retryInterval = 10, refreshInterval = lockTimeout * 0.8 }] )
275+ ##### new RedlockMultiSemaphore(redisClients, key, maxCount, permits [ , { lockTimeout = 10000, acquireTimeout = 10000, acquireAttemptsLimit = Number.POSITIVE_INFINITY, retryInterval = 10, refreshInterval = lockTimeout * 0.8 }] )
273276
274277- ` redisClients ` - ** required** , array of configured ` redis ` client connected to independent nodes
275278- ` key ` - ** required** , key for locking resource (final key in redis: ` semaphore:<key> ` )
0 commit comments