edgurgel / httpoison

Yet Another HTTP client for Elixir powered by hackney
https://hex.pm/packages/httpoison
MIT License
2.23k stars 340 forks source link

Should I read body to avoid leaks? #98

Closed shankardevy closed 8 years ago

shankardevy commented 8 years ago

I am using HTTPoison to make several thousand requests/sec. I am recently been plagued with :system_limit error and I filed an issue with hackney here: https://github.com/benoitc/hackney/issues/257

The author says that I should read the response body of all requests. I am not sure if it has the same implication when I use HTTPoison and not use hackney directly. Can you shed some light?

edgurgel commented 8 years ago

HTTPoison fetches the body on every "sync" request:

https://github.com/edgurgel/httpoison/blob/master/lib/httpoison/base.ex#L400-L404

I'm not sure what's the cause of the system limit but I can try to replicate on my machine. Can you share the httpoison and hackney version you are using?

shankardevy commented 8 years ago

Pls see my updated comment here https://github.com/benoitc/hackney/issues/257#issuecomment-155911078

iex(56)> :erlang.system_info(:port_count)
11
iex(57)> HTTPoison.get "http://www.google.com"
{:ok,
 %HTTPoison.Response{body: "<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n<TITLE>302 Moved</TITLE></HEAD><BODY>\n<H1>302 Moved</H1>\nThe document has moved\n<A HREF=\"http://www.google.co.in/?gfe_rd=cr&amp;ei=2jVEVsWwKaTv8weynIGQDA\">here</A>.\r\n</BODY></HTML>\r\n",
  headers: [{"Cache-Control", "private"},
   {"Content-Type", "text/html; charset=UTF-8"},
   {"Location", "http://www.google.co.in/?gfe_rd=cr&ei=2jVEVsWwKaTv8weynIGQDA"},
   {"Content-Length", "261"}, {"Date", "Thu, 12 Nov 2015 06:46:50 GMT"},
   {"Server", "GFE/2.0"}], status_code: 302}}
iex(58)> :erlang.system_info(:port_count)     
12
iex(59)> :erlang.system_info(:port_count)     
12

After about 2mins

iex(60)> :erlang.system_info(:port_count)     
11

The delay of 2 mins to free up an used port increases the port count exponentially in my case and hits the limit causing system_limit error. Any ideas how to resolve it?

edgurgel commented 8 years ago

I suspect that this is caused because hackney holds connections for some time and reuse if the request is going for the same host. I imagine you are doing request for different hosts and it's opening too many connections? Can you try to tweak the timeout env that hackney uses?

https://github.com/benoitc/hackney/blob/master/src/hackney.app.src#L20

By default it's 150000.

Application.put_env(:hackney, :timeout, 10000)

After 10 seconds the created port will get released.

jschniper commented 8 years ago

I'm running into a similar problem. I've been seeing a hackney pool where the in_use count slowly grows over time until it completely fills, which causes the application to become unresponsive. Something seems to be causing the sockets to hang open. I've been trying to track it back down to the individual requests to see if there is any commonality but I haven't had much luck yet. For some extra background, we're using HTTPoison inside a Phoenix application to pull data from a web service.

Hopefully I'll have some more information soon.

jschniper commented 8 years ago

I went back and set up a separate pool for each request so I could see if there were any particular ones that were problematic and it turns out there is one particular call that seems to be the culprit. I've included a simplified version of the code below.

def profile(token) do
  pid = :poolboy.checkout(:redis_pool)

  case :eredis.q(pid, ["GET", "profile:#{token}"], 5000) do
    {:ok, :undefined} ->
      resp = Service.get!("/profile/#{token}", ["Content-Type": "text/plain"], hackney: [pool: :profile])

      :eredis.q(pid, ["SET", "profile:#{token}", Poison.encode!(resp.body), "EX", 900], 5000)

      profile = resp.body
    {:ok, profile} ->
      profile = Poison.decode!(profile)
  end

  :poolboy.checkin(:redis_pool, pid)

  profile
end
defmodule Service do
  use HTTPoison.Base

  def process_url(url) do
    "http://example.com" <> url
  end

  def process_request_headers(headers) do
    ["Content-Type": "application/json", "Accept": "application/json"] ++ headers
  end

  def process_response_body(body) do
    case Poison.decode(body) do
      {:ok, json} -> json
      _ -> body
    end
  end
end
edgurgel commented 8 years ago

Just out of curiosity, what's the size of the redis_pool? I imagine the size of the redis pool will be also the amount of simultaneous requests being done through HTTPoison/hackney. The amount of connections should at least the size of the redis_pool and I would also keep the time out limits similar so they both "give up" properly.

jschniper commented 8 years ago

The redis pool is sitting at 10 or 15 right now versus the 50 default max_connections for hackney but I'm not seeing any signs of too many requests. The behavior seems to be that we "lose" a socket every so often.

jschniper commented 8 years ago

I haven't had much time to circle back to this but I was wondering if you would recommend opening an issue in the hackney project?

edgurgel commented 8 years ago

@jschniper, yeah go ahead! :+1:

jschniper commented 8 years ago

I wanted to let you know that I replaced the call to HTTPoison with a call directly to hackney and I'm not seeing the sockets hanging open anymore. I went back and I noticed that the call I was making will either return a profile or a 204 if the profile can't be found so I was wondering if https://github.com/edgurgel/httpoison/blob/master/lib/httpoison/base.ex#L397 might be the culprit.

I need to let the application run for a while longer to make sure the problem doesn't reappear but it certainly looks better at this moment.

edgurgel commented 8 years ago

Can you share the hackney and HTTPoison versions that you are using? I want to investigate as well.

jschniper commented 8 years ago

I was using HTTPoison 0.8.0 which I think corresponds with hackney 1.4.8

edevil commented 8 years ago

Any news on this?

edgurgel commented 8 years ago

Related: https://github.com/edgurgel/httpoison/pull/114

Can you guys give a try on HTTPoison master branch? If we are sure that this fixes our problem I can release a new version.

jschniper commented 8 years ago

I've had my fork in production for a few days and everything looks good.

edgurgel commented 8 years ago

@jschniper oh ok! I thought you had just used directly hackney in production. That's great. I will release later today a bugfix version! Thank you for catching this bug :+1:

edgurgel commented 8 years ago

Released https://github.com/edgurgel/httpoison/releases/tag/v0.8.1