From 7fcd84bbe4fb0f9ae56422aacf2726d2fbc59a67 Mon Sep 17 00:00:00 2001 From: Eric Callahan Date: Thu, 27 Jan 2022 17:00:08 -0500 Subject: [PATCH] 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 --- moonraker/components/power.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/moonraker/components/power.py b/moonraker/components/power.py index a2932f9..7b01c29 100644 --- a/moonraker/components/power.py +++ b/moonraker/components/power.py @@ -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: