1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
# justbackoff [](https://travis-ci.org/admiralobvious/justbackoff) [](https://codecov.io/gh/admiralobvious/justbackoff)
A simple backoff algorithm for Python >3.6.
### Install
```shell script
$ pip install justbackoff
```
### Usage
Backoff is a counter. It starts at `min_ms`. After every call to `duration()`,
it is multiplied by `factor`. It is capped at `max_ms`.
It returns to `min_ms` on every call to `reset()`.
`jitter` adds randomness ([see below](#example-using-jitter)).
---
#### Simple example
``` python
from justbackoff import Backoff
b = Backoff(min_ms=100, max_ms=10000, factor=2, jitter=False)
print(b.duration())
print(b.duration())
print(b.duration())
print("Reset!")
b.reset()
print(b.duration())
```
``` shell script
0.1
0.2
0.4
Reset!
0.1
```
---
#### Example using `socket` package
``` python
import socket
import time
from justbackoff import Backoff
sock = socket.socket()
b = Backoff()
while True:
try:
sock.connect(("127.0.0.1", 1337))
except Exception as e:
d = b.duration()
print("{}, reconnecting in {} seconds".format(e, d))
time.sleep(d)
continue
b.reset()
sock.send("Hello, world!")
sock.close()
```
---
#### Example using `jitter`
Enabling `jitter` adds some randomization to the backoff durations.
[See Amazon's writeup of performance gains using jitter](http://www.awsarchitectureblog.com/2015/03/backoff.html).
Seeding is not necessary but doing so gives repeatable results.
```python
import random
from justbackoff import Backoff
b = Backoff(min_ms=100, max_ms=10000, factor=2, jitter=True)
random.seed(42)
print(b.duration())
print(b.duration())
print(b.duration())
print("Reset!")
b.reset()
print(b.duration())
print(b.duration())
print(b.duration())
```
``` shell script
0.1
0.102501075522
0.182508795511
Reset!
0.1
0.173647121416
0.303009846227
```
#### Credits
Ported from Go [backoff](https://github.com/jpillora/backoff)
|