@@ -97,12 +97,16 @@ var ipCountriesScript = `
9797local now = tonumber(ARGV[2])
9898local cutoff = now - 86400
9999
100+ -- 1. Use ZADD to record a send event in sent-countries sorted set key
100101redis.call('ZADD', KEYS[1], now, ARGV[1])
102+ -- 2. use ZREMRANGEBYSCORE to drop records older than cutoff in both sets before processing
101103redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', cutoff)
102104redis.call('ZREMRANGEBYSCORE', KEYS[2], '-inf', cutoff)
105+ -- 3. Update the expiry of both set to ensure they are not cleaned up when we still need them
103106redis.call('EXPIRE', KEYS[1], ARGV[4])
104107redis.call('EXPIRE', KEYS[2], ARGV[4])
105108
109+ -- 4. Derive counties without at least one verified otp
106110local sent_countries = redis.call('ZRANGE', KEYS[1], 0, -1)
107111local verified_countries = redis.call('ZRANGE', KEYS[2], 0, -1)
108112local verified_lookup = {}
@@ -248,6 +252,7 @@ func (s *LeakyBucketStore) RecordSMSOTPVerifiedCountry(ctx context.Context, ip,
248252
249253 return s .Redis .WithConnContext (ctx , func (ctx context.Context , conn redis.Redis_6_0_Cmdable ) error {
250254 return conn .Eval (ctx , `
255+ -- Update the last verified otp timestamp of the country code in the sorted set
251256redis.call('ZADD', KEYS[1], ARGV[1], ARGV[2])
252257redis.call('EXPIRE', KEYS[1], ARGV[3])
253258return 1
0 commit comments