FIX (rel #3)
This commit is contained in:
parent
dd7430503d
commit
4317123ffe
@ -1,5 +1,3 @@
|
|||||||
const crypto = require('crypto');
|
|
||||||
|
|
||||||
export class Lock {
|
export class Lock {
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -18,20 +16,10 @@ export class Lock {
|
|||||||
* @returns {Promise<boolean>}
|
* @returns {Promise<boolean>}
|
||||||
*/
|
*/
|
||||||
async obtainLock() {
|
async obtainLock() {
|
||||||
if (await this._redis.get(this._lock_key)) {
|
const timestamp = +new Date();
|
||||||
// someone already has the lock
|
let setResult = await this._redis.setnx(this._lock_key, timestamp);
|
||||||
return false;
|
if (!setResult) {
|
||||||
}
|
// it already held a value - failed locking
|
||||||
|
|
||||||
// trying to set the lock:
|
|
||||||
let buffer = crypto.randomBytes(10);
|
|
||||||
const randomValue = buffer.toString('hex');
|
|
||||||
await this._redis.set(this._lock_key, randomValue);
|
|
||||||
|
|
||||||
// checking if it was set:
|
|
||||||
let value = await this._redis.get(this._lock_key);
|
|
||||||
if (value !== randomValue) {
|
|
||||||
// someone else managed to obtain this lock
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user