|
| 1 | +namespace LrmCloud.Web.Services; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// Coordinates token refresh attempts across different parts of the app |
| 5 | +/// to prevent race conditions when multiple components try to refresh simultaneously. |
| 6 | +/// </summary> |
| 7 | +public class TokenRefreshCoordinator |
| 8 | +{ |
| 9 | + private readonly SemaphoreSlim _refreshLock = new(1, 1); |
| 10 | + private bool _refreshInProgress; |
| 11 | + private DateTime? _lastRefreshAttempt; |
| 12 | + |
| 13 | + /// <summary> |
| 14 | + /// Minimum time between refresh attempts to prevent rapid-fire refreshes. |
| 15 | + /// </summary> |
| 16 | + private static readonly TimeSpan MinRefreshInterval = TimeSpan.FromSeconds(5); |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Try to acquire the refresh lock. Returns true if this caller should perform the refresh. |
| 20 | + /// Returns false if another refresh is in progress or was recently attempted. |
| 21 | + /// </summary> |
| 22 | + public async Task<bool> TryAcquireRefreshLockAsync(CancellationToken cancellationToken = default) |
| 23 | + { |
| 24 | + // Quick check before waiting for lock |
| 25 | + if (_refreshInProgress) |
| 26 | + return false; |
| 27 | + |
| 28 | + // Check if refresh was recently attempted (even if it failed) |
| 29 | + if (_lastRefreshAttempt.HasValue && |
| 30 | + DateTime.UtcNow - _lastRefreshAttempt.Value < MinRefreshInterval) |
| 31 | + return false; |
| 32 | + |
| 33 | + // Try to acquire the lock with a short timeout |
| 34 | + if (!await _refreshLock.WaitAsync(TimeSpan.FromMilliseconds(100), cancellationToken)) |
| 35 | + return false; |
| 36 | + |
| 37 | + // Double-check after acquiring lock |
| 38 | + if (_refreshInProgress) |
| 39 | + { |
| 40 | + _refreshLock.Release(); |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + _refreshInProgress = true; |
| 45 | + _lastRefreshAttempt = DateTime.UtcNow; |
| 46 | + return true; |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Release the refresh lock after refresh attempt completes. |
| 51 | + /// </summary> |
| 52 | + public void ReleaseRefreshLock() |
| 53 | + { |
| 54 | + _refreshInProgress = false; |
| 55 | + try |
| 56 | + { |
| 57 | + _refreshLock.Release(); |
| 58 | + } |
| 59 | + catch (SemaphoreFullException) |
| 60 | + { |
| 61 | + // Already released, ignore |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Check if a refresh is currently in progress. |
| 67 | + /// </summary> |
| 68 | + public bool IsRefreshInProgress => _refreshInProgress; |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Wait for any ongoing refresh to complete. |
| 72 | + /// Returns true if we waited, false if no refresh was in progress. |
| 73 | + /// </summary> |
| 74 | + public async Task<bool> WaitForRefreshAsync(TimeSpan timeout, CancellationToken cancellationToken = default) |
| 75 | + { |
| 76 | + if (!_refreshInProgress) |
| 77 | + return false; |
| 78 | + |
| 79 | + var startTime = DateTime.UtcNow; |
| 80 | + while (_refreshInProgress && DateTime.UtcNow - startTime < timeout) |
| 81 | + { |
| 82 | + await Task.Delay(50, cancellationToken); |
| 83 | + } |
| 84 | + return true; |
| 85 | + } |
| 86 | +} |
0 commit comments