From 7f47f6751f944c4c52010776f312f86370422a3d Mon Sep 17 00:00:00 2001 From: Daniel Scott-Raynsford Date: Mon, 18 Jul 2022 21:40:12 +1200 Subject: [PATCH 1/2] Fix issue-458 --- CHANGELOG.md | 3 +++ source/Private/utils/Invoke-CosmosDbRequest.ps1 | 15 ++++++++++++++- source/en-US/CosmosDB.strings.psd1 | 2 ++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bbd6f083..96be9cc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fix Azure DevOps build pipeline and update to latest sampler pattern. +- Fix exception being thrown when a 429 is returned by CosmosDB, but the + `x-ms-retry-after-ms` header is not returned. This may occur in requests + that follow large (> 1MB) insert or updates - Fixes [Issue #439](https://github.com/PlagueHO/CosmosDB/issues/439). ### Changed diff --git a/source/Private/utils/Invoke-CosmosDbRequest.ps1 b/source/Private/utils/Invoke-CosmosDbRequest.ps1 index a70a4464..62aa7040 100644 --- a/source/Private/utils/Invoke-CosmosDbRequest.ps1 +++ b/source/Private/utils/Invoke-CosmosDbRequest.ps1 @@ -242,7 +242,16 @@ function Invoke-CosmosDbRequest The exception was caused by exceeding provisioned throughput so determine is we should delay and try again or exit #> - [System.Int32] $retryAfter = ($_.Exception.Response.Headers | Where-Object -Property Key -eq 'x-ms-retry-after-ms').Value[0] + $retryAfterHeader = $_.Exception.Response.Headers | Where-Object -Property Key -eq 'x-ms-retry-after-ms' + if ($retryAfterHeader) + { + [System.Int32] $retryAfter = $retryAfterHeader.Value[0] + } + else + { + [System.Int32] $retryAfter = 0 + Write-Verbose -Message $($LocalizedData.ErrorTooManyRequestsWithNoRetryAfter) + } $delay = Get-CosmosDbBackoffDelay ` -BackOffPolicy $Context.BackoffPolicy ` @@ -257,6 +266,10 @@ function Invoke-CosmosDbRequest Start-Sleep -Milliseconds $delay continue } + else + { + Write-Verbose -Message $($LocalizedData.ErrorTooManyRequests) + } } if ($_.Exception.Response) diff --git a/source/en-US/CosmosDB.strings.psd1 b/source/en-US/CosmosDB.strings.psd1 index cb9ba4a0..92e96299 100644 --- a/source/en-US/CosmosDB.strings.psd1 +++ b/source/en-US/CosmosDB.strings.psd1 @@ -61,4 +61,6 @@ ConvertFrom-StringData -StringData @' ErrorNewDatabaseThroughputParameterConflict = Both 'OfferThroughput' and 'AutoscaleThroughput' should not be specified when creating a new database. DeprecateAttachmentWarning = Attachments are a legacy feature. Their support is scoped to offer continued functionality if you are already using this feature. See https://aka.ms/cosmosdb-attachments for more information. ErrorConvertingDocumentJsonToObject = An error occured converting the document information returned from Cosmsos DB into an object. This might be caused by the document including keys with same name but differing in case. Include the -ReturnJson parameter to return these as JSON instead. + ErrorTooManyRequests = The server returned a '429 Too Many Requests' error. This is likely due to the client making too many requests to the server. Please retry your request. + ErrorTooManyRequestsWithNoRetryAfter = The server returned a '429 Too Many Requests' error, but the did not include an 'x-ms-retry-after-ms' header in the response. A retry delay of 0ms will be used. '@ From c34c79ba803a0f0c4ff271ea59ab855ef6d36dd7 Mon Sep 17 00:00:00 2001 From: Daniel Scott-Raynsford Date: Tue, 19 Jul 2022 16:22:54 +1200 Subject: [PATCH 2/2] Correct changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 96be9cc0..11c3e8aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix Azure DevOps build pipeline and update to latest sampler pattern. - Fix exception being thrown when a 429 is returned by CosmosDB, but the `x-ms-retry-after-ms` header is not returned. This may occur in requests - that follow large (> 1MB) insert or updates - Fixes [Issue #439](https://github.com/PlagueHO/CosmosDB/issues/439). + that follow large (> 1MB) insert or updates - Fixes [Issue #458](https://github.com/PlagueHO/CosmosDB/issues/458). ### Changed