Skip to content

Commit 0a09581

Browse files
committed
Port call_later
1 parent 03c8a6d commit 0a09581

23 files changed

+139
-51
lines changed

synapse/app/phone_stats_home.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,14 @@ async def _generate_monthly_active_users() -> None:
278278
# We need to defer this init for the cases that we daemonize
279279
# otherwise the process ID we get is that of the non-daemon process
280280
clock.call_later(
281-
0,
281+
Duration(seconds=0),
282282
performance_stats_init,
283283
)
284284

285285
# We wait 5 minutes to send the first set of stats as the server can
286286
# be quite busy the first few minutes
287287
clock.call_later(
288-
INITIAL_DELAY_BEFORE_FIRST_PHONE_HOME.as_secs(),
288+
INITIAL_DELAY_BEFORE_FIRST_PHONE_HOME,
289289
phone_stats_home,
290290
hs,
291291
stats,

synapse/handlers/delayed_events.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
UserID,
4343
create_requester,
4444
)
45+
from synapse.util.duration import Duration
4546
from synapse.util.events import generate_fake_event_id
4647
from synapse.util.metrics import Measure
4748
from synapse.util.sentinel import Sentinel
@@ -92,7 +93,7 @@ async def _schedule_db_events() -> None:
9293
# Kick off again (without blocking) to catch any missed notifications
9394
# that may have fired before the callback was added.
9495
self._clock.call_later(
95-
0,
96+
Duration(seconds=0),
9697
self.notify_new_event,
9798
)
9899

@@ -501,17 +502,17 @@ def _schedule_next_at_or_none(self, next_send_ts: Timestamp | None) -> None:
501502

502503
def _schedule_next_at(self, next_send_ts: Timestamp) -> None:
503504
delay = next_send_ts - self._get_current_ts()
504-
delay_sec = delay / 1000 if delay > 0 else 0
505+
delay_duration = Duration(milliseconds=max(delay, 0))
505506

506507
if self._next_delayed_event_call is None:
507508
self._next_delayed_event_call = self._clock.call_later(
508-
delay_sec,
509+
delay_duration,
509510
self.hs.run_as_background_process,
510511
"_send_on_timeout",
511512
self._send_on_timeout,
512513
)
513514
else:
514-
self._next_delayed_event_call.reset(delay_sec)
515+
self._next_delayed_event_call.reset(delay_duration.as_secs())
515516

516517
async def get_all_for_user(self, requester: Requester) -> list[JsonDict]:
517518
"""Return all pending delayed events requested by the given user."""

synapse/handlers/message.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -434,14 +434,11 @@ def _schedule_expiry_for_event(self, event_id: str, expiry_ts: int) -> None:
434434

435435
# Figure out how many seconds we need to wait before expiring the event.
436436
now_ms = self.clock.time_msec()
437-
delay = (expiry_ts - now_ms) / 1000
437+
delay = Duration(milliseconds=max(expiry_ts - now_ms, 0))
438438

439-
# callLater doesn't support negative delays, so trim the delay to 0 if we're
440-
# in that case.
441-
if delay < 0:
442-
delay = 0
443-
444-
logger.info("Scheduling expiry for event %s in %.3fs", event_id, delay)
439+
logger.info(
440+
"Scheduling expiry for event %s in %.3fs", event_id, delay.as_secs()
441+
)
445442

446443
self._scheduled_expiry = self.clock.call_later(
447444
delay,

synapse/handlers/presence.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ def __init__(self, hs: "HomeServer"):
862862
# The initial delay is to allow disconnected clients a chance to
863863
# reconnect before we treat them as offline.
864864
self.clock.call_later(
865-
30,
865+
Duration(seconds=30),
866866
self.clock.looping_call,
867867
self._handle_timeouts,
868868
Duration(seconds=5),
@@ -872,7 +872,7 @@ def __init__(self, hs: "HomeServer"):
872872
# internally.
873873
if self._presence_enabled:
874874
self.clock.call_later(
875-
60,
875+
Duration(seconds=60),
876876
self.clock.looping_call,
877877
self._persist_unpersisted_changes,
878878
Duration(minutes=1),

synapse/handlers/room_member.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2191,7 +2191,7 @@ def __init__(self, hs: "HomeServer"):
21912191

21922192
# We kick this off to pick up outstanding work from before the last restart.
21932193
self._clock.call_later(
2194-
0,
2194+
Duration(seconds=0),
21952195
self.notify_new_event,
21962196
)
21972197

synapse/handlers/stats.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from synapse.metrics import SERVER_NAME_LABEL, event_processing_positions
3333
from synapse.storage.databases.main.state_deltas import StateDelta
3434
from synapse.types import JsonDict
35+
from synapse.util.duration import Duration
3536
from synapse.util.events import get_plain_text_topic_from_event_content
3637

3738
if TYPE_CHECKING:
@@ -72,7 +73,7 @@ def __init__(self, hs: "HomeServer"):
7273
# We kick this off so that we don't have to wait for a change before
7374
# we start populating stats
7475
self.clock.call_later(
75-
0,
76+
Duration(seconds=0),
7677
self.notify_new_event,
7778
)
7879

synapse/handlers/user_directory.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from synapse.storage.databases.main.user_directory import SearchResult
4141
from synapse.storage.roommember import ProfileInfo
4242
from synapse.types import UserID
43+
from synapse.util.duration import Duration
4344
from synapse.util.metrics import Measure
4445
from synapse.util.retryutils import NotRetryingDestination
4546
from synapse.util.stringutils import non_null_str_or_none
@@ -52,15 +53,15 @@
5253
# Don't refresh a stale user directory entry, using a Federation /profile request,
5354
# for 60 seconds. This gives time for other state events to arrive (which will
5455
# then be coalesced such that only one /profile request is made).
55-
USER_DIRECTORY_STALE_REFRESH_TIME_MS = 60 * 1000
56+
USER_DIRECTORY_STALE_REFRESH_TIME = Duration(seconds=60)
5657

5758
# Maximum number of remote servers that we will attempt to refresh profiles for
5859
# in one go.
5960
MAX_SERVERS_TO_REFRESH_PROFILES_FOR_IN_ONE_GO = 5
6061

6162
# As long as we have servers to refresh (without backoff), keep adding more
6263
# every 15 seconds.
63-
INTERVAL_TO_ADD_MORE_SERVERS_TO_REFRESH_PROFILES = 15
64+
INTERVAL_TO_ADD_MORE_SERVERS_TO_REFRESH_PROFILES = Duration(seconds=15)
6465

6566

6667
def calculate_time_of_next_retry(now_ts: int, retry_count: int) -> int:
@@ -137,13 +138,13 @@ def __init__(self, hs: "HomeServer"):
137138
# We kick this off so that we don't have to wait for a change before
138139
# we start populating the user directory
139140
self.clock.call_later(
140-
0,
141+
Duration(seconds=0),
141142
self.notify_new_event,
142143
)
143144

144145
# Kick off the profile refresh process on startup
145146
self._refresh_remote_profiles_call_later = self.clock.call_later(
146-
10,
147+
Duration(seconds=10),
147148
self.kick_off_remote_profile_refresh_process,
148149
)
149150

@@ -550,21 +551,21 @@ async def _handle_possible_remote_profile_change(
550551
now_ts = self.clock.time_msec()
551552
await self.store.set_remote_user_profile_in_user_dir_stale(
552553
user_id,
553-
next_try_at_ms=now_ts + USER_DIRECTORY_STALE_REFRESH_TIME_MS,
554+
next_try_at_ms=now_ts + USER_DIRECTORY_STALE_REFRESH_TIME.as_millis(),
554555
retry_counter=0,
555556
)
556557
# Schedule a wake-up to refresh the user directory for this server.
557558
# We intentionally wake up this server directly because we don't want
558559
# other servers ahead of it in the queue to get in the way of updating
559560
# the profile if the server only just sent us an event.
560561
self.clock.call_later(
561-
USER_DIRECTORY_STALE_REFRESH_TIME_MS // 1000 + 1,
562+
USER_DIRECTORY_STALE_REFRESH_TIME + Duration(seconds=1),
562563
self.kick_off_remote_profile_refresh_process_for_remote_server,
563564
UserID.from_string(user_id).domain,
564565
)
565566
# Schedule a wake-up to handle any backoffs that may occur in the future.
566567
self.clock.call_later(
567-
2 * USER_DIRECTORY_STALE_REFRESH_TIME_MS // 1000 + 1,
568+
USER_DIRECTORY_STALE_REFRESH_TIME * 2 + Duration(seconds=1),
568569
self.kick_off_remote_profile_refresh_process,
569570
)
570571
return
@@ -656,7 +657,9 @@ async def _unsafe_refresh_remote_profiles(self) -> None:
656657
if not users:
657658
return
658659
_, _, next_try_at_ts = users[0]
659-
delay = ((next_try_at_ts - self.clock.time_msec()) // 1000) + 2
660+
delay = Duration(
661+
milliseconds=next_try_at_ts - self.clock.time_msec()
662+
) + Duration(seconds=2)
660663
self._refresh_remote_profiles_call_later = self.clock.call_later(
661664
delay,
662665
self.kick_off_remote_profile_refresh_process,

synapse/handlers/worker_lock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def _wake_all_locks(
187187
lock.release_lock()
188188

189189
self._clock.call_later(
190-
0,
190+
Duration(seconds=0),
191191
_wake_all_locks,
192192
locks,
193193
)

synapse/http/client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
from synapse.types import ISynapseReactor, StrSequence
8888
from synapse.util.async_helpers import timeout_deferred
8989
from synapse.util.clock import Clock
90+
from synapse.util.duration import Duration
9091
from synapse.util.json import json_decoder
9192

9293
if TYPE_CHECKING:
@@ -172,7 +173,7 @@ def _make_scheduler(clock: Clock) -> Callable[[Callable[[], object]], IDelayedCa
172173

173174
def _scheduler(x: Callable[[], object]) -> IDelayedCall:
174175
return clock.call_later(
175-
_EPSILON,
176+
Duration(seconds=_EPSILON),
176177
x,
177178
)
178179

synapse/module_api/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,8 +1445,7 @@ def delayed_background_call(
14451445
desc = f.__name__
14461446

14471447
return self._clock.call_later(
1448-
# convert ms to seconds as needed by call_later.
1449-
msec * 0.001,
1448+
Duration(milliseconds=msec),
14501449
self._hs.run_as_background_process,
14511450
desc,
14521451
lambda: maybe_awaitable(f(*args, **kwargs)),

0 commit comments

Comments
 (0)