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

View File

@@ -38,6 +38,7 @@ from synapse.logging.context import make_deferred_yieldable
from synapse.types import JsonDict
from synapse.util.cancellation import cancellable
from synapse.util.clock import Clock
from synapse.util.duration import Duration
from tests import unittest
from tests.http.server._base import test_disconnect
@@ -406,11 +407,11 @@ class CancellableDirectServeJsonResource(DirectServeJsonResource):
@cancellable
async def _async_render_GET(self, request: SynapseRequest) -> tuple[int, JsonDict]:
await self.clock.sleep(1.0)
await self.clock.sleep(Duration(seconds=1))
return HTTPStatus.OK, {"result": True}
async def _async_render_POST(self, request: SynapseRequest) -> tuple[int, JsonDict]:
await self.clock.sleep(1.0)
await self.clock.sleep(Duration(seconds=1))
return HTTPStatus.OK, {"result": True}
@@ -423,11 +424,11 @@ class CancellableDirectServeHtmlResource(DirectServeHtmlResource):
@cancellable
async def _async_render_GET(self, request: SynapseRequest) -> tuple[int, bytes]:
await self.clock.sleep(1.0)
await self.clock.sleep(Duration(seconds=1))
return HTTPStatus.OK, b"ok"
async def _async_render_POST(self, request: SynapseRequest) -> tuple[int, bytes]:
await self.clock.sleep(1.0)
await self.clock.sleep(Duration(seconds=1))
return HTTPStatus.OK, b"ok"