-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Fix HTTP status from 409 to 429 for WebCmdlets to get retry interval from Retry-After header. #19622
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
Conversation
|
This is my first PR for the PowerShell repository, so please let me know if there are any problems. |
|
Great catch, thank you for your contribution ! |
| // If the status code is 429 get the retry interval from the Headers. | ||
| // Ignore broken header and its value. | ||
| if (response.StatusCode is HttpStatusCode.Conflict && response.Headers.TryGetValues(HttpKnownHeaderNames.RetryAfter, out IEnumerable<string> retryAfter)) | ||
| if (response.StatusCode is HttpStatusCode.TooManyRequests && response.Headers.TryGetValues(HttpKnownHeaderNames.RetryAfter, out IEnumerable<string> retryAfter)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great to cover this by a test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@iSazonov I can look into it
|
Oops, I just added test code, not noticing that @CarloToso had already added in another PR. Thanks @CarloToso . And sorry I didn't notice your comment. 😥 |
| $jsonResult.SessionId | Should -BeExactly $sessionId | ||
| } | ||
|
|
||
| It "Invoke-WebRequest respects the Retry-After header value in 429 status" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you confirm the tests fail before the change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've already tested it. Before the change (current master branch) these tests failed.
Executing script F:\PowerShell\test\powershell\Modules\Microsoft.PowerShell.Utility\WebCmdlets.Tests.ps1
Describing Invoke-WebRequest tests
WARNING: This module is use-at-your-own-risk.
It exists to test PowerShell Core builds and is not supported by Microsoft.
Please report any issues at https://github.com/rjmholt/SelfSignedCertificate.
Certificate written to C:\Users\mkht\AppData\Local\Temp\ServerCert.pfx
WARNING: Parameter 'EmailAddress' is obsolete. The email name component is deprecated by the PKIX standard
Certificate written to C:\Users\mkht\AppData\Local\Temp\ClientCert.pfx
Context Invoke-WebRequest retry tests
[+] Invoke-WebRequest can retry - specified number of times - error 304 2.15s
[+] Invoke-WebRequest can retry - specified number of times - error 400 3.07s
[+] Invoke-WebRequest can retry - specified number of times - error 599 1.02s
[+] Invoke-WebRequest can retry - specified number of times - error 404 2.04s
[+] Invoke-WebRequest can retry - when retry count is higher than failure count 2.03s
[+] Invoke-WebRequest should fail when failureCount is greater than MaximumRetryCount 1.04s
[+] Invoke-WebRequest can retry with POST 1.02s
[-] Invoke-WebRequest respects the Retry-After header value in 429 status 6.05s
Expected the actual value to be less than 2.9, but got 6.0393409.
2179: $stopWatch.Elapsed.TotalSeconds | Should -BeLessThan 2.9
at <ScriptBlock>, F:\PowerShell\test\powershell\Modules\Microsoft.PowerShell.Utility\WebCmdlets.Tests.ps1: line 2179
[-] Invoke-WebRequest ignores the Retry-After header value NOT in 429 status 8.05s
Expected the actual value to be less than 2.9, but got 8.0427231.
2200: $stopWatch.Elapsed.TotalSeconds | Should -BeLessThan 2.9
at <ScriptBlock>, F:\PowerShell\test\powershell\Modules\Microsoft.PowerShell.Utility\WebCmdlets.Tests.ps1: line 2200
Describing Invoke-RestMethod tests
Context Invoke-RestMethod retry tests
[+] Invoke-RestMethod can retry - specified number of times - error 304 2.04s
[+] Invoke-RestMethod can retry with POST 1.02s
[-] Invoke-RestMethod respects the Retry-After header value in 429 status 6.03s
Expected the actual value to be less than 2.9, but got 6.0209983.
4111: $stopWatch.Elapsed.TotalSeconds | Should -BeLessThan 2.9
at <ScriptBlock>, F:\PowerShell\test\powershell\Modules\Microsoft.PowerShell.Utility\WebCmdlets.Tests.ps1: line 4111
[-] Invoke-RestMethod ignores the Retry-After header value NOT in 429 status 8.05s
Expected the actual value to be less than 2.9, but got 8.0350285.
4132: $stopWatch.Elapsed.TotalSeconds | Should -BeLessThan 2.9
at <ScriptBlock>, F:\PowerShell\test\powershell\Modules\Microsoft.PowerShell.Utility\WebCmdlets.Tests.ps1: line 4132
Tests completed in 128.49s
Tests Passed: 557, Failed: 4, Skipped: 4, Pending: 34, Inconclusive: 0 |
@mkht Don't revert your changes, I'll close my PR. I think you should use the -Verbose redirect method from my PR instead of using |
|
This PR has Quantification details
Why proper sizing of changes matters
Optimal pull request sizes drive a better predictable PR flow as they strike a
What can I do to optimize my changes
How to interpret the change counts in git diff output
Was this comment helpful? 👍 :ok_hand: :thumbsdown: (Email) |
|
I followed @CarloToso advice and changed the method of judging the test to using verbose messages. I confirmed that the test failed with the pre-modified code and succeeded with the modified code. |
daxian-dbw
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @mkht for the contribution!
|
🎉 Handy links: |
PR Summary
The code that gets the appropriate retry interval from the Retry-After response header is incorrect and is executing with status code 409 when it should be executing with status code 429. This PR will correct that.
The original code was implemented in PR #18717.
This PR will fix #19621 and also correctly resolves #13188 again.
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header