From 39fa05bb41e7e20762e0ea08cceb2f4f445cbf83 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Mon, 26 Oct 2026 15:26:42 +0100 Subject: [PATCH 1/2] Implement rate-limiting antimeasures --- .../API/Invoke-ConfluenceRestMethod.ps1 | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/functions/public/API/Invoke-ConfluenceRestMethod.ps1 b/src/functions/public/API/Invoke-ConfluenceRestMethod.ps1 index 05bf709..2e520a0 100644 --- a/src/functions/public/API/Invoke-ConfluenceRestMethod.ps1 +++ b/src/functions/public/API/Invoke-ConfluenceRestMethod.ps1 @@ -167,6 +167,29 @@ function Invoke-ConfluenceRestMethod { } } + + # Requests are near rate limit or have surpassed the rate limit. + # Atlassian documents several response headers, https://developer.atlassian.com/cloud/confluence/rate-limiting/#rate-limit-related-headers + if ([Convert]::ToBoolean($responseHeaders['X-RateLimit-NearLimit'])) { + # Less than 20% quota remains, wait 500ms pre-emptively + Write-Debug 'Requests are close to rate-limiting. Sleeping for 500ms.' + Start-Sleep -Milliseconds 500 + } + + if ($statusCode -eq 429) { + Write-Debug "Requests have been throttled. Sleeping for $($responseHeaders['Retry-After']) seconds" + + # During testing, this header was always returned as an array + if ($responseHeaders['Retry-After'] -is [array]) { + Start-Sleep -Seconds ($responseHeaders['Retry-After'] | Select-Object -First 1) + } else { + Start-Sleep -Seconds $responseHeaders['Retry-After'] + } + + # Retry throttled request + $response = Invoke-RestMethod @request -Debug:$false + } + if ($statusCode -ge 400) { $detail = if ($null -eq $response) { '' From e860192f3f66c12f733a5daad89b735dd98bee1d Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Mon, 26 Oct 2026 16:02:49 +0100 Subject: [PATCH 2/2] Potential fix for pull request finding 'Handle X-RateLimit-NearLimit header string arrays correctly' Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- src/functions/public/API/Invoke-ConfluenceRestMethod.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/functions/public/API/Invoke-ConfluenceRestMethod.ps1 b/src/functions/public/API/Invoke-ConfluenceRestMethod.ps1 index 2e520a0..3b70ef6 100644 --- a/src/functions/public/API/Invoke-ConfluenceRestMethod.ps1 +++ b/src/functions/public/API/Invoke-ConfluenceRestMethod.ps1 @@ -170,7 +170,7 @@ function Invoke-ConfluenceRestMethod { # Requests are near rate limit or have surpassed the rate limit. # Atlassian documents several response headers, https://developer.atlassian.com/cloud/confluence/rate-limiting/#rate-limit-related-headers - if ([Convert]::ToBoolean($responseHeaders['X-RateLimit-NearLimit'])) { + if (($responseHeaders['X-RateLimit-NearLimit'] | Select-Object -First 1) -eq 'true') { # Less than 20% quota remains, wait 500ms pre-emptively Write-Debug 'Requests are close to rate-limiting. Sleeping for 500ms.' Start-Sleep -Milliseconds 500