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