tesla icon indicating copy to clipboard operation
tesla copied to clipboard

Retry does not work on timeout

Open MMore opened this issue 3 years ago • 0 comments

Hello,

love your library! I just wanted to add a retry for my existing API client using the Retry middleware. I expected it would retry on timeouts, but it does not. It returns {:error, :timeout} on a timeout, but retry isn't triggered. For other errors retry works as expected.

That's basically my module:

defmodule MyModule.MyApiClient do
    use Tesla
  
    require Logger
  
    plug(
      Tesla.Middleware.BaseUrl,
      "https://myurl.com"
    )
  
    plug(Tesla.Middleware.Headers, [
      {"Content-Type", "application/json; charset=UTF-8"}
    ])
  
    plug(Tesla.Middleware.Logger, debug: false)
  
    plug(Tesla.Middleware.Timeout, timeout: 12_000)
  
    plug(Tesla.Middleware.Retry,
      delay: 500,
      max_retries: 3,
      max_delay: 5_000,
      should_retry: fn
        {:ok, %{status: status}} when status in [400, 500] ->
          IO.puts(:retrying)
          true
  
        {:ok, _} ->
          false
  
        {:error, :timeout} ->
          IO.puts(:retrying_timeout)
          true
  
        {:error, reason} ->
          IO.puts(inspect(reason, pretty: true, limit: :infinity))
          IO.puts(:retrying_error)
          true
      end
    )
  
    plug(Tesla.Middleware.JSON)
  
    def new(token) do
      Tesla.client([{Tesla.Middleware.BearerAuth, token: token}])
    end
  
    def get_items(client) do
      get(client, "/more_url")
    end
  end

The following versions I use:

  • Elixir 1.13.4
  • hackney 1.18.1
  • Tesla 1.4.4

Any ideas, what I could do? Thank you!

MMore avatar Jun 25 '22 15:06 MMore