peburrows / goth

Elixir package for Oauth authentication via Google Cloud APIs
http://hexdocs.pm/goth
MIT License
289 stars 111 forks source link

Improve retry logic #123

Closed wojtekmach closed 2 years ago

wojtekmach commented 2 years ago

Currently we wait just 1s after each retry and try up to 3 times. This is not good enough. Let's make it configurable and give more robust defaults. It would probably be best to go for exponential backoff, see: https://github.com/wojtekmach/backoff/blob/main/lib/backoff.ex.

aleDsz commented 2 years ago

Looking into backoff.ex, sounds interesting not only to backoff, but to provide a behaviour that anyone could create their custom backoff with their rules.

So, I'm thinking about defining a default backoff implementation if the user doesn't define it on goth config. This could be awesome to make them to decide when Goth should stop retrying and how it should retry with their backoff.

defmodule MyApp.Backoff do
  use Goth.Backoff
  # All functions are already exported with the default implementation
  # But, allows you to override every public function

  @impl true
  def new(options) do
    # do stuff with your options ...
    # return the backoff struct
    %Backoff{type: atom(), min: pos_integer(), max: pos_integer(), state: any()}
  end

  @impl true
  def backoff(%Backoff{} = backoff) do
    # do stuff with your own logic to generate a new backoff state
    # If you want to keep the old state for know `backoff_types`, you can
    # call them with: `Backoff.backoff(backoff)` and handle your
    # custom backoff type specifically
    {123456, %{backoff | state: new_state}}
  end
end
wojtekmach commented 2 years ago

Pluggable backoff sounds interesting but lets keep it simple for now. We can always make it more configurable later.