Skip to content

Commit 562c1b4

Browse files
authored
Merge pull request #205 from Kkamikadzee/master
Switch from Unix seconds to milliseconds
2 parents 1506f4c + b0a2fb8 commit 562c1b4

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

src/RedisRateLimiting/Concurrency/RedisConcurrencyManager.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ internal class RedisConcurrencyManager
1818
local queue_limit = tonumber(@queue_limit)
1919
local try_enqueue = tonumber(@try_enqueue)
2020
local timestamp = tonumber(@current_time)
21-
-- max seconds it takes to complete a request
22-
local ttl = 60
21+
-- max milliseconds it takes to complete a request
22+
local ttl = 60000
2323
2424
redis.call(""zremrangebyscore"", @rate_limit_key, '-inf', timestamp - ttl)
2525
@@ -116,7 +116,7 @@ public RedisConcurrencyManager(
116116

117117
internal async Task<RedisConcurrencyResponse> TryAcquireLeaseAsync(string requestId, bool tryEnqueue = false)
118118
{
119-
var nowUnixTimeSeconds = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
119+
var unixTimeMilliseconds = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
120120

121121
var database = _connectionMultiplexer.GetDatabase();
122122

@@ -130,7 +130,7 @@ internal async Task<RedisConcurrencyResponse> TryAcquireLeaseAsync(string reques
130130
permit_limit = (RedisValue)_options.PermitLimit,
131131
try_enqueue = (RedisValue)(tryEnqueue ? 1 : 0),
132132
queue_limit = (RedisValue)_options.QueueLimit,
133-
current_time = (RedisValue)nowUnixTimeSeconds,
133+
current_time = (RedisValue)unixTimeMilliseconds,
134134
unique_id = (RedisValue)requestId,
135135
});
136136

@@ -149,7 +149,7 @@ internal async Task<RedisConcurrencyResponse> TryAcquireLeaseAsync(string reques
149149

150150
internal RedisConcurrencyResponse TryAcquireLease(string requestId, bool tryEnqueue = false)
151151
{
152-
var nowUnixTimeSeconds = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
152+
var unixTimeMilliseconds = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
153153

154154
var database = _connectionMultiplexer.GetDatabase();
155155

@@ -163,7 +163,7 @@ internal RedisConcurrencyResponse TryAcquireLease(string requestId, bool tryEnqu
163163
permit_limit = (RedisValue)_options.PermitLimit,
164164
try_enqueue = (RedisValue)(tryEnqueue ? 1 : 0),
165165
queue_limit = (RedisValue)_options.QueueLimit,
166-
current_time = (RedisValue)nowUnixTimeSeconds,
166+
current_time = (RedisValue)unixTimeMilliseconds,
167167
unique_id = (RedisValue)requestId,
168168
});
169169

src/RedisRateLimiting/FixedWindow/RedisFixedWindowManager.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ internal class RedisFixedWindowManager
2525
redis.call(""set"", @expires_at_key, @next_expires_at)
2626
-- tell Redis to clean this up _one second after_ the expires_at time (clock differences).
2727
-- (Redis will only clean up these keys long after the window has passed)
28-
redis.call(""expireat"", @rate_limit_key, @next_expires_at + 1)
29-
redis.call(""expireat"", @expires_at_key, @next_expires_at + 1)
28+
redis.call(""pexpireat"", @rate_limit_key, @next_expires_at + 1)
29+
redis.call(""pexpireat"", @expires_at_key, @next_expires_at + 1)
3030
-- since the database was updated, return the new value
3131
expires_at = @next_expires_at
3232
end
@@ -65,7 +65,7 @@ public RedisFixedWindowManager(
6565
internal async Task<RedisFixedWindowResponse> TryAcquireLeaseAsync(int permitCount)
6666
{
6767
var now = DateTimeOffset.UtcNow;
68-
var nowUnixTimeSeconds = now.ToUnixTimeSeconds();
68+
var unixTimeMilliseconds = now.ToUnixTimeMilliseconds();
6969

7070
var database = _connectionMultiplexer.GetDatabase();
7171

@@ -75,8 +75,8 @@ internal async Task<RedisFixedWindowResponse> TryAcquireLeaseAsync(int permitCou
7575
{
7676
rate_limit_key = RateLimitKey,
7777
expires_at_key = RateLimitExpireKey,
78-
next_expires_at = (RedisValue)now.Add(_options.Window).ToUnixTimeSeconds(),
79-
current_time = (RedisValue)nowUnixTimeSeconds,
78+
next_expires_at = (RedisValue)now.Add(_options.Window).ToUnixTimeMilliseconds(),
79+
current_time = (RedisValue)unixTimeMilliseconds,
8080
permit_limit = (RedisValue)_options.PermitLimit,
8181
increment_amount = (RedisValue)permitCount,
8282
});
@@ -88,7 +88,7 @@ internal async Task<RedisFixedWindowResponse> TryAcquireLeaseAsync(int permitCou
8888
result.Count = (long)response[0];
8989
result.ExpiresAt = (long)response[1];
9090
result.Allowed = (bool)response[2];
91-
result.RetryAfter = TimeSpan.FromSeconds(result.ExpiresAt - nowUnixTimeSeconds);
91+
result.RetryAfter = TimeSpan.FromMilliseconds(result.ExpiresAt - unixTimeMilliseconds);
9292
}
9393

9494
return result;
@@ -97,7 +97,7 @@ internal async Task<RedisFixedWindowResponse> TryAcquireLeaseAsync(int permitCou
9797
internal RedisFixedWindowResponse TryAcquireLease()
9898
{
9999
var now = DateTimeOffset.UtcNow;
100-
var nowUnixTimeSeconds = now.ToUnixTimeSeconds();
100+
var unixTimeMilliseconds = now.ToUnixTimeMilliseconds();
101101

102102
var database = _connectionMultiplexer.GetDatabase();
103103

@@ -107,8 +107,8 @@ internal RedisFixedWindowResponse TryAcquireLease()
107107
{
108108
rate_limit_key = RateLimitKey,
109109
expires_at_key = RateLimitExpireKey,
110-
next_expires_at = (RedisValue)now.Add(_options.Window).ToUnixTimeSeconds(),
111-
current_time = (RedisValue)nowUnixTimeSeconds,
110+
next_expires_at = (RedisValue)now.Add(_options.Window).ToUnixTimeMilliseconds(),
111+
current_time = (RedisValue)unixTimeMilliseconds,
112112
increment_amount = (RedisValue)1D,
113113
});
114114

@@ -118,7 +118,7 @@ internal RedisFixedWindowResponse TryAcquireLease()
118118
{
119119
result.Count = (long)response[0];
120120
result.ExpiresAt = (long)response[1];
121-
result.RetryAfter = TimeSpan.FromSeconds(result.ExpiresAt - nowUnixTimeSeconds);
121+
result.RetryAfter = TimeSpan.FromMilliseconds(result.ExpiresAt - unixTimeMilliseconds);
122122
}
123123

124124
return result;

src/RedisRateLimiting/SlidingWindow/RedisSlidingWindowManager.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal class RedisSlidingWindowManager
2828
redis.call(""zadd"", @rate_limit_key, timestamp, @unique_id)
2929
end
3030
31-
local expireAtMilliseconds = math.floor((timestamp + window) * 1000 + 1);
31+
local expireAtMilliseconds = timestamp + window + 1;
3232
redis.call(""pexpireat"", @rate_limit_key, expireAtMilliseconds)
3333
3434
if allowed
@@ -63,7 +63,7 @@ public RedisSlidingWindowManager(
6363
internal async Task<RedisSlidingWindowResponse> TryAcquireLeaseAsync(string requestId)
6464
{
6565
var now = DateTimeOffset.UtcNow;
66-
double nowUnixTimeSeconds = now.ToUnixTimeMilliseconds() / 1000.0;
66+
var unixTimeMilliseconds = now.ToUnixTimeMilliseconds();
6767

6868
var database = _connectionMultiplexer.GetDatabase();
6969

@@ -74,8 +74,8 @@ internal async Task<RedisSlidingWindowResponse> TryAcquireLeaseAsync(string requ
7474
rate_limit_key = RateLimitKey,
7575
stats_key = StatsRateLimitKey,
7676
permit_limit = (RedisValue)_options.PermitLimit,
77-
window = (RedisValue)_options.Window.TotalSeconds,
78-
current_time = (RedisValue)nowUnixTimeSeconds,
77+
window = (RedisValue)_options.Window.TotalMilliseconds,
78+
current_time = (RedisValue)unixTimeMilliseconds,
7979
unique_id = (RedisValue)requestId,
8080
});
8181

@@ -93,7 +93,7 @@ internal async Task<RedisSlidingWindowResponse> TryAcquireLeaseAsync(string requ
9393
internal RedisSlidingWindowResponse TryAcquireLease(string requestId)
9494
{
9595
var now = DateTimeOffset.UtcNow;
96-
double nowUnixTimeSeconds = now.ToUnixTimeMilliseconds() / 1000.0;
96+
var unixTimeMilliseconds = now.ToUnixTimeMilliseconds();
9797

9898
var database = _connectionMultiplexer.GetDatabase();
9999

@@ -104,8 +104,8 @@ internal RedisSlidingWindowResponse TryAcquireLease(string requestId)
104104
rate_limit_key = RateLimitKey,
105105
stats_key = StatsRateLimitKey,
106106
permit_limit = (RedisValue)_options.PermitLimit,
107-
window = (RedisValue)_options.Window.TotalSeconds,
108-
current_time = (RedisValue)nowUnixTimeSeconds,
107+
window = (RedisValue)_options.Window.TotalMilliseconds,
108+
current_time = (RedisValue)unixTimeMilliseconds,
109109
unique_id = (RedisValue)requestId,
110110
});
111111

0 commit comments

Comments
 (0)