power: add retries to http requests

This is an attempt to mitigate 599 errors raised by tornado's
curl based http client.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2022-01-27 17:00:08 -05:00
parent 1cac7399f9
commit 7fcd84bbe4
1 changed files with 13 additions and 8 deletions

View File

@ -416,15 +416,20 @@ class HTTPDevice(PowerDevice):
async def _send_http_command(self,
url: str,
command: str
command: str,
retries: int = 3
) -> Dict[str, Any]:
try:
response = await self.client.fetch(url)
data = json_decode(response.body)
except Exception:
msg = f"Error sending '{self.type}' command: {command}"
logging.exception(msg)
raise self.server.error(msg)
for i in range(retries):
try:
response = await self.client.fetch(
url, connect_timeout=5., request_timeout=20.)
data = json_decode(response.body)
except Exception as e:
if i == retries - 1:
msg = f"Error sending '{self.type}' command: {command}"
raise self.server.error(msg) from e
else:
break
return data
async def _send_power_request(self, state: str) -> str: