Skip to content

Conversation

@mkht
Copy link
Contributor

@mkht mkht commented May 6, 2023

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

@mkht
Copy link
Contributor Author

mkht commented May 6, 2023

This is my first PR for the PowerShell repository, so please let me know if there are any problems.

@CarloToso
Copy link
Contributor

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))
Copy link
Collaborator

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.

Copy link
Contributor

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

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label May 7, 2023
@CarloToso CarloToso mentioned this pull request May 8, 2023
22 tasks
@mkht
Copy link
Contributor Author

mkht commented May 8, 2023

Oops, I just added test code, not noticing that @CarloToso had already added in another PR.
Should I revert my test code? I will follow the moderator's instructions.

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" {
Copy link
Collaborator

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?

Copy link
Contributor Author

@mkht mkht May 8, 2023

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 

@CarloToso
Copy link
Contributor

CarloToso commented May 8, 2023

@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 System.Diagnostics.Stopwatch. Sometime the tests take more time than intended and so they could fail randomly

@pull-request-quantifier-deprecated

This PR has 46 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Extra Small
Size       : +45 -1
Percentile : 18.4%

Total files changed: 2

Change summary by file extension:
.cs : +1 -1
.ps1 : +44 -0

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@mkht
Copy link
Contributor Author

mkht commented May 8, 2023

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.

Copy link
Member

@daxian-dbw daxian-dbw left a 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!

@daxian-dbw daxian-dbw merged commit 9502931 into PowerShell:master May 8, 2023
@mkht mkht deleted the fix-http429-retryafter branch May 9, 2023 13:37
@ghost
Copy link

ghost commented Jun 29, 2023

🎉v7.4.0-preview.4 has been released which incorporates this pull request.:tada:

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Extra Small

Projects

None yet

5 participants