Skip to content
This repository has been archived by the owner on Aug 15, 2023. It is now read-only.

nathforge/expbackoff

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ExpBackoff

An exponential backoff implementation as described in an AWS architecture post. Uses full jitter.

Example

from expbackoff import Backoff

backoff = Backoff(base_seconds=0.5, max_seconds=120)

while True:
    backoff.sleep() # Depending on previous failures, potentially delay before performing an action
    success = do_a_thing() # Perform an action, record whether it succeeded
    backoff.update(success) # Update the failure count

API

  • backoff = expbackoff.Backoff(...)
  • Create a Backoff object. Arguments:
    • base_seconds (required): Part of the backoff calculation, i.e base_seconds * 2 ^ (failures - 1)
    • max_seconds (required): Max seconds to delay, regardless of failure count.
    • failures (optional): Number of current failures. Useful if another service is handling your retries. Defaults to 0.
    • random (optional): A random object. Defaults to random.random.
  • backoff.sleep(): If failures have occurred, sleep for backoff.get_seconds_with_jitter()
  • backoff.update(success): Update the failure count by passing a boolean representing success.
  • backoff.get_seconds_with_jitter(): The current backoff time in seconds, with jitter applied. Zero if there are no recorded failures. Read-only.
  • backoff.get_raw_seconds(): The current backoff time in seconds, without jitter applied. Zero if there are no recorded failures. Read-only.
  • backoff.failures: The current number of failures. Read-only.

Alternatives

Popular options are retrying and backoff. These both use a decorator API so aren’t suitable for all uses - hence expbackoff.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages