|
| 1 | +# Custom Wait Strategies |
| 2 | + |
| 3 | +Create custom wait generators for specialized retry patterns. |
| 4 | + |
| 5 | +## Wait Generator Interface |
| 6 | + |
| 7 | +A wait generator is a function that yields wait times in seconds: |
| 8 | + |
| 9 | +```python |
| 10 | +def my_wait_gen(): |
| 11 | + """Yields: 1, 2, 3, 4, 5, 5, 5, ...""" |
| 12 | + for i in range(1, 6): |
| 13 | + yield i |
| 14 | + while True: |
| 15 | + yield 5 |
| 16 | + |
| 17 | +@backoff.on_exception(my_wait_gen, Exception) |
| 18 | +def my_function(): |
| 19 | + pass |
| 20 | +``` |
| 21 | + |
| 22 | +## Parameters |
| 23 | + |
| 24 | +Accept parameters to customize behavior: |
| 25 | + |
| 26 | +```python |
| 27 | +def linear_backoff(start=1, increment=1, max_value=None): |
| 28 | + """Linear backoff: start, start+increment, start+2*increment, ...""" |
| 29 | + value = start |
| 30 | + while True: |
| 31 | + if max_value and value > max_value: |
| 32 | + yield max_value |
| 33 | + else: |
| 34 | + yield value |
| 35 | + value += increment |
| 36 | + |
| 37 | +@backoff.on_exception( |
| 38 | + linear_backoff, |
| 39 | + Exception, |
| 40 | + start=2, |
| 41 | + increment=3, |
| 42 | + max_value=30 |
| 43 | +) |
| 44 | +def my_function(): |
| 45 | + pass |
| 46 | +``` |
| 47 | + |
| 48 | +## Examples |
| 49 | + |
| 50 | +### Polynomial Backoff |
| 51 | + |
| 52 | +```python |
| 53 | +def polynomial_backoff(base=2, exponent=2, max_value=None): |
| 54 | + """Polynomial: base^(tries^exponent)""" |
| 55 | + n = 1 |
| 56 | + while True: |
| 57 | + value = base ** (n ** exponent) |
| 58 | + if max_value and value > max_value: |
| 59 | + yield max_value |
| 60 | + else: |
| 61 | + yield value |
| 62 | + n += 1 |
| 63 | + |
| 64 | +@backoff.on_exception(polynomial_backoff, Exception, base=2, exponent=1.5) |
| 65 | +``` |
| 66 | + |
| 67 | +### Stepped Backoff |
| 68 | + |
| 69 | +```python |
| 70 | +def stepped_backoff(steps): |
| 71 | + """Different wait times for different ranges |
| 72 | + steps = [(3, 1), (5, 5), (None, 10)] # 3 tries at 1s, next 5 at 5s, rest at 10s |
| 73 | + """ |
| 74 | + for max_tries, wait_time in steps: |
| 75 | + if max_tries is None: |
| 76 | + while True: |
| 77 | + yield wait_time |
| 78 | + else: |
| 79 | + for _ in range(max_tries): |
| 80 | + yield wait_time |
| 81 | + |
| 82 | +@backoff.on_exception( |
| 83 | + stepped_backoff, |
| 84 | + Exception, |
| 85 | + steps=[(3, 1), (3, 5), (None, 30)] |
| 86 | +) |
| 87 | +``` |
| 88 | + |
| 89 | +### Random Backoff |
| 90 | + |
| 91 | +```python |
| 92 | +import random |
| 93 | + |
| 94 | +def random_backoff(min_wait=1, max_wait=60): |
| 95 | + """Random wait between min and max""" |
| 96 | + while True: |
| 97 | + yield random.uniform(min_wait, max_wait) |
| 98 | + |
| 99 | +@backoff.on_exception(random_backoff, Exception, min_wait=1, max_wait=10) |
| 100 | +``` |
| 101 | + |
| 102 | +### Time-of-Day Aware |
| 103 | + |
| 104 | +```python |
| 105 | +from datetime import datetime |
| 106 | + |
| 107 | +def business_hours_backoff(): |
| 108 | + """Shorter waits during business hours""" |
| 109 | + while True: |
| 110 | + hour = datetime.now().hour |
| 111 | + if 9 <= hour < 17: |
| 112 | + yield 5 # 5 seconds during business hours |
| 113 | + else: |
| 114 | + yield 60 # 1 minute otherwise |
| 115 | + |
| 116 | +@backoff.on_exception(business_hours_backoff, Exception) |
| 117 | +``` |
0 commit comments