From 7157936b87b22d1224141ec4d1a843e7338f628e Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Mon, 14 Aug 2023 23:05:32 +0200 Subject: [PATCH] Added get_remaining_timeout utility function. --- platypush/utils/__init__.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/platypush/utils/__init__.py b/platypush/utils/__init__.py index a164c982b0..20763fc907 100644 --- a/platypush/utils/__init__.py +++ b/platypush/utils/__init__.py @@ -15,7 +15,8 @@ import ssl import urllib.request from threading import Lock as TLock from tempfile import gettempdir -from typing import Generator, Optional, Tuple, Union +import time +from typing import Generator, Optional, Tuple, Type, Union from dateutil import parser, tz from redis import Redis @@ -633,4 +634,16 @@ def get_default_pid_file() -> str: return os.path.join(gettempdir(), 'platypush.pid') +def get_remaining_timeout( + timeout: Optional[float], start: float, cls: Union[Type[int], Type[float]] = float +) -> Optional[Union[int, float]]: + """ + Get the remaining timeout, given a start time. + """ + if timeout is None: + return None + + return cls(max(0, timeout - (time.time() - start))) + + # vim:sw=4:ts=4:et: