kamui/retriable

Feature request: allow `with_context` without context configuration

cabello opened this issue ยท 3 comments

Hi ๐Ÿ‘‹

From:

if !config.contexts.key?(context_key)
raise ArgumentError, "#{context_key} not found in Retriable.config.contexts. Available contexts: #{config.contexts.keys}"
end
I understand that a configured context is required prior to using that context.

I am working on a SDK that has clients to several internal services and I would like to use with_context before hitting each service so I can let users configure the retry ability of each service independently and I would like to fallback to a default configuration if such context is not configured yet.

For example, mixing what's available from the current documentation:

Retriable.configure do |c|
  c.tries = 5
  c.max_elapsed_time = 3600 # 1 hour
  c.contexts[:aws] = {
    tries: 3,
    base_interval: 5,
    on_retry: Proc.new { puts 'Curse you, AWS!' }
  }
  c.contexts[:mysql] = {
    tries: 10,
    multiplier: 2.5,
    on: Mysql::DeadlockException
  }
end

If later I use postgres context, I would like to fallback to the global config:

Retriable.with_context(:postgres) do
  # use global config, tries: 5, max_elapsed_time: 3600
end

One idea on how to implement this feature would be as a new option to with_context, for example fallback_global_config: true, would you accept a PR with such a thing?

Retriable.with_context(:postgres, fallback_global_config: true) do
    # use global config, tries: 5, max_elapsed_time: 3600
end

Thanks for sharing this awesome project and considering my feature request ๐Ÿ’œ

One way I found to circumvent this context fallback "limitation" was to configure retriable on a per request basis, something as follow:

def retriable_config
  {
    tries: config.retriable_tries || DEFAULT_TRIES,
    on: config.retriable_on,
    on_retry: config.retriable_on_retry,
    base_interval: config.retriable_base_interval,
    max_elapsed_time: config.retriable_max_elapsed_time,
    max_interval: config.retriable_max_interval,
    multiplier: config.retriable_multiplier,
    rand_factor: config.retriable_rand_factor,
  }.compact
end
Retriable.retriable(retriable_config) do
  request.execute
end

And each of my service clients have their on configuration.

in your example, why can't you just check whether the :postgres context has been configured before calling Retriable.with_context(), and if it's not just call Retriable.retriable()?

You are correct on the suggested approach although this would be nice to have I also believe simpler software works better. Closing for now.