pydanny / refry

Refry is a modern, maintained, typed easy-to-use retry decorator.
BSD 3-Clause "New" or "Revised" License
22 stars 1 forks source link

feat(retry): Add support for backoff strategies and jitter #38

Closed rjvitorino closed 3 months ago

rjvitorino commented 4 months ago

This pull request adds support for different backoff strategies (sequential, logarithmic, exponential) and optional jitter to the @retry decorator, addressing the need for more flexible retry mechanisms as described in issue #3.

Summary of Changes

Backoff Strategies

The @retry decorator now supports the following backoff strategies:

Jitter

An optional jitter parameter has been added to introduce a random amount of time to each backoff interval, helping to mitigate the thundering herd problem.

Usage

Here is an example of how to use the updated @retry decorator:

import refry

@refry.retry(rate_limit_exception=Exception, backoff_increment=2, retries=5, backoff_type="sequential", jitter=True)
def function_with_problem():
    raise Exception('Something bad happens here')

function_with_problem()

Tests

Unit tests have been added to cover:

These changes improve the flexibility and robustness of the @refry.retry decorator, making it suitable for a wider range of use cases.

Issue Fixed

This pull request fixes issue #3 (except the multiple types of server errors/exceptions)

Checklist

Thank you for reviewing this pull request. @pydanny I look forward to your feedback.

rjvitorino commented 3 months ago

@pydanny did you check this?