Skip to content

Commit

Permalink
refactor: simplify exponential_backoff (#758)
Browse files Browse the repository at this point in the history
* refactor: simplify `exponential_backoff`

* Add docstrings

---------

Co-authored-by: Tom Christie <tom@tomchristie.com>
  • Loading branch information
m9810223 and tomchristie authored Aug 3, 2023
1 parent 29f8e90 commit 3cca055
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
11 changes: 9 additions & 2 deletions httpcore/_async/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@


def exponential_backoff(factor: float) -> Iterator[float]:
"""
Generate a geometric sequence that has a ratio of 2 and starts with 0.
For example:
- `factor = 2`: `0, 2, 4, 8, 16, 32, 64, ...`
- `factor = 3`: `0, 3, 6, 12, 24, 48, 96, ...`
"""
yield 0
for n in itertools.count(2):
yield factor * (2 ** (n - 2))
for n in itertools.count():
yield factor * 2**n


class AsyncHTTPConnection(AsyncConnectionInterface):
Expand Down
11 changes: 9 additions & 2 deletions httpcore/_sync/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@


def exponential_backoff(factor: float) -> Iterator[float]:
"""
Generate a geometric sequence that has a ratio of 2 and starts with 0.
For example:
- `factor = 2`: `0, 2, 4, 8, 16, 32, 64, ...`
- `factor = 3`: `0, 3, 6, 12, 24, 48, 96, ...`
"""
yield 0
for n in itertools.count(2):
yield factor * (2 ** (n - 2))
for n in itertools.count():
yield factor * 2**n


class HTTPConnection(ConnectionInterface):
Expand Down

0 comments on commit 3cca055

Please sign in to comment.