redis-semaphore
Advanced tools
Comparing version
@@ -0,1 +1,5 @@ | ||
### redis-semaphore@5.1.0 | ||
- Added `tryAcquire` | ||
### redis-semaphore@5.0.0 | ||
@@ -2,0 +6,0 @@ |
@@ -7,4 +7,6 @@ import MultiSemaphore from './RedisMultiSemaphore'; | ||
import RedlockSemaphore from './RedlockSemaphore'; | ||
import LostLockError from './errors/LostLockError'; | ||
import TimeoutError from './errors/TimeoutError'; | ||
export { defaultTimeoutOptions } from './misc'; | ||
export { Mutex, Semaphore, MultiSemaphore, RedlockMutex, RedlockSemaphore, RedlockMultiSemaphore }; | ||
export { Mutex, Semaphore, MultiSemaphore, RedlockMutex, RedlockSemaphore, RedlockMultiSemaphore, LostLockError, TimeoutError }; | ||
export type { LockLostCallback, TimeoutOptions, LockOptions } from './types'; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.RedlockMultiSemaphore = exports.RedlockSemaphore = exports.RedlockMutex = exports.MultiSemaphore = exports.Semaphore = exports.Mutex = exports.defaultTimeoutOptions = void 0; | ||
exports.TimeoutError = exports.LostLockError = exports.RedlockMultiSemaphore = exports.RedlockSemaphore = exports.RedlockMutex = exports.MultiSemaphore = exports.Semaphore = exports.Mutex = exports.defaultTimeoutOptions = void 0; | ||
const RedisMultiSemaphore_1 = __importDefault(require("./RedisMultiSemaphore")); | ||
@@ -20,4 +20,8 @@ exports.MultiSemaphore = RedisMultiSemaphore_1.default; | ||
exports.RedlockSemaphore = RedlockSemaphore_1.default; | ||
const LostLockError_1 = __importDefault(require("./errors/LostLockError")); | ||
exports.LostLockError = LostLockError_1.default; | ||
const TimeoutError_1 = __importDefault(require("./errors/TimeoutError")); | ||
exports.TimeoutError = TimeoutError_1.default; | ||
var misc_1 = require("./misc"); | ||
Object.defineProperty(exports, "defaultTimeoutOptions", { enumerable: true, get: function () { return misc_1.defaultTimeoutOptions; } }); | ||
//# sourceMappingURL=index.js.map |
@@ -29,4 +29,5 @@ /// <reference types="node" /> | ||
acquire(): Promise<void>; | ||
tryAcquire(): Promise<boolean>; | ||
release(): Promise<void>; | ||
} | ||
export {}; |
@@ -76,2 +76,14 @@ "use strict"; | ||
} | ||
async tryAcquire() { | ||
debug(`tryAcquire ${this._kind} (key: ${this._key})`); | ||
const acquired = await this._acquire(); | ||
if (!acquired) { | ||
return false; | ||
} | ||
this._acquired = true; | ||
if (this._refreshTimeInterval > 0) { | ||
this._startRefresh(); | ||
} | ||
return true; | ||
} | ||
async release() { | ||
@@ -82,3 +94,5 @@ debug(`release ${this._kind} (key: ${this._key}, identifier: ${this._identifier})`); | ||
} | ||
await this._release(); | ||
if (this._acquired) { | ||
await this._release(); | ||
} | ||
this._acquired = false; | ||
@@ -85,0 +99,0 @@ } |
{ | ||
"name": "redis-semaphore", | ||
"version": "5.0.0", | ||
"version": "5.1.0", | ||
"description": "Distributed mutex and semaphore based on Redis", | ||
@@ -32,12 +32,12 @@ "main": "lib/index.js", | ||
"@swarthy/wait-for": "^2.1.1", | ||
"@types/chai": "^4.3.1", | ||
"@types/chai": "^4.3.3", | ||
"@types/chai-as-promised": "^7.1.5", | ||
"@types/debug": "^4.1.7", | ||
"@types/mocha": "^9.1.1", | ||
"@types/node": "17.0.31", | ||
"@types/sinon": "^10.0.11", | ||
"@types/mocha": "^10.0.0", | ||
"@types/node": "18.8.3", | ||
"@types/sinon": "^10.0.13", | ||
"@types/sinon-chai": "^3.2.8", | ||
"@types/uuid": "^8.3.4", | ||
"@typescript-eslint/eslint-plugin": "5.21.0", | ||
"@typescript-eslint/parser": "5.21.0", | ||
"@typescript-eslint/eslint-plugin": "5.39.0", | ||
"@typescript-eslint/parser": "5.39.0", | ||
"benchmark": "^2.1.4", | ||
@@ -47,14 +47,14 @@ "chai": "4.3.6", | ||
"coveralls": "^3.1.1", | ||
"eslint": "8.14.0", | ||
"eslint": "8.24.0", | ||
"eslint-config-inclusive": "1.2.10", | ||
"eslint-plugin-node": "11.1.0", | ||
"ioredis": "^5.0.4", | ||
"ioredis": "^5.2.3", | ||
"mocha": "10.0.0", | ||
"mocha-lcov-reporter": "^1.3.0", | ||
"nyc": "^15.1.0", | ||
"sinon": "13.0.2", | ||
"sinon": "14.0.1", | ||
"sinon-chai": "3.7.0", | ||
"snyk": "1.915.0", | ||
"ts-node": "^10.7.0", | ||
"typescript": "^4.6.4" | ||
"snyk": "1.1024.0", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^4.8.4" | ||
}, | ||
@@ -69,4 +69,4 @@ "engines": { | ||
"debug": "^4.3.4", | ||
"uuid": "^8.3.2" | ||
"uuid": "^9.0.0" | ||
} | ||
} |
@@ -87,2 +87,22 @@ # redis-semaphore | ||
#### Example with optional lock | ||
```javascript | ||
async function doSomething() { | ||
const mutex = new Mutex(redisClient, 'lockingResource', { acquireTimeout: 0 }) | ||
const lockAcquired = await mutex.tryAcquire() | ||
if (!lockAcquired) { | ||
return | ||
} | ||
try { | ||
while (mutex.isAcquired) { | ||
// critical cycle iteration | ||
} | ||
} finally { | ||
// It's safe to always call release, because if lock is no longer belongs to this mutex, .release() will have no effect | ||
await mutex.release() | ||
} | ||
} | ||
``` | ||
### Semaphore | ||
@@ -89,0 +109,0 @@ |
@@ -7,2 +7,4 @@ import MultiSemaphore from './RedisMultiSemaphore' | ||
import RedlockSemaphore from './RedlockSemaphore' | ||
import LostLockError from './errors/LostLockError' | ||
import TimeoutError from './errors/TimeoutError' | ||
@@ -17,5 +19,7 @@ export { defaultTimeoutOptions } from './misc' | ||
RedlockSemaphore, | ||
RedlockMultiSemaphore | ||
RedlockMultiSemaphore, | ||
LostLockError, | ||
TimeoutError | ||
} | ||
export type { LockLostCallback, TimeoutOptions, LockOptions } from './types' |
@@ -117,2 +117,15 @@ import createDebug from 'debug' | ||
async tryAcquire() { | ||
debug(`tryAcquire ${this._kind} (key: ${this._key})`) | ||
const acquired = await this._acquire() | ||
if (!acquired) { | ||
return false | ||
} | ||
this._acquired = true | ||
if (this._refreshTimeInterval > 0) { | ||
this._startRefresh() | ||
} | ||
return true | ||
} | ||
async release() { | ||
@@ -125,5 +138,7 @@ debug( | ||
} | ||
await this._release() | ||
if (this._acquired) { | ||
await this._release() | ||
} | ||
this._acquired = false | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
129574
1.81%2380
1.67%334
6.37%+ Added
- Removed
Updated