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

Enable HTTP persistence when using Session with Invoke-WebRequest and Invoke-RestMethod #19173

Conversation

stevenebutler
Copy link
Contributor

@stevenebutler stevenebutler commented Feb 18, 2023

PR Summary

This PR addresses 12764 by using persistent HTTP connections when Invoke-WebRequest uses the -Session variable. It does this by

  • tracking the HttpClient object in the saved WebRequestSession
  • monitoring properties in WebRequestSession and rebuilding the HttpClient if they are changed. This is done to ensure that existing scripts that might tweak parameters between invocations of IWR actually apply the changes so they will behave in a backwards compatible manner. When anything that impacts the HttpClient is changed, the persistent connection is abandoned by disposing the HttpClient and underlying HttpClientHandler and constructing a new one. A warning is logged when this occurs as it has a performance impact (though no worse than the existing behaviour).
  • To support the point 2 above, many switches were added as internal set-only properties to the WebRequestSession so changes between invocations can be detected and the handler rebuilt appropriately.

PR Context

This PR makes a considerable difference to performance when using Invoke-WebRequest or Invoke-RestMethod with sessions when multiple requests are made to the same host. This is particularly evident for HTTPS requests, as each HTTPS connection can take hundreds of milliseconds to establish.

This PR as written passes existing tests in WebCmdlets.Tests.ps1 and additional WebSession tests that verify the HttpClient is rebuilt when appropriate. This PR is submitted as WIP for feedback following advice on #12764

The PR is attempting to provide as much backwards compatibility as possible at the expense of the complexity of tracking how the HttpClient used in the WebSession was constructed and recreating the HttpClient in the case it would have been constructed differently between invocations. Some feedback on whether this is a reasonable approach would help shape the PR.

PR Checklist

@stevenebutler
Copy link
Contributor Author

@microsoft-github-policy-service agree

@CarloToso
Copy link
Contributor

CarloToso commented Feb 18, 2023

Maybe you could do something like this for WebRequestSession.cs (it's just an example I think it can be done better)

        internal int MaxAutomaticRedirections
        {
            get => _maxAutomaticRedirections;
            set => _maxAutomaticRedirections = SetVariable(typeof(int), _maxAutomaticRedirections, value);
        }

        internal dynamic SetVariable(Type type, object oldValue, object newValue)
        {
            dynamic convertedOldValue = Convert.ChangeType(oldValue, type);
            dynamic convertedNewValue = Convert.ChangeType(newValue, type);
            if (convertedOldValue != convertedNewValue)
            {
                _changed = true;
                return convertedNewValue;
            }
            else
            {
                return convertedOldValue;
            }
        }

// build the cookie jar
Cookies = new CookieContainer();
// Build the cookie jar
_cookies = new CookieContainer();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand the code correctly you can revert _cookies -> Cookies (and the same for all the others in WebRequestSession())

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could, but it would go through a longer code path checking for change when we already know it's changed. Is that the preferred option?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @iSazonov will answer to this question

@stevenebutler
Copy link
Contributor Author

Maybe you could do something like this for WebRequestSession.cs (it's just an example I think it can be done better)

        internal int MaxAutomaticRedirections
        {
            get => _maxAutomaticRedirections;
            set => _maxAutomaticRedirections = SetVariable(typeof(int), _maxAutomaticRedirections, value);
        }

        internal dynamic SetVariable(Type type, object oldValue, object newValue)
        {
            dynamic convertedOldValue = Convert.ChangeType(oldValue, type);
            dynamic convertedNewValue = Convert.ChangeType(newValue, type);
            if (convertedOldValue != convertedNewValue)
            {
                _changed = true;
                return convertedNewValue;
            }
            else
            {
                return convertedOldValue;
            }
        }

I used a couple of generics to do something similar. I'm not a fan of using dynamic since it turns off lots of type checking.

   private void SetClassVar<T>(ref T oldValue, T newValue) where T : class
        {
            if (oldValue == newValue)
            {
                return;
            }
            _changed = true;
            oldValue = newValue;
        }

        private void SetStructVar<T>(ref T oldValue, T newValue) where T : struct
        {
            if (oldValue.Equals(newValue))
            {
                return;
            }
            _changed = true;
            oldValue = newValue;
        }

Let me know if there's a better way. I wasn't able to use a single generic for both structs and classes but I'm not an expert in this area and someone else may have a better suggestion.

@CarloToso
Copy link
Contributor

@stevenebutler you could look into adding some tests to WebCmdlets.Tests.ps1

@stevenebutler
Copy link
Contributor Author

stevenebutler commented Feb 19, 2023 via email

* Move GetHttpClient body to WebRequestSession
* Make internal properties setter only
* Implement IDisposable for WebRequestSession
* Add tests for persistence
* Fix GetHashCode() / Equals() on WebProxy
As per review request from @iSazonov, WebRequestPSCmdlet implements
IDisposable and cleans up non-persistent WebSession objects in the
Dispose method.
Responding to review comments from @iSazonov
$caught = $true
}
$Warnings | Should -be $connWarning
$caught | should -BeTrue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$caught | should -BeTrue
$caught | Should -BeTrue

} catch {
$caught = $true
}
$Warnings | Should -be $connWarning
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$Warnings | Should -be $connWarning
$Warnings | Should -Be $connWarning


It 'Connection persistence maintained' {
$Uri = Get-WebListenerUrl
$null = Invoke-WebRequest -Uri $uri -SessionVariable Session -wv warnings
Copy link
Contributor

@CarloToso CarloToso Feb 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$null = Invoke-WebRequest -Uri $uri -SessionVariable Session -wv warnings
$null = Invoke-WebRequest -Uri $uri -SessionVariable Session -WarningVariable Warnings

Could you please, always use variables with the same capitalization in the tests

}

It 'Connection persistence outperforms non-persistence' {
$Uri = Get-WebListenerUrl -Https
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$Uri = Get-WebListenerUrl -Https
$uri = Get-WebListenerUrl -Https

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have addressed the naming issues in the latest commit.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can remove the W.I.P label

@stevenebutler stevenebutler changed the title WIP: Enable HTTP persistence when using Session with Invoke-WebRequest and Invoke-RestMethod Enable HTTP persistence when using Session with Invoke-WebRequest and Invoke-RestMethod Feb 26, 2023
{
// If both ProxyCredential and ProxyUseDefaultCredentials are passed,
// UseDefaultCredentials will overwrite the supplied credentials.
webProxy.UseDefaultCredentials = true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is not correct since the code is in else branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right. However this is what was there before, which makes me wonder if it's a bug, or just a bad comment?

Should we fix the comment or the code?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CarloToso Could you please look this?

$null = Invoke-WebRequest $uri -WebSession $Session -Proxy 'http://localhost:8080' -WarningVariable warnings
$warnings | Should -Be $connWarning
}
}
}

Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to duplicate the tests for Invoke-RestMethod cmdlet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's get it right for IWR then I'll look at duplicating.

@iSazonov iSazonov mentioned this pull request Feb 28, 2023
22 tasks
CarloToso added a commit to CarloToso/PowerShell that referenced this pull request Feb 28, 2023
stevenebutler and others added 4 commits March 1, 2023 07:26
Co-authored-by: Ilya <darpa@yandex.ru>
Comment improvements
Changed reconnection warning to verbose message
Moved verbose message text to resource
Updated tests
// This indicates GetResponse will handle redirects.
SetStructVar(ref _allowAutoRedirect, !(handleRedirect || MaximumRedirection == 0));
// Do not auto redirect if the the caller does not want it, or maximum redirections is 0
SetStructVar(ref _allowAutoRedirect, !(doNotRedirect || MaximumRedirection == 0));
Copy link
Contributor

@CarloToso CarloToso Mar 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the change from handleRedirect to doNotRedirect or doNotHandleRedirect could be confusing. Maybe you can find a new name that always makes sense. Maybe cmdletHandleRedirect or something better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is handleRedirect from the perspective of cmdlet, but do not from the perspective of the websession's httpclient. I'm not sure what else to call it. Have made the change you suggested to doNotHandleRedirect in the WebSession.

Copy link
Contributor

@CarloToso CarloToso Mar 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think doNotHandleRedirect is better but it's still not the best (sorry I deleted the suggestion, I was thinking of a more fitting name), let's keep thinking about it and use doNotHandleRedirect in the meantime

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about supressHttpClientRedirects ?

Also, could you take a look at @iSazonov 's comment regarding the comment around UseDefaultCredentials. It looks wrong based on the code, but I'm not sure if the code or comment is ultimately correct.

                        // If both ProxyCredential and ProxyUseDefaultCredentials are passed,
                        // UseDefaultCredentials will overwrite the supplied credentials.
                        webProxy.UseDefaultCredentials = true;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gave it a quick look and it seems the code could be wrong, not only there but every time we use UseDefaultCredentials, when I have some time I will open a tracking issue to investigate.

supressHttpClientRedirects might work, what do you think @iSazonov?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe more short disableRedirect.

@pull-request-quantifier-deprecated

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


Quantification details

Label      : Large
Size       : +282 -56
Percentile : 73.8%

Total files changed: 5

Change summary by file extension:
.cs : +149 -56
.resx : +3 -0
.ps1 : +130 -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.

@iSazonov
Copy link
Collaborator

iSazonov commented Mar 1, 2023

@stevenebutler The PR contains too many commits and comments. I suggest to close the PR and open new one. Also maybe squash commits - it seems the history is not so useful.

@stevenebutler
Copy link
Contributor Author

I am closing this PR in favour of #19249

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants