From 60a501d9bcf360eab2c663bdf6d962742bb8e8e4 Mon Sep 17 00:00:00 2001 From: Mikey O'Toole Date: Mon, 23 Oct 2023 12:03:14 +0100 Subject: [PATCH] Alter retry handling to increase delay after subsequent retries. --- Public/Invoke-HaloRequest.ps1 | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Public/Invoke-HaloRequest.ps1 b/Public/Invoke-HaloRequest.ps1 index 707fd54..f480cc6 100644 --- a/Public/Invoke-HaloRequest.ps1 +++ b/Public/Invoke-HaloRequest.ps1 @@ -44,6 +44,8 @@ function Invoke-HaloRequest { $RequestHeaders = $null } $Retries = 0 + $BaseDelay = 5 # Base delay of 5 seconds + $MaxDelay = 60 # Maximum delay of 60 seconds do { $Retries++ $Results = try { @@ -61,8 +63,9 @@ function Invoke-HaloRequest { } catch [Microsoft.PowerShell.Commands.HttpResponseException] { $Success = $False if ($_.Exception.Response.StatusCode.value__ -eq 429) { - Write-Warning 'The request was throttled, waiting for 5 seconds.' - Start-Sleep -Seconds 5 + $WaitTime = [math]::Min($BaseDelay * [math]::Pow(2, $Retries - 1), $MaxDelay) + Write-Warning "The request was throttled, waiting for $WaitTime seconds." + Start-Sleep -Seconds $WaitTime continue } else { throw $_