|
7 | 7 | using System.Security.Authentication; |
8 | 8 | using System.Security.Cryptography.X509Certificates; |
9 | 9 | using System.Text; |
| 10 | +using System.Threading; |
10 | 11 | using System.Threading.Tasks; |
11 | 12 | using Pipelines.Sockets.Unofficial.Arenas; |
| 13 | +using RESPite; |
12 | 14 |
|
13 | 15 | namespace StackExchange.Redis |
14 | 16 | { |
@@ -337,5 +339,70 @@ internal static int VectorSafeIndexOfCRLF(this ReadOnlySpan<byte> span) |
337 | 339 | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
338 | 340 | internal static TTo[]? ToArray<TTo, TState>(in this RawResult result, Projection<RawResult, TState, TTo> selector, in TState state) |
339 | 341 | => result.IsNull ? null : result.GetItems().ToArray(selector, in state); |
| 342 | + |
| 343 | + /// <summary> |
| 344 | + /// Attempts to acquire a GCRA rate limit token, retrying with delays if rate limited. |
| 345 | + /// </summary> |
| 346 | + /// <param name="database">The database instance.</param> |
| 347 | + /// <param name="key">The key for the rate limiter.</param> |
| 348 | + /// <param name="maxBurst">The maximum burst size.</param> |
| 349 | + /// <param name="requestsPerPeriod">The number of requests allowed per period.</param> |
| 350 | + /// <param name="allow">The maximum time to wait for a successful acquisition.</param> |
| 351 | + /// <param name="periodSeconds">The period in seconds (default: 1.0).</param> |
| 352 | + /// <param name="count">The number of tokens to acquire (default: 1).</param> |
| 353 | + /// <param name="flags">The command flags to use.</param> |
| 354 | + /// <param name="cancellationToken">The cancellation token.</param> |
| 355 | + /// <returns>True if the token was acquired within the allowed time; false otherwise.</returns> |
| 356 | + [Experimental(Experiments.Server_8_8, UrlFormat = Experiments.UrlFormat)] |
| 357 | + public static async ValueTask<bool> TryAcquireGcraAsync( |
| 358 | + this IDatabaseAsync database, |
| 359 | + RedisKey key, |
| 360 | + int maxBurst, |
| 361 | + int requestsPerPeriod, |
| 362 | + TimeSpan allow, |
| 363 | + double periodSeconds = 1.0, |
| 364 | + int count = 1, |
| 365 | + CommandFlags flags = CommandFlags.None, |
| 366 | + CancellationToken cancellationToken = default) |
| 367 | + { |
| 368 | + cancellationToken.ThrowIfCancellationRequested(); |
| 369 | + |
| 370 | + var startTime = DateTime.UtcNow; |
| 371 | + var allowMilliseconds = allow.TotalMilliseconds; |
| 372 | + |
| 373 | + while (true) |
| 374 | + { |
| 375 | + var result = await database.StringGcraRateLimitAsync(key, maxBurst, requestsPerPeriod, periodSeconds, count, flags).ConfigureAwait(false); |
| 376 | + |
| 377 | + if (!result.Limited) |
| 378 | + { |
| 379 | + return true; |
| 380 | + } |
| 381 | + |
| 382 | + var elapsed = (DateTime.UtcNow - startTime).TotalMilliseconds; |
| 383 | + var remaining = allowMilliseconds - elapsed; |
| 384 | + |
| 385 | + if (remaining <= 0) |
| 386 | + { |
| 387 | + return false; |
| 388 | + } |
| 389 | + |
| 390 | + var delaySeconds = result.RetryAfterSeconds; |
| 391 | + if (delaySeconds <= 0) |
| 392 | + { |
| 393 | + // Shouldn't happen when Limited is true, but handle defensively |
| 394 | + return false; |
| 395 | + } |
| 396 | + |
| 397 | + var delayMilliseconds = delaySeconds * 1000.0; |
| 398 | + if (delayMilliseconds > remaining) |
| 399 | + { |
| 400 | + // Not enough time left to wait for retry |
| 401 | + return false; |
| 402 | + } |
| 403 | + |
| 404 | + await Task.Delay(TimeSpan.FromSeconds(delaySeconds), cancellationToken).ConfigureAwait(false); |
| 405 | + } |
| 406 | + } |
340 | 407 | } |
341 | 408 | } |
0 commit comments