Skip to content

Commit 046eac0

Browse files
authored
imp(notifier): add ttl for redis records (#391)
* imp(notifier): add ttl for redis records * imp(notifier): replace hardcoded value with util const
1 parent 6fafa98 commit 046eac0

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

workers/notifier/src/redisHelper.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createClient, RedisClientType } from 'redis';
22
import { Rule } from '../types/rule';
33
import { NotifierEvent } from '../types/notifier-task';
4+
import { MS_IN_SEC } from '../../../lib/utils/consts';
45

56
/**
67
* Class with helper functions for working with Redis
@@ -57,12 +58,14 @@ export default class RedisHelper {
5758
local key = KEYS[1]
5859
local currentTimestamp = tonumber(ARGV[1])
5960
local thresholdPeriod = tonumber(ARGV[2])
61+
local ttl = tonumber(ARGV[3])
6062
6163
local startPeriodTimestamp = tonumber(redis.call("HGET", key, "timestamp"))
6264
6365
if ((startPeriodTimestamp == nil) or (currentTimestamp >= startPeriodTimestamp + thresholdPeriod)) then
6466
redis.call("HSET", key, "timestamp", currentTimestamp)
6567
redis.call("HSET", key, "eventsCount", 0)
68+
redis.call("EXPIRE", key, ttl)
6669
end
6770
6871
local newCounter = redis.call("HINCRBY", key, "eventsCount", 1)
@@ -71,11 +74,16 @@ export default class RedisHelper {
7174

7275
const key = `${projectId}:${ruleId}:${groupHash}:${thresholdPeriod}:times`;
7376

77+
/**
78+
* Treshold period is in milliseconds, but redis expects ttl in seconds
79+
*/
80+
const ttl = Math.floor(thresholdPeriod / MS_IN_SEC);
81+
7482
const currentTimestamp = Date.now();
7583

7684
const currentEventCount = await this.redisClient.eval(script, {
7785
keys: [ key ],
78-
arguments: [currentTimestamp.toString(), thresholdPeriod.toString()],
86+
arguments: [currentTimestamp.toString(), thresholdPeriod.toString(), ttl.toString()],
7987
}) as number;
8088

8189
return (currentEventCount !== null) ? currentEventCount : 0;

workers/notifier/tests/redisHelper.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,37 @@ describe('RedisHelper', () => {
9494
expect(currentEventCount).toBe(1);
9595
expect(currentlyStoredTimestamp).toBe(Date.now().toString());
9696
});
97+
98+
describe('key expiration period', () => {
99+
it('should expire key after ttl period', async () => {
100+
const ruleId = 'ruleId';
101+
const groupHash = 'groupHash';
102+
const projectId = 'projectId';
103+
const thresholdPeriod = 2000; // 2 seconds in milliseconds
104+
const key = `${projectId}:${ruleId}:${groupHash}:${thresholdPeriod}:times`;
105+
106+
/**
107+
* Call computeEventCountForPeriod to set the key
108+
*/
109+
await redisHelper.computeEventCountForPeriod(projectId, ruleId, groupHash, thresholdPeriod);
110+
111+
/**
112+
* Verify, that key exists
113+
*/
114+
let value = await redisClient.hGet(key, 'eventsCount');
115+
expect(value).not.toBeNull();
116+
117+
/**
118+
* Wait for the TTL to expire (slightly longer than threshold period)
119+
*/
120+
await new Promise(resolve => setTimeout(resolve, thresholdPeriod + 500));
121+
122+
/**
123+
* Verify, that key has expired and removed from the redis storage
124+
*/
125+
value = await redisClient.hGet(key, 'eventsCount');
126+
expect(value).toBeNull();
127+
});
128+
});
97129
});
98130
});

0 commit comments

Comments
 (0)