Skip to content

Add support for deadline-based timers - #238

Merged
lalinsky merged 4 commits into
mainfrom
feat-timer-deadline-support
Jan 26, 2026
Merged

lalinsky merged 4 commits into
mainfrom
feat-timer-deadline-support

Conversation

@lalinsky

Copy link
Copy Markdown
Owner

Summary

  • Add support for absolute deadline-based timer initialization in addition to duration-based timers
  • Update Timer.init to handle deadline-based timeouts
  • Update setTimer logic to skip calculating deadline when delay is zero (indicating deadline was set explicitly)
  • Add tests for zero duration timers and explicit deadline timers

Test Plan

  • All existing tests pass (326/326)
  • New tests verify zero duration timer behavior
  • New tests verify explicit deadline timer behavior

Change Timer.delay from Duration to ?Duration to support both
duration-based and absolute deadline-based timer initialization.
Update setTimer logic to handle optional delay field.

Add tests for zero duration timers and explicit deadline timers.
@coderabbitai

coderabbitai Bot commented Jan 26, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Timer representation changed from a raw delay field to a Timeout union; Timer.init now stores the Timeout directly. Loop APIs and internal timer lifecycle compute deadlines from the Timeout variants. Call sites and tests updated to pass Timeout-shaped literals and added zero-duration and explicit-deadline tests.

Changes

Cohort / File(s) Summary
Timer struct & init
src/ev/completion.zig
Replace delay: Duration = .zero with timeout: Timeout; simplify pub fn init(timeout: Timeout) Timer to always set .timeout = timeout and remove per-variant .delay/.deadline initialization.
Loop timer lifecycle & API
src/ev/loop.zig
Replace delay-based logic with timeout-based model: setTimer switches on timer.timeout (none/duration/deadline) to compute timer.deadline; internal removal/insertion order preserved; public setTimer now uses the embedded timeout.
Call sites updated
src/common.zig, src/runtime/autocancel.zig, examples/ev_demo.zig
Replace direct Duration arguments with Timeout-shaped literals (e.g., .{ .duration = ... }) and update logging to reference timer.timeout.duration where relevant.
Tests
src/ev/test/timer.zig
Update test calls to pass Timeout literals; add tests for zero-duration immediate completion and explicit-deadline behavior.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

A timeout slipped into the frame,
delay's old name no longer the same.
Switches set deadlines, tests applaud,
the loop keeps time, no need for fraud.
⏱️ Small change, steady cadence, onward.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Add support for deadline-based timers' directly matches the PR's main objective of introducing deadline-based timer initialization.
Description check ✅ Passed The description clearly outlines the changes: adding deadline-based timer support, updating Timer.init, modifying setTimer logic, and adding new tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat-timer-deadline-support

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@src/ev/loop.zig`:
- Around line 225-238: The current setTimer uses was_active = timer.c.state ==
.running which can race with the firing path that removes the heap entry and
zeroes deadline while leaving state as .running; change the guard to treat a
timer as active only if its deadline sentinel is set (e.g., was_active =
timer.c.state == .running and timer.deadline.ns > 0) so cross-thread rearming
won't call timers.remove() on a timer already removed; ensure you still update
deadline when delay is provided (supporting explicit-deadline timers where delay
is null), keep the std.debug.assert(timer.deadline.ns > 0) check, and then use
that was_active to decide between timers.remove(timer) vs incrementing
self.active before setting timer.c.state = .running and
self.timers.insert(timer).

Comment thread src/ev/loop.zig Resolved
Change Timer.delay to Timer.timeout to clarify that this field stores
the user's timeout specification (none/duration/deadline), while the
deadline field is only for internal heap management.

Fix setTimer to check timer.deadline.ns > 0 instead of timer.c.state
to correctly detect if timer was active. Remove timer from heap before
updating deadline, since heap removal depends on the old deadline value.
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.

1 participant