|
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 |
|
12 | 13 | namespace StackExchange.Redis |
@@ -328,5 +329,67 @@ internal static int VectorSafeIndexOfCRLF(this ReadOnlySpan<byte> span) |
328 | 329 | return -1; |
329 | 330 | } |
330 | 331 | #endif |
| 332 | + |
| 333 | + /// <summary> |
| 334 | + /// Attempts to acquire a GCRA rate limit token, retrying with delays if rate limited. |
| 335 | + /// </summary> |
| 336 | + /// <param name="database">The database instance.</param> |
| 337 | + /// <param name="key">The key for the rate limiter.</param> |
| 338 | + /// <param name="maxBurst">The maximum burst size.</param> |
| 339 | + /// <param name="requestsPerPeriod">The number of requests allowed per period.</param> |
| 340 | + /// <param name="allow">The maximum time to wait for a successful acquisition.</param> |
| 341 | + /// <param name="periodSeconds">The period in seconds (default: 1.0).</param> |
| 342 | + /// <param name="count">The number of tokens to acquire (default: 1).</param> |
| 343 | + /// <param name="flags">The command flags to use.</param> |
| 344 | + /// <param name="cancellationToken">The cancellation token.</param> |
| 345 | + /// <returns>True if the token was acquired within the allowed time; false otherwise.</returns> |
| 346 | + public static async ValueTask<bool> TryAcquireGcraAsync( |
| 347 | + this IDatabaseAsync database, |
| 348 | + RedisKey key, |
| 349 | + int maxBurst, |
| 350 | + int requestsPerPeriod, |
| 351 | + TimeSpan allow, |
| 352 | + double periodSeconds = 1.0, |
| 353 | + int count = 1, |
| 354 | + CommandFlags flags = CommandFlags.None, |
| 355 | + CancellationToken cancellationToken = default) |
| 356 | + { |
| 357 | + var startTime = DateTime.UtcNow; |
| 358 | + var allowMilliseconds = allow.TotalMilliseconds; |
| 359 | + |
| 360 | + while (true) |
| 361 | + { |
| 362 | + var result = await database.StringGcraRateLimitAsync(key, maxBurst, requestsPerPeriod, periodSeconds, count, flags).ConfigureAwait(false); |
| 363 | + |
| 364 | + if (!result.Limited) |
| 365 | + { |
| 366 | + return true; |
| 367 | + } |
| 368 | + |
| 369 | + var elapsed = (DateTime.UtcNow - startTime).TotalMilliseconds; |
| 370 | + var remaining = allowMilliseconds - elapsed; |
| 371 | + |
| 372 | + if (remaining <= 0) |
| 373 | + { |
| 374 | + return false; |
| 375 | + } |
| 376 | + |
| 377 | + var delaySeconds = result.RetryAfterSeconds; |
| 378 | + if (delaySeconds <= 0) |
| 379 | + { |
| 380 | + // Shouldn't happen when Limited is true, but handle defensively |
| 381 | + return false; |
| 382 | + } |
| 383 | + |
| 384 | + var delayMilliseconds = delaySeconds * 1000.0; |
| 385 | + if (delayMilliseconds > remaining) |
| 386 | + { |
| 387 | + // Not enough time left to wait for retry |
| 388 | + return false; |
| 389 | + } |
| 390 | + |
| 391 | + await Task.Delay(TimeSpan.FromSeconds(delaySeconds), cancellationToken).ConfigureAwait(false); |
| 392 | + } |
| 393 | + } |
331 | 394 | } |
332 | 395 | } |
0 commit comments