@@ -191,32 +191,28 @@ func (r *RedisClient) UpdateRateLimit(projectID string, eventsLimit int64, event
191191 local limit = tonumber(ARGV[3])
192192 local period = tonumber(ARGV[4])
193193
194+ -- Read current window value
194195 local current = redis.call('HGET', key, field)
195196 if not current then
196- -- No existing record, create new window
197- redis.call('HSET', key, field, now .. ':1')
197+ -- No existing record, event count is within limit
198198 return 1
199199 end
200200
201201 local timestamp, count = string.match(current, '(%d+):(%d+)')
202202 timestamp = tonumber(timestamp)
203203 count = tonumber(count)
204204
205- -- Check if we're in a new time window
205+ -- If we're in a new time window - event count is within limit
206206 if now - timestamp >= period then
207- -- Reset for new window
208- redis.call('HSET', key, field, now .. ':1')
209207 return 1
210208 end
211209
212- -- Check if incrementing would exceed limit
213- if count + 1 > limit then
214- return 0
210+ -- Still in current window: check if event count is within limit
211+ if count < limit then
212+ return 1
215213 end
216214
217- -- Increment counter
218- redis.call('HSET', key, field, timestamp .. ':' .. (count + 1))
219- return 1
215+ return 0
220216 `
221217
222218 // Run the script
0 commit comments