diff --git a/platypush/utils/__init__.py b/platypush/utils/__init__.py index a164c982b..20763fc90 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: