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

[API Proposal]: HttpClient should consider adding a CloseIdleConnections() method #62256

Open
geoffkizer opened this issue Dec 1, 2021 · 3 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Net.Http
Milestone

Comments

@geoffkizer
Copy link
Contributor

geoffkizer commented Dec 1, 2021

Background and motivation

Golang has this, see: https://pkg.go.dev/net/http#Client.CloseIdleConnections

This allows a user who knows that their HTTP usage has stopped, or at least decreased significantly, to force idle connections to be closed immediately instead of waiting for idle timeout.

Users can partially achieve this today by just disposing the HttpClient, which will cause all connections to be closed immediately; however, it will also cancel pending requests, so it is only viable if the client usage has completely stopped.

API Proposal

namespace System.Net.Http
{
    public class HttpClient
    {
        public void CloseIdleConnections();
    }
}

This probably requires a similar API at the HttpMessageHandler layer that the HttpClient can call into.

API Usage

var client = new HttpClient();
// use the client a bunch
client.CloseIdleConnections();

Alternative Designs

As mentioned above, you can just Dispose the client entirely if you are done with it for now, and create a new one when you need it again.

Alternatively, you can set the idle timeout to a smaller value, which will cause idle connections to be scavenged more quickly.

Risks

No response

@geoffkizer geoffkizer added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Dec 1, 2021
@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Net.Http untriaged New issue has not been triaged by the area owner labels Dec 1, 2021
@ghost
Copy link

ghost commented Dec 1, 2021

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

Background and motivation

Golang has this, see: https://pkg.go.dev/net/http#Client.CloseIdleConnections

This allows a user who knows that their HTTP usage has stopped, or at least decreased significantly, to force idle connections to be closed immediately instead of waiting for idle timeout.

Users can partially achieve this today by just disposing the HttpClient, which will cause all connections to be closed immediately; however, it will also cancel pending requests, so it is only viable if the client usage has completely stopped.

API Proposal

namespace System.Net.Http
{
    public class HttpClient
    {
        public void CloseIdleConnections());
    }
}

This probably requires a similar API at the HttpMessageHandler layer that the HttpClient can call into.

API Usage

var client = new HttpClient();
// use the client a bunch
client.CloseIdleConnections();

Alternative Designs

As mentioned above, you can just Dispose the client entirely if you are done with it for now, and create a new one when you need it again.

Alternatively, you can set the idle timeout to a smaller value, which will cause idle connections to be scavenged more quickly.

Risks

No response

Author: geoffkizer
Assignees: -
Labels:

api-suggestion, area-System.Net.Http, untriaged

Milestone: -

@geoffkizer geoffkizer added this to the Future milestone Dec 1, 2021
@antonfirsov
Copy link
Member

Would it be a silent no-op when the underlying HttpMessageHandler doesn't support it?

@karelz karelz removed the untriaged New issue has not been triaged by the area owner label Dec 2, 2021
@karelz
Copy link
Member

karelz commented Dec 2, 2021

Triage: Nice to have. Similar to GC.Collect - generally not needed, but in special circumstances useful.
Would nice to see if any customers would find it useful and what the situations would be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Net.Http
Projects
None yet
Development

No branches or pull requests

3 participants