-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed5821c
commit e2c3552
Showing
4 changed files
with
34 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/Starward/Services/Download/TokenBucketRateLimiterExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.RateLimiting; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Starward.Services.Download; | ||
|
||
internal static class TokenBucketRateLimiterExtension | ||
{ | ||
public static int Acquire(this TokenBucketRateLimiter rateLimiter, int permits, out TimeSpan retryAfter) | ||
{ | ||
var a = (int)Volatile.Read(ref PrivateGetTokenCount(rateLimiter)); | ||
int count = Math.Min(permits, a); | ||
return rateLimiter.AttemptAcquire(count).TryGetMetadata(MetadataName.RetryAfter, out retryAfter) ? 0 : count; | ||
} | ||
|
||
// private double _tokenCount; | ||
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_tokenCount")] | ||
private static extern ref double PrivateGetTokenCount(TokenBucketRateLimiter rateLimiter); | ||
} |