Answer the question
In order to leave comments, you need to log in
NodeJS & Lock & Race?
How can I organize something like this cheaply? Taking into account the fact that locking should work within the server (several instance's are raised).
await this.lock(token /* str */);
// ... some code
await this.unlock(token);
Answer the question
In order to leave comments, you need to log in
We usually use Redis for such purposes. They have an algorithm in the documentation .
The simplest option without expire:
redis.setnx(token, 'lock', function (err, acquired) {
if (err) { /*...*/ }
if (acquired) {
// ... some code
redis.del(token, function (err) { /*...*/ }); // unlock
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question