11import { createClient , RedisClientType } from 'redis' ;
22import { Rule } from '../types/rule' ;
33import { 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 ;
0 commit comments