Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTPoison manually close connection

This is a very simple module that keeps checking the requester's IP. I use backconnect proxies which means that it gets new IP on every http request.

defmodule Test do
  def go() do
    Enum.each(1..1, fn x ->
    Task.Supervisor.async_nolink(Scraper.TaskSupervisor, fn ->
      r = HTTPoison.get("https://api.ipify.org?format=json", [],
        [timeout: 10_000, recv_timeout: 10_000, proxy: "ip:port", ssl: [{:versions, [:'tlsv1.2']}]])

      case r do

        {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
          IO.inspect body |> Jason.decode
          :timer.sleep(1000)
          go()
      end
    end)
    end)

  end
end
Test.go()
:timer.sleep(2000000)

Problem? HTTPoison(hackney) doesn't release the connection as long as process is alive so IP is always the same. How would I manually close the connection inside:

{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
          IO.inspect body |> Jason.decode
like image 788
Nema Ga Avatar asked Mar 01 '26 07:03

Nema Ga


1 Answers

From the HTTPoison README:

Normally hackney opens and closes connections on demand, but it also creates a default pool of connections which are reused for requests to the same host. If the connection and host support keepalive, the connection is kept open until explicitly closed.

HTTPoison does not export the close for the underlying socket, and there is an open issue for that since 2017.

Meanwhile, you might call hackney_pool:stop_pool(:default). This is obviously not the optimal solution by any mean and it has a huge overhead. So my advise would be to either provide a pull request to HTTPoison enabling close connection functionality (via delegating to ;hackney_response.close/1) or simply get rid of redundant HTTPoison and just use hackney.

like image 63
Aleksei Matiushkin Avatar answered Mar 04 '26 09:03

Aleksei Matiushkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!