lostisland / faraday

Simple, but flexible HTTP client library, with support for multiple backends.
https://lostisland.github.io/faraday
MIT License
5.7k stars 971 forks source link

Change connection proxy #1534

Closed jclusso closed 7 months ago

jclusso commented 7 months ago

Basic Info

Issue description

If you try and change the proxy user of a connection, it does not change unless the URI has changed. I'm not sure if this is the intended behaviour or if there is a way around this. The only thing I've found I can do is to add some sort of query parameter on the proxy uri which will fix it. I assume it has something to do with how this works, but I couldn't figure it out.

Steps to reproduce

The code below will not change the proxy user on the retry. If you change the uri it will work and the user will be updated.

conn = Faraday.new('https://google.com') do |f|
  f.proxy = {
    uri: 'https://myproxyurl:1234',
    user: 'user',
    password: 'password'
  }
  f.request :retry, {
    max: 2,
    methods: %i(get post),
    retry_block: -> (env, options, retries, exception) do
      env.request.proxy = {
        uri: 'https://myproxyurl:1234',
        user: 'new-user',
        password: 'password'
      }
    end
  }
  f.adapter :net_http_persistent
end
iMacTia commented 7 months ago

Hi @jclusso, sorry about the confusion here, it took me a while to figure this out as well! It turns out, you're doing nothing wrong. What you're doing is reasonable and I'd expect it to work as well.

However, the adapter you're using net_http_persistent has a little caching of its own for the proxy. You can see how they decide if using the cached proxy or not based on the uri in this line. I'm gonna close this issue simply because this is not an issue with Faraday itself, but rather with the adapter implementation.

As that adapter is not actively maintained by anyone, I'd strongly encourage you to open up a PR if you'd like to see this fixed in the adapter. I don't use net_http_persistent much myself, so I'm unsure how much of an impact removing the cache would have, but I'd be happy to review a PR backed by some tests 👍

Otherwise, it seems like your idea of repeating the uri works. If you end up changing adapter you can try again removing it.

jclusso commented 7 months ago

Turns out this is fixed in Faraday 2 and the latest versions of net_http_persistent.