Skip to content

Commit 3209f62

Browse files
author
Matt Madigan
committed
Change to using UTC time stamps, Add TTL to keys
1 parent 042a156 commit 3209f62

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

RateLimit.Redis/Decepticon.RateLimit.Redis.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Donate via Paypal: dekcodeofficial@gmail.com</Description>
1919
<PackageIcon>icon.png</PackageIcon>
2020
<PackageReleaseNotes></PackageReleaseNotes>
2121
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
22-
<Version>1.0.0</Version>
22+
<Version>1.1.0</Version>
2323
</PropertyGroup>
2424

2525
<ItemGroup>

RateLimit.Redis/Helpers/DateTimeHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal static class DateTimeHelpers
1717
public static DateTime UnixTimeStampToDateTime(double unixTimeStampInSeconds)
1818
{
1919
// Unix timestamp is seconds past epoch
20-
return _Epoch.AddSeconds(unixTimeStampInSeconds).ToLocalTime();
20+
return _Epoch.AddSeconds(unixTimeStampInSeconds).ToUniversalTime();
2121
}
2222
}
2323
}

RateLimit.Redis/RateLimiter.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ public ThrottleRateLimitResult Validate(ThrottleRateLimitRequest request)
176176
local count = tonumber(ARGV[3])
177177
local ticks = currentTimeTicks
178178
local result = 1
179+
local ttl = 86400
179180
180181
-- Try getting tracking config from existing record
181182
local countStr = redis.call('GET', KEYS[1])
@@ -206,15 +207,18 @@ public ThrottleRateLimitResult Validate(ThrottleRateLimitRequest request)
206207
207208
-- Consume a token
208209
count = count - 1
210+
211+
-- Get seconds for a complete refill, and then add 1hr to prevent deleting keys too frequently
212+
ttl = (capacity / refillRate) + 3600
209213
210214
-- Determine the outcome
211215
if count < 0 then
212216
result = 0
213217
count = 0
214218
end
215219
216-
redis.call('SET', KEYS[1], count)
217-
redis.call('SET', KEYS[2], ticks)
220+
redis.call('SET', KEYS[1], count, 'EX', ttl)
221+
redis.call('SET', KEYS[2], ticks, 'EX', ttl)
218222
219223
return result .. ',' .. math.abs(10000000 / refillRate)
220224
";

0 commit comments

Comments
 (0)