tenacity icon indicating copy to clipboard operation
tenacity copied to clipboard

wait with proportional, but limited, jitter

Open moshez opened this issue 2 years ago • 0 comments

Currently, for exponential-backoff-with-jitter, you have two options:

  • A constant jitter (additive)
  • random.uniform(0, exponential_wait_time)

In my code, I like 10% jitter -- this way it is proportional, but is still somewhat predictable.

The way I do it from the outside is to define a waiter as follows:

class wait_proportional_jitter_exponential(tenacity.wait_exponential):
    def __call__(self, retry_state: "RetryCallState") -> float:
        goal = super().__call__(retry_state=retry_state)
        return goal * random.uniform(0.9, 1.1)

Would this make sense to upstream? Probably not as-is, I'd make the 10% an argument. Maybe this could be merged with wait_random_exponential by adding an argument proportion, so that the code would read

class wait_random_exponential(wait_exponential):
    ...
    def __call__(self, retry_state: "RetryCallState") -> float:
        high = ...
        return random.uniform(self.min_proprtion * high, high)

(I could pass in min_proportion=0.9 for the behavior I need, and the default value could be min_proportion=0 for the current behavior).

Let me know if this makes sense, and if it does, whether you think it should be a new waiter or make an existing waiter more flexible, and I'll be happy to add a PR with code, docs, and unit tests.

moshez avatar Mar 28 '23 17:03 moshez