@@ -17,14 +17,7 @@ internal class RedisTokenBucketManager
17
17
local rate = tonumber(@tokens_per_period)
18
18
local period = tonumber(@replenish_period)
19
19
local requested = tonumber(@permit_count)
20
-
21
- -- Even though it is the default since Redis 5, we explicitly enable command replication.
22
- -- This ensures that non-deterministic commands like 'TIME' are replicated by effect.
23
- redis.replicate_commands()
24
-
25
- -- Retrieve the current time as unix timestamp with millisecond accuracy.
26
- local time = redis.call('TIME')
27
- local now = math.floor((time[1] * 1000) + (time[2] / 1000))
20
+ local now = tonumber(@current_time)
28
21
29
22
-- Load the current state from Redis. We use MGET to save a round-trip.
30
23
local state = redis.call('MGET', @rate_limit_key, @timestamp_key)
@@ -97,6 +90,7 @@ internal async Task<RedisTokenBucketResponse> TryAcquireLeaseAsync()
97
90
token_limit = ( RedisValue ) _options . TokenLimit ,
98
91
replenish_period = ( RedisValue ) _options . ReplenishmentPeriod . TotalMilliseconds ,
99
92
permit_count = ( RedisValue ) 1D ,
93
+ current_time = ( RedisValue ) DateTimeOffset . UtcNow . ToUnixTimeMilliseconds ( ) ,
100
94
} ) ;
101
95
102
96
var result = new RedisTokenBucketResponse ( ) ;
@@ -125,6 +119,7 @@ internal RedisTokenBucketResponse TryAcquireLease()
125
119
token_limit = ( RedisValue ) _options . TokenLimit ,
126
120
replenish_period = ( RedisValue ) _options . ReplenishmentPeriod . TotalMilliseconds ,
127
121
permit_count = ( RedisValue ) 1D ,
122
+ current_time = ( RedisValue ) DateTimeOffset . UtcNow . ToUnixTimeMilliseconds ( ) ,
128
123
} ) ;
129
124
130
125
var result = new RedisTokenBucketResponse ( ) ;
@@ -146,4 +141,4 @@ internal class RedisTokenBucketResponse
146
141
internal long Count { get ; set ; }
147
142
internal int RetryAfter { get ; set ; }
148
143
}
149
- }
144
+ }
0 commit comments