Skip to content

Commit e461243

Browse files
committed
Add comments to explain how ip countries bucket works
1 parent f1236bd commit e461243

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

pkg/lib/fraudprotection/leaky_bucket_store.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,16 @@ var ipCountriesScript = `
9797
local now = tonumber(ARGV[2])
9898
local cutoff = now - 86400
9999
100+
-- 1. Use ZADD to record a send event in sent-countries sorted set key
100101
redis.call('ZADD', KEYS[1], now, ARGV[1])
102+
-- 2. use ZREMRANGEBYSCORE to drop records older than cutoff in both sets before processing
101103
redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', cutoff)
102104
redis.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
103106
redis.call('EXPIRE', KEYS[1], ARGV[4])
104107
redis.call('EXPIRE', KEYS[2], ARGV[4])
105108
109+
-- 4. Derive counties without at least one verified otp
106110
local sent_countries = redis.call('ZRANGE', KEYS[1], 0, -1)
107111
local verified_countries = redis.call('ZRANGE', KEYS[2], 0, -1)
108112
local 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
251256
redis.call('ZADD', KEYS[1], ARGV[1], ARGV[2])
252257
redis.call('EXPIRE', KEYS[1], ARGV[3])
253258
return 1

0 commit comments

Comments
 (0)