-
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.
* refactor: download limiter * opt: Decrease the time interval for replenishing tokens * fix: crash by NumberBox possibly being -21e * Improve more precise speed limits Co-authored-by: DismissedLight <[email protected]> * opt: reduce downloader writes * refactor: simplify rate limiting --------- Co-authored-by: DismissedLight <[email protected]> Co-authored-by: Scighost <[email protected]>
- Loading branch information
1 parent
0731321
commit 93c028d
Showing
6 changed files
with
106 additions
and
50 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
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
24 changes: 24 additions & 0 deletions
24
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,24 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.RateLimiting; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Starward.Services.Download; | ||
|
||
internal static class TokenBucketRateLimiterExtension | ||
{ | ||
public static bool TryAcquire(this TokenBucketRateLimiter rateLimiter, int permits, out int acquired, out TimeSpan retryAfter) | ||
{ | ||
acquired = Math.Min(permits, (int)Volatile.Read(ref PrivateGetTokenCount(rateLimiter))); | ||
lock (PrivateGetLock(rateLimiter)) | ||
return !rateLimiter.AttemptAcquire(acquired).TryGetMetadata(MetadataName.RetryAfter, out retryAfter); | ||
} | ||
|
||
// private object Lock → _queue | ||
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "get_Lock")] | ||
private static extern object PrivateGetLock(TokenBucketRateLimiter rateLimiter); | ||
|
||
// private double _tokenCount; | ||
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_tokenCount")] | ||
private static extern ref double PrivateGetTokenCount(TokenBucketRateLimiter rateLimiter); | ||
} |