Skip to content

Commit bdd896d

Browse files
committed
Documentation for timer drift for periodic timers
1 parent a3d4bdd commit bdd896d

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,22 @@ to rely on this high precision.
365365
Similarly, the execution order of timers scheduled to execute at the
366366
same time (within its possible accuracy) is not guaranteed.
367367

368+
This interface suggests that event loop implementations SHOULD use a
369+
monotic time source if available. Given that a monotonic time source is
370+
not available on PHP by default, event loop implementations MAY fall back
371+
to using wall-clock time.
372+
While this does not affect many common use cases, this is an important
373+
distinction for programs that rely on a high time precision or on systems
374+
that are subject to discontinuous time adjustments (time jumps).
375+
This means that if you schedule a timer to trigger in 30s and then adjust
376+
your system time forward by 20s, the timer SHOULD still trigger in 30s.
377+
See also [event loop implementations](#loop-implementations) for more details.
378+
379+
Additionally, periodic timers may be subject to timer drift due to
380+
re-scheduling after each invocation. As such, it's generally not
381+
recommended to rely on this for high precision intervals with millisecond
382+
accuracy or below.
383+
368384
#### cancelTimer()
369385

370386
The `cancelTimer(TimerInterface $timer): void` method can be used to

src/LoopInterface.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,17 @@ public function removeWriteStream($stream);
179179
* Similarly, the execution order of timers scheduled to execute at the
180180
* same time (within its possible accuracy) is not guaranteed.
181181
*
182+
* This interface suggests that event loop implementations SHOULD use a
183+
* monotic time source if available. Given that a monotonic time source is
184+
* not available on PHP by default, event loop implementations MAY fall back
185+
* to using wall-clock time.
186+
* While this does not affect many common use cases, this is an important
187+
* distinction for programs that rely on a high time precision or on systems
188+
* that are subject to discontinuous time adjustments (time jumps).
189+
* This means that if you schedule a timer to trigger in 30s and then adjust
190+
* your system time forward by 20s, the timer SHOULD still trigger in 30s.
191+
* See also [event loop implementations](#loop-implementations) for more details.
192+
*
182193
* @param int|float $interval The number of seconds to wait before execution.
183194
* @param callable $callback The callback to invoke.
184195
*
@@ -257,6 +268,11 @@ public function addTimer($interval, $callback);
257268
* your system time forward by 20s, the timer SHOULD still trigger in 30s.
258269
* See also [event loop implementations](#loop-implementations) for more details.
259270
*
271+
* Additionally, periodic timers may be subject to timer drift due to
272+
* re-scheduling after each invocation. As such, it's generally not
273+
* recommended to rely on this for high precision intervals with millisecond
274+
* accuracy or below.
275+
*
260276
* @param int|float $interval The number of seconds to wait before execution.
261277
* @param callable $callback The callback to invoke.
262278
*

0 commit comments

Comments
 (0)