Private/Wait-PrismRateLimit.ps1
|
function Wait-PrismRateLimit { <# .SYNOPSIS Waits before retrying a Prism Central HTTP 429 response. .DESCRIPTION Honors Retry-After (seconds or an HTTP date) when supplied. Otherwise uses X-Api-Ratelimit-Refresh-Period-Seconds, with exponential backoff and jitter. The five retry attempts use backoff delays of 1, 2, 4, 8 and 16 seconds, before jitter. Server timing is a minimum, not a cap on the backoff. Reset headers are deliberately not interpreted: their units and semantics have not been verified. Returns false when the server requests a wait longer than sixty seconds; callers should surface the original HTTP error rather than retry early. A shared deadline includes this wait. Throws TimeoutException if the delay would exhaust the budget; it never shortens the server's required wait. #> [CmdletBinding()] [OutputType([bool])] param( [Parameter(Mandatory)]$Headers, [Parameter(Mandatory)][ValidateRange(1, 5)][int]$RetryAttempt, [Parameter()][Nullable[DateTimeOffset]]$Deadline, # The HTTP 429 being retried; attached to any TimeoutException as its inner exception. [Parameter()][Exception]$InnerException ) # HttpResponseHeaders and response-header dictionaries both enumerate Key/Value # pairs. Values can be strings or collections; preserve HTTP dates containing commas. $timing = @{} foreach ($entry in $Headers.GetEnumerator()) { if ($entry.Key -in @('Retry-After', 'X-Api-Ratelimit-Refresh-Period-Seconds')) { $timing[$entry.Key] = [string](@($entry.Value)[0]) } } $serverSeconds = 0.0 $seconds = 0.0 $culture = [Globalization.CultureInfo]::InvariantCulture $retryAfter = $timing['Retry-After'] if ($retryAfter -match '^\d+$' -and [double]::TryParse($retryAfter, [Globalization.NumberStyles]::None, $culture, [ref]$seconds)) { $serverSeconds = $seconds } else { $date = [DateTimeOffset]::MinValue if ([DateTimeOffset]::TryParseExact($retryAfter, 'r', $culture, [Globalization.DateTimeStyles]::AssumeUniversal, [ref]$date)) { $serverSeconds = [math]::Max(0.0, ($date - [DateTimeOffset]::UtcNow).TotalSeconds) } } if ($serverSeconds -le 0) { $refreshPeriod = $timing['X-Api-Ratelimit-Refresh-Period-Seconds'] if ($refreshPeriod -match '^\d+(\.\d+)?$' -and [double]::TryParse($refreshPeriod, [Globalization.NumberStyles]::AllowDecimalPoint, $culture, [ref]$seconds)) { $serverSeconds = $seconds } } if ($serverSeconds -gt 60) { Write-Verbose 'Prism rate-limit wait exceeds sixty seconds; surfacing the original HTTP 429.' return $false } $backoffSeconds = [math]::Pow(2, $RetryAttempt - 1) $milliseconds = [int][math]::Ceiling([math]::Max($serverSeconds, $backoffSeconds) * 1000) # Keep the complete wait within sixty seconds, including jitter. $milliseconds = [math]::Min(60000, $milliseconds + (Get-Random -Minimum 0 -Maximum 251)) if ($null -ne $Deadline -and $milliseconds -ge ($Deadline - [DateTimeOffset]::UtcNow).TotalMilliseconds) { $message = "The Prism rate-limit delay of $milliseconds ms would exhaust the request deadline." if ($null -ne $InnerException) { throw [TimeoutException]::new("$message Last error: $($InnerException.Message)", $InnerException) } throw [TimeoutException]::new($message) } Write-Verbose "Prism HTTP 429: retry $RetryAttempt of 5 after $milliseconds ms." Start-Sleep -Milliseconds $milliseconds return $true } |