Skip to content

Question Regarding New HTTP Persistent Connection Feature in Web Cmdlets #20713

Open

Description

@JAK1047

Summary of the new feature / enhancement

One of the changes in 7.4.0 I'm the most excited for is the HTTP Connection Reuse (#19249) primarily to help speed up API calls with pagination where a call might need to be sent to the same endpoint 100+ times to retrieve all results.

A fairly common example of that however is taking advantage of the -FollowRelLink parameter to only have a single Invoke-RestMethod command in my script. I can't find any documentation stating as such, but would that parameter by default take advantage of this new improvement without specifying a WebSession? Since there would rarely be a time you specified that parameter if you were not expecting multiple calls to be made. Or would a dummy call still have to be made first and thrown away just to obtain the WebSession variable for use?

Proposed technical implementation details (optional)

No response

Activity

  1. added
    Issue-Enhancementthe issue is more of a feature request than a bug
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    on Nov 17, 2023
  2. JAK1047 commented on Nov 20, 2023

    @JAK1047
    Author

    Answered my own question that a websession isn't implicitly made when using -FollowRelLink. It would be a useful feature if feasible to implicitly reuse the HTTP connection when calling -FollowRelLink as a test on my end shows if I make a dummy call just to establish the web session first it cuts my command time from 6.7 minutes to 3.2 which is substantial. But in my own example I'll have to hard code a throwaway call first to establish a web session which many users unaware of this change may not think to do.

  3. stevenebutler commented on Nov 24, 2023

    @stevenebutler
    Contributor

    Have you tried creating your websession object manually first? I haven't tried this myself but it may do the trick.

    I don't have an API that has rellinks like you suggest but as an example, something like below might work for you?

    $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
    Invoke-RestMethod https://example.com -WebSession $session -FollowRelLink
  4. JAK1047 commented on Nov 25, 2023

    @JAK1047
    Author

    Have you tried creating your websession object manually first? I haven't tried this myself but it may do the trick.

    I don't have an API that has rellinks like you suggest but as an example, something like below might work for you?

    $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
    Invoke-RestMethod https://example.com -WebSession $session -FollowRelLink

    Yep that's essentially what I wound up doing in the end and can confirm it works. To expand on my point a bit though not many users will know about the new reuse functionality of 7.4 to use a WebSession and since -FollowRelLink is designed to be used in situations where multiple calls to the same address will be made (and as such benefits the most from this change implemented in 7.4) having the function automatically create and use a web session if one is not provided may be a nice improvement to the cmd.

  5. iSazonov commented on Nov 25, 2023

    @iSazonov
    Collaborator

    Web cmdlets always create WebSession if they haven't WebSession as argument. So in one cmdlet invocation the same HttpClient is used.

  6. JAK1047 commented on Nov 25, 2023

    @JAK1047
    Author

    And every subsequent call made from -FollowRelLink reuses that same WebSession created in the initial cmd as well? That was what the original question was, but from my own experimentation that did not seem to be the case. Specifying a web session first for the command to use was cutting the same command from 6.7 minutes to 3.2. I don't know of any public APIs with pagination support, but on Monday I can test against Graph API as well to see if there was just an issue with the API I was testing against (Okta in this case)

  7. JAK1047 commented on Nov 28, 2023

    @JAK1047
    Author

    Graph apparently uses Odata links for pagination so -FollowRelLink didn't work, but tested with a SolarWinds implementation we have and saw similar timings. Specifying a WebSession when using -FollowRelLink sped up the command time by nearly half. So, my assumption at the moment is still that the command does not reuse the initial WebSession created when FollowRelLink is used unless a web session is implicitly passed to it.

  8. stevenebutler commented on Dec 1, 2023

    @stevenebutler
    Contributor

    My reading of the code suggests Ilya (@iSazonov) is correct, so if it's taking double time without specifying a WebSession parameter then I have to assume there's some subtle interaction (bug?) that's causing it to recreate the WebSession client for each request. There's a lot of features/code in the path for following rel links so not easy to tell what without a test case to debug.

    If you could provide a test case I can look into it when I have a chance. Note that the WebSession only exists for a single CmdLet invocation - if it is not persisted through being passed as a parameter or being requested to be saved in the session variable it will be disposed at the end of each cmdlet invocation.

  9. JAK1047 commented on Dec 1, 2023

    @JAK1047
    Author

    Definitely thank you for looking. I only have two separate test cases within my own company, so I'd love to have someone else try to confirm there's not just a flaw to my testing methodology. Finding an API that's free, public, and follows pagination using rellinks to use as an example test case has turned up nada so far however. If I find anything I'll make sure to reply back.

  10. stevenebutler commented on Dec 1, 2023

    @stevenebutler
    Contributor

    If you are familiar with Fiddler Classic it is easy to see if multiple connections are being made by proxying the powershell commands through Fiddler.

    1. Start Fiddler
    2. Start PWSH session
    3. Run the command
    4. Monitor Fiddler for multiple CONNECT requests to the destination site
  11. JAK1047 commented on Dec 1, 2023

    @JAK1047
    Author

    Sadly my company laptop is locked down and software installations can only come through SCCM after approval, but I can probably do the same with plain old netsh trace and then opening the etl file in Wireshark on my personal PC. I'll look into it.

  12. stevenebutler commented on Dec 1, 2023

    @stevenebutler
    Contributor

    I setup a test with fiddler in autoresponder mode and it looks like pwsh maintained a persistent connection for FollowRelLink.

    Perhaps it behaves differently when a proxy is not in use - if you can show evidence it's not keeping a persistent connection please update here and I'll look into it further.
    image

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugNeeds-TriageThe issue is new and needs to be triaged by a work group.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions