Skip to content

Commit

Permalink
Retry failed requests with backoff to make unstable connections less …
Browse files Browse the repository at this point in the history
…flaky (#399)

* Add backoff to retry request on error

* Update tests

* Use max_tries instead max_time to stabalize retry count
  • Loading branch information
DCSBL authored Aug 17, 2024
1 parent 5494c5d commit de0a77e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
Binary file modified .coverage
Binary file not shown.
2 changes: 2 additions & 0 deletions homewizard_energy/homewizard_energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Any, TypeVar

import async_timeout
import backoff
from aiohttp.client import ClientError, ClientResponseError, ClientSession
from aiohttp.hdrs import METH_GET, METH_PUT

Expand Down Expand Up @@ -145,6 +146,7 @@ async def identify(
await self.request("api/v1/identify", method=METH_PUT)
return True

@backoff.on_exception(backoff.expo, RequestError, max_tries=5)
async def request(
self, path: str, method: str = METH_GET, data: object = None
) -> Any:
Expand Down
17 changes: 14 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ python = "^3.9"
aiohttp = ">=3.0.0"
async-timeout = "^4.0.3"
multidict = "^6.0.5" ## To fix aiohttp dependency at python 3.12
backoff = "^2.2.1"

[tool.poetry.dev-dependencies]
aresponses = "^3.0.0"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_homewizard_energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ async def test_request_timeout():
with pytest.raises(RequestError):
await api.request("api/v1/data")

assert api._session.request.call_count == 1
assert api._session.request.call_count == 5


async def test_close_when_out_of_scope():
Expand Down

0 comments on commit de0a77e

Please sign in to comment.