Port Clock functions to use Duration class (#19229)

This changes the arguments in clock functions to be `Duration` and
converts call sites and constants into `Duration`. There are still some
more functions around that should be converted (e.g.
`timeout_deferred`), but we leave that to another PR.

We also changes `.as_secs()` to return a float, as the rounding broke
things subtly. The only reason to keep it (its the same as
`timedelta.total_seconds()`) is for symmetry with `as_millis()`.

Follows on from https://github.com/element-hq/synapse/pull/19223
This commit is contained in:
Erik Johnston
2025-12-01 13:55:06 +00:00
committed by GitHub
parent d143276bda
commit 1bddd25a85
95 changed files with 511 additions and 260 deletions
+4 -4
View File
@@ -158,6 +158,7 @@ from synapse.types.state import StateFilter
from synapse.util.async_helpers import maybe_awaitable
from synapse.util.caches.descriptors import CachedFunction, cached as _cached
from synapse.util.clock import Clock
from synapse.util.duration import Duration
from synapse.util.frozenutils import freeze
if TYPE_CHECKING:
@@ -1389,7 +1390,7 @@ class ModuleApi:
if self._hs.config.worker.run_background_tasks or run_on_all_instances:
self._clock.looping_call(
self._hs.run_as_background_process,
msec,
Duration(milliseconds=msec),
desc,
lambda: maybe_awaitable(f(*args, **kwargs)),
)
@@ -1444,8 +1445,7 @@ class ModuleApi:
desc = f.__name__
return self._clock.call_later(
# convert ms to seconds as needed by call_later.
msec * 0.001,
Duration(milliseconds=msec),
self._hs.run_as_background_process,
desc,
lambda: maybe_awaitable(f(*args, **kwargs)),
@@ -1457,7 +1457,7 @@ class ModuleApi:
Added in Synapse v1.49.0.
"""
await self._clock.sleep(seconds)
await self._clock.sleep(Duration(seconds=seconds))
async def send_http_push_notification(
self,