Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exception when x-ms-retry-after-ms header missing from 429 error - Fixes #458 #460

Merged
merged 2 commits into from
Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
'@