Skip to content

Commit 91fe5d4

Browse files
authored
Merge pull request #195 from WeihanLi/patch-1
use Stopwatch.GetElapsedTime directly
2 parents a259704 + ccb4dc6 commit 91fe5d4

File tree

4 files changed

+4
-12
lines changed

4 files changed

+4
-12
lines changed

src/RedisRateLimiting/Concurrency/RedisConcurrencyRateLimiter.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ namespace RedisRateLimiting
1111
{
1212
public class RedisConcurrencyRateLimiter<TKey> : RateLimiter
1313
{
14-
private static readonly double TickFrequency = (double)TimeSpan.TicksPerSecond / Stopwatch.Frequency;
15-
1614
private readonly RedisConcurrencyManager _redisManager;
1715
private readonly RedisConcurrencyRateLimiterOptions _options;
1816
private readonly ConcurrentQueue<Request> _queue = new();
@@ -28,7 +26,7 @@ public class RedisConcurrencyRateLimiter<TKey> : RateLimiter
2826

2927
public override TimeSpan? IdleDuration => Interlocked.CompareExchange(ref _activeRequestsCount, 0, 0) > 0
3028
? null
31-
: new TimeSpan((long)((Stopwatch.GetTimestamp() - _idleSince) * TickFrequency));
29+
: Stopwatch.GetElapsedTime(_idleSince);
3230

3331
public RedisConcurrencyRateLimiter(TKey partitionKey, RedisConcurrencyRateLimiterOptions options)
3432
{

src/RedisRateLimiting/FixedWindow/RedisFixedWindowRateLimiter.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ namespace RedisRateLimiting
1010
{
1111
public class RedisFixedWindowRateLimiter<TKey> : RateLimiter
1212
{
13-
private static readonly double TickFrequency = (double)TimeSpan.TicksPerSecond / Stopwatch.Frequency;
14-
1513
private readonly RedisFixedWindowManager _redisManager;
1614
private readonly RedisFixedWindowRateLimiterOptions _options;
1715

@@ -22,7 +20,7 @@ public class RedisFixedWindowRateLimiter<TKey> : RateLimiter
2220

2321
public override TimeSpan? IdleDuration => Interlocked.CompareExchange(ref _activeRequestsCount, 0, 0) > 0
2422
? null
25-
: new TimeSpan((long)((Stopwatch.GetTimestamp() - _idleSince) * TickFrequency));
23+
: Stopwatch.GetElapsedTime(_idleSince);
2624

2725
public RedisFixedWindowRateLimiter(TKey partitionKey, RedisFixedWindowRateLimiterOptions options)
2826
{

src/RedisRateLimiting/SlidingWindow/RedisSlidingWindowRateLimiter.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ namespace RedisRateLimiting
1010
{
1111
public class RedisSlidingWindowRateLimiter<TKey> : RateLimiter
1212
{
13-
private static readonly double TickFrequency = (double)TimeSpan.TicksPerSecond / Stopwatch.Frequency;
14-
1513
private readonly RedisSlidingWindowManager _redisManager;
1614
private readonly RedisSlidingWindowRateLimiterOptions _options;
1715

@@ -22,7 +20,7 @@ public class RedisSlidingWindowRateLimiter<TKey> : RateLimiter
2220

2321
public override TimeSpan? IdleDuration => Interlocked.CompareExchange(ref _activeRequestsCount, 0, 0) > 0
2422
? null
25-
: new TimeSpan((long)((Stopwatch.GetTimestamp() - _idleSince) * TickFrequency));
23+
: Stopwatch.GetElapsedTime(_idleSince);
2624

2725
public RedisSlidingWindowRateLimiter(TKey partitionKey, RedisSlidingWindowRateLimiterOptions options)
2826
{

src/RedisRateLimiting/TokenBucket/RedisTokenBucketRateLimiter.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ namespace RedisRateLimiting
1010
{
1111
public class RedisTokenBucketRateLimiter<TKey> : RateLimiter
1212
{
13-
private static readonly double TickFrequency = (double)TimeSpan.TicksPerSecond / Stopwatch.Frequency;
14-
1513
private readonly RedisTokenBucketManager _redisManager;
1614
private readonly RedisTokenBucketRateLimiterOptions _options;
1715

@@ -22,7 +20,7 @@ public class RedisTokenBucketRateLimiter<TKey> : RateLimiter
2220

2321
public override TimeSpan? IdleDuration => Interlocked.CompareExchange(ref _activeRequestsCount, 0, 0) > 0
2422
? null
25-
: new TimeSpan((long)((Stopwatch.GetTimestamp() - _idleSince) * TickFrequency));
23+
: Stopwatch.GetElapsedTime(_idleSince);
2624

2725
public RedisTokenBucketRateLimiter(TKey partitionKey, RedisTokenBucketRateLimiterOptions options)
2826
{

0 commit comments

Comments
 (0)