From 718ecbd982f9505afdf025a5107acfd26b4688f8 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Fri, 16 Feb 2024 12:26:00 +0000 Subject: [PATCH] timeout decorator func added to util --- xair_api/util.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/xair_api/util.py b/xair_api/util.py index 9a4c1b0..a26fb6d 100644 --- a/xair_api/util.py +++ b/xair_api/util.py @@ -1,6 +1,35 @@ import functools +import time from math import exp, log +from .errors import XAirRemoteConnectionTimeoutError + + +def timeout(func): + """ + Times out the login function once time elapsed exceeds remote.connect_timeout. + """ + + @functools.wraps(func) + def wrapper(*args, **kwargs): + remote, *_ = args + + err = None + start = time.time() + while time.time() < start + remote.connect_timeout: + try: + func(*args, **kwargs) + remote.logger.debug(f"login time: {round(time.time() - start, 2)}") + err = None + break + except XAirRemoteConnectionTimeoutError as e: + err = e + continue + if err: + raise err + + return wrapper + def lin_get(min, max, val): return min + (max - min) * val