1
1
using System ;
2
- using System . Runtime ;
2
+ using System . Runtime . CompilerServices ;
3
3
using System . Threading ;
4
4
using System . Threading . Tasks ;
5
5
6
+ [ assembly: InternalsVisibleTo ( "Tests" ) ]
6
7
namespace FikaAmazonAPI . Utils
7
8
{
8
9
internal class RateLimits
@@ -47,10 +48,9 @@ public async Task WaitForPermittedRequest(CancellationToken cancellationToken =
47
48
}
48
49
49
50
int ratePeriodMs = GetRatePeriodMs ( ) ;
50
- var requestsUsedAtOnset = RequestsUsed ;
51
51
52
52
// when requests used more than zero, replenish according to time elapsed
53
- IncrementAvailableTokens ( debugMode ) ;
53
+ DecrementRequestsUsed ( debugMode ) ;
54
54
55
55
var nextRequestsSent = RequestsUsed + 1 ;
56
56
var nextRequestsSentTxt = ( nextRequestsSent > Burst ) ? "FULL" : nextRequestsSent . ToString ( ) ;
@@ -76,7 +76,7 @@ public async Task WaitForPermittedRequest(CancellationToken cancellationToken =
76
76
else
77
77
{
78
78
// replenish token
79
- IncrementAvailableTokens ( debugMode ) ;
79
+ DecrementRequestsUsed ( debugMode ) ;
80
80
}
81
81
82
82
if ( RequestsUsed <= 0 )
@@ -87,7 +87,7 @@ public async Task WaitForPermittedRequest(CancellationToken cancellationToken =
87
87
}
88
88
89
89
// now remove token from bucket for pending request
90
- requestIsPermitted = TryDecrementAvailableTokens ( debugMode ) ;
90
+ requestIsPermitted = TryIncrementRequestsUsed ( debugMode ) ;
91
91
}
92
92
}
93
93
@@ -97,13 +97,13 @@ internal void SetRateLimit(decimal rate)
97
97
}
98
98
99
99
// increments available tokens, unless another thread has already incremented them.
100
- private void IncrementAvailableTokens ( bool isDebug )
100
+ private void DecrementRequestsUsed ( bool isDebug )
101
101
{
102
102
WriteDebug ( $ "Attempting to increment tokens", isDebug ) ;
103
103
lock ( _locker )
104
104
{
105
105
var ratePeriodMilliseconds = GetRatePeriodMs ( ) ;
106
- var requestsToReplenish = ratePeriodMilliseconds == 0 ? 0 : ( DateTime . UtcNow - LastRequestReplenished ) . Milliseconds / ratePeriodMilliseconds ;
106
+ var requestsToReplenish = ratePeriodMilliseconds == 0 ? 0 : ( int ) ( ( DateTime . UtcNow - LastRequestReplenished ) . TotalMilliseconds / ratePeriodMilliseconds ) ;
107
107
WriteDebug ( $ "{ requestsToReplenish } tokens to replenish since { LastRequestReplenished } ", isDebug ) ;
108
108
if ( requestsToReplenish == 0 || RequestsUsed == 0 )
109
109
{
@@ -118,7 +118,7 @@ private void IncrementAvailableTokens(bool isDebug)
118
118
}
119
119
120
120
// will try to decrement available tokens, will fail if another thread has used last of burst quota
121
- private bool TryDecrementAvailableTokens ( bool isDebug )
121
+ private bool TryIncrementRequestsUsed ( bool isDebug )
122
122
{
123
123
var succeeded = false ;
124
124
0 commit comments