algesten / ureq

A simple, safe HTTP client
Apache License 2.0
1.72k stars 177 forks source link

Feature request: ability to clear the pool #810

Open sosthene-nitrokey opened 2 months ago

sosthene-nitrokey commented 2 months ago

There are some situations (firewall for example), where it is desirable to clear all connections in the pool. For example after a IO error in a request, one might want to clear all the connections in the pool to ensure that the error was not due to the connection being stale.

This feature request would be to add the following method to Agent:

    /// Remove all idle connections from the pool
    ///
    /// Requests after this being called will use a fresh connection.
    ///
    /// Requests that terminate after this call will be added to the pool.
    pub fn clear_pool(&self);

See this implementation for our own fork of ureq based on the 2.0 branch.

algesten commented 2 months ago

Interesting!

The last thing I did was to make it possible to override the AgenConfig settings per request.

It strikes me that this is somewhat related to the max_idle_age setting. Meaning if you set that to 0, you should get a fresh connection every time. We should maybe tweak the internals a bit to ensure this is indeed the behavior we get.

sosthene-nitrokey commented 2 months ago

That's great, but I don't think this solves this case. Sometimes you would know that no requests will happen for a while and want to close idle connections to not waste resources, and sending a new request with max_idle_age to zero for that seems wasteful.

algesten commented 2 months ago

@sosthene-nitrokey but then an Agent is quite cheap to create, so you could just drop the entire instance. The AgentConfig is cloneable, so you can configure it once and do .into() to get a new Agent up.

sosthene-nitrokey commented 2 months ago

There is no AgentConfig in the released ureq. Is it a 3.0 thing?

In 2.x the current AgentBuilder is not clonable.

There is also the issue that if the agent is cloned around, changing the agent needs an additional mechanism to ensure all instances are modified too. Something like arc-swap.

algesten commented 1 month ago

Sorry, yeah. My mind is in 3.x land.