Skip to content

Instantly share code, notes, and snippets.

@hbin
Last active October 19, 2017 05:46
Show Gist options
  • Save hbin/4df5213f1a324049de8719dc87bf3fc1 to your computer and use it in GitHub Desktop.
Save hbin/4df5213f1a324049de8719dc87bf3fc1 to your computer and use it in GitHub Desktop.
Randomness to the exponential backoff of max_retries
# Original uncapped, exponential backoff
EXPONENTIAL_BACKOFF = lambda { |c| Kernel.sleep(2 ** c.retries * c.config.retry_base_delay) }
# Exponential backoff capped to :max_delay
CAPPED_BACKOFF = lambda { |c| Kernel.sleep([c.config.retry_max_delay, (2 ** c.retries * c.config.retry_base_delay)].min) }
# Retain at least half of the capped backoff, + random the other half.
EQUAL_JITTER_BACKOFF = lambda { |c| delay = ([c.config.retry_max_delay, (2 ** c.retries * c.config.retry_base_delay)].min)/2.0 ; Kernel.sleep((delay + Kernel.rand(0..delay))) }
# Full Jitter, random between no delay and the capped exponential backoff
FULL_JITTER_BACKOFF = lambda { |c| Kernel.sleep(Kernel.rand(0..[c.config.retry_max_delay,(2 ** c.retries * c.config.retry_base_delay)].min))}
DEFAULT_BACKOFF = EXPONENTIAL_BACKOFF # EQUAL_JITTER might be a better default option
option(:retry_limit, 3) option(:retry_limit, 3)
option(:retry_max_delay,20) # same as the java sdk, caps exponential backoff after 6 retries.
option(:retry_base_delay,0.3)
option(:retry_backoff, DEFAULT_BACKOFF)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment