@@ -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 ;
0 commit comments