Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 29 additions & 16 deletions src/RedisStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ class RedisStore extends Store {
this.client = redis.createClient(config);
this.client.on('error', (err) => console.log('Redis Client Error', err));
this.client.connect()


this.txAttempts = config.txAttempts || 1;
}

/**
Expand All @@ -50,22 +49,36 @@ class RedisStore extends Store {
*/
async _hit(key, options, weight) {

let [counter, dateEnd] = await this.client.multi().get(key).ttl(key).exec();

if(counter === null) {
counter = weight;
dateEnd = Date.now() + options.interval;

const seconds = Math.ceil(options.interval / 1000);
await this.client.setEx(key, seconds.toString(), counter.toString());
} else if (dateEnd === -2 || dateEnd === -1) {
counter = counter + weight;
dateEnd = Date.now() + options.interval;
let [counter, dateEnd] = [weight, Date.now() + options.interval];

for (let attempts=0; attempts < this.txAttempts; attempts++ ){
await this.client.watch(key);
try{
counter = await this.client.get(key);
dateEnd = await this.client.ttl(key);
} catch (err) {
console.log(err);
}

const seconds = Math.ceil(options.interval / 1000);
await this.client.setEx(key, seconds.toString(), counter.toString());
} else {
counter = await this.client.incrBy(key, weight);
if(counter === null || dateEnd === -2 || dateEnd === -1) {
if ((dateEnd === -2 || dateEnd === -1)){
counter = counter + weight;
}
try{
await this.client.multi().setEx(key, seconds.toString(), counter.toString()).exec();
break; //ends loop in case of success
} catch (err) {
console.log(err);
}
} else {
try{
counter = await this.client.multi().incrBy(key, weight).exec();
break; //ends loop in case of success
} catch (err) {
console.log(err);
}
}
}

return {
Expand Down