Skip to content

Mine/add timer system - #334

Open
michael-grunder wants to merge 6 commits into
valkey-io:mainfrom
michael-grunder:mine/add-timer-system
Open

Mine/add timer system#334
michael-grunder wants to merge 6 commits into
valkey-io:mainfrom
michael-grunder:mine/add-timer-system

Conversation

@michael-grunder

Copy link
Copy Markdown
Collaborator

No description provided.

bjosv and others added 6 commits July 3, 2026 09:31
Add src/timer.c with a pre-allocated pool of timers managed as a
sorted linked list. Timers can be scheduled, cancelled, and
processed one at a time (to allow callbacks to free the context).

This is the foundation for running multiple concurrent timers
(command timeout, connect timeout, cluster topology refresh, etc.)
through a single adapter scheduleTimer hook.

Callers that need periodic behavior reschedule in their callback.

Uses CLOCK_MONOTONIC on Unix and QueryPerformanceCounter on Windows.

Signed-off-by: Björn Svensson <bjorn.a.svensson@est.tech>
Connect and command timeouts are now separate timers:
- connect_timer: scheduled during connect, cancelled on success
- command_timer: scheduled on first write when connected, tracks
  reply activity to detect unresponsive servers

valkeyAsyncHandleTimeout dispatches expired timers via
valkeyProcessTimers. No adapter changes required.

| Test                     | main   | PR     |
|--------------------------|--------|--------|
| 10000x PING pipelined    | 0.004s | 0.005s | (avg from 4 runs)
| 10000x INCRBY pipelined  | 0.004s | 0.004s | (avg from 4 runs)
| 10000x LRANGE pipelined  | 0.793s | 0.782s | (avg from 4 runs)

Note: changing command_timeout at runtime no longer affects an
already-running timer. The new value takes effect after the current
timer fires.

Signed-off-by: Björn Svensson <bjorn.a.svensson@est.tech>
We store the timer in microseconds, but libhv uses milliseconds. This
means a timer of `100.5` milliseconds will get scheduled `now + 100ms`.

The timer then likely fires early, and we reschedule for `now + .5ms`
which truncates to `0ms` in libhv which then deletes the timer.

This issue was entierly found with codex.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants