hackney icon indicating copy to clipboard operation
hackney copied to clipboard

intermittent {:error, :closed} response

Open spencerdcarlson opened this issue 5 years ago • 7 comments

I'm not sure if this is the best place to post this. My issues looks like it is similar to 439 but it might just be related to how I am using hackney.

We are making about 4.19K requests per min and seeing a {:error, :closed} response about once every 30 min.

Here is my basic configuration

# create a custom connection pool
defmodule MyApp.Application do
  use Application

  @moduledoc false

  def start(_type, _args) do
    children = [
      :hackney_pool.child_spec(MyApp.ConnectionPool, timeout: 150_000, max_connections: 50)
    ]

    opts = [strategy: :one_for_one, name: MyApp.Supervisor]
    Supervisor.start_link(children, opts)
  end
end
def post do
  url = "https://httpstat.us/200"
  json = Jason.encode!(%{cmd: "do it"})
  :hackney.request(
      :post,
      url,
      [{"content-type", "application/json"}],
      json,
      pool: MyApp.ConnectionPool
    )
    |> handle_response()
end

defp handle_response(response) do
    case response do
      {:ok, code, _, _ref} when is_number(code) and code >= 200 and code < 300 ->
        {:ok, :success}

      {:ok, 401, _, _ref} ->
        {:error, :unauthorized}

      {:ok, status, _, _} ->
        {:error, status}

      error = {:error, _type} ->
        error

      other ->
        other
    end
  end

The majority of the requests are returning successfully, but once every 30 min I am seeing {:error, :closed}

spencerdcarlson avatar May 29 '20 16:05 spencerdcarlson

I am also facing this issue. It is intermittent and can be noticed in logs , every hour. Its count is ~3 % of the total request.

ashish060211 avatar Jan 27 '21 08:01 ashish060211

Hello, we're seeing the same problem. The problem goes away if we disable connection pooling. I should also add that we're using Hackney 1.17.4.

alejom99 avatar Aug 12 '21 22:08 alejom99

Same issue with hackney v1.18.0 on Erlang 24.2.1: connections are periodically closed. Setting pool: false seems to fix the issue.

fireproofsocks avatar Feb 09 '22 15:02 fireproofsocks

It is expected that the server close sometimes the connextion. When do you get this error? Is this in the misdle of a request , atthe beginning or at the end ?

benoitc avatar Feb 10 '22 01:02 benoitc

I am triggering periodic API calls via a GenServer that uses Process.send_after to hit an API endpoint every 60 seconds. The failures always seem to happen in minute 3 (on the third request), and the {:error, :closed} seems to happen right as the connection is attempted.

fireproofsocks avatar Feb 11 '22 19:02 fireproofsocks