Я практикуюсь на redis и redlock, и я пишу некоторую функцию, например, для обработки базовой транзакции банка. У меня есть 2 счета, и я хочу перевести деньги между этими 2 счетами. Для решения проблемы синхронизма я использую модуль redlock. это мой код, но я получаю сообщение об ошибке:
UnhandledPromiseRejectionWarning: LockError: Exceeded 10 attempts to lock the resource "bahrami".
и это мой код:
const redis = require('./redis')
const readline = require('readline')
var Redlock = require('redlock');
var redlock = new Redlock(
// you should have one client for each independent redis node
// or cluster
[redis],
{
// the expected clock drift; for more details
// see http://redis.io/topics/distlock
driftFactor: 0.01, // time in ms
// the max number of times Redlock will attempt
// to lock a resource before erroring
retryCount: 10,
// the time in ms between attempts
retryDelay: 200, // time in ms
// the max time in ms randomly added to retries
// to improve performance under high contention
// see https://www.awsarchitectureblog.com/2015/03/backoff.html
retryJitter: 200 // time in ms
}
);
const ttl = 1000;
const transferMoney = async () => {
const sourceAccount = await getInput('enter sourceAccount');
const destinationAccount = await getInput('enter destinationAccount');
const money = await getInput ('enter amount of money ')
const sourceLock = await redlock.lock(sourceAccount, ttl)
const sourceAccountAmount = await redis.get(sourceAccount)
const destLock = await redlock.lock(destinationAccount, ttl)
const destinationAccountAmount = await redis.get(destinationAccount)
if (sourceAccountAmount >= money){
await redis.set(sourceAccount, sourceAccountAmount - money)
const sum = parseInt(destinationAccountAmount) + parseInt(money)
await redis.set(destinationAccount, sum)
}
else{
console.log('your money does not enough!')
}
const tr = await redis.lpush(`transaction:s:${sourceAccount}:d:${destinationAccount}`, JSON.stringify({Amount}))
console.log(tr)
await sourceLock.unlock()
await destLock.unlock()
}