Skip to content

Commit

Permalink
Fix exception when x-ms-retry-after-ms header missing from 429 error -
Browse files Browse the repository at this point in the history
…Fixes #458 (#460)

* Fix issue-458

* Correct changelog
  • Loading branch information
PlagueHO authored Aug 7, 2022
1 parent 0d45516 commit 0ec63ea
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 #458](https://github.com/PlagueHO/CosmosDB/issues/458).

### Changed

Expand Down
15 changes: 14 additions & 1 deletion source/Private/utils/Invoke-CosmosDbRequest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 `
Expand All @@ -257,6 +266,10 @@ function Invoke-CosmosDbRequest
Start-Sleep -Milliseconds $delay
continue
}
else
{
Write-Verbose -Message $($LocalizedData.ErrorTooManyRequests)
}
}

if ($_.Exception.Response)
Expand Down
2 changes: 2 additions & 0 deletions source/en-US/CosmosDB.strings.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -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.
'@

0 comments on commit 0ec63ea

Please sign in to comment.