Skip to content

stdlib: sleep and timeout (spike) - #183

Draft
vito wants to merge 2 commits into
mainfrom
sleep-timeout
Draft

vito wants to merge 2 commits into
mainfrom
sleep-timeout

Conversation

@vito

@vito vito commented Oct 5, 2026

Copy link
Copy Markdown
Owner

Spike of timing primitives for the stdlib. Split out of #179, where it was an unrelated commit.

What's here

  • sleep(duration: String!) pauses for the given duration, then returns null. It's cancelled along with the surrounding evaluation, e.g. when it loses a race or a timeout expires.
  • timeout(duration: String!) { ... } evaluates the block under a deadline. It returns the block's value, or null if the deadline wins. When the deadline wins, the block's in-flight work (sleeps, GraphQL requests, loops) is cancelled. Only the timeout's own deadline turns into null. Errors raised in the block, break/return, and cancellation of a parent all pass through unchanged.
  • loop now checks for cancellation between iterations. Without that check, a loop that never touches the network couldn't be stopped by a timeout, a lost race, or a failing {{ }} sibling.

Tests are in tests/test_sleep.dang.

Durations are a stopgap

Durations are plain Go duration strings ("300ms", "1.5s", "5m") for now. We should probably add a proper Duration scalar type:

  • constructed from a string, with a :: cast, and literals coerced into a Duration! slot
  • a canonical form
  • methods such as seconds / milliseconds
  • arithmetic with DateTime

We'd also add more Time/DateTime functions alongside it. #172 (the DateTime scalar feature request) is the companion design. Once Duration exists, sleep and timeout would take Duration! instead of String!.

Testing

I ran go test for every dang Go module (pkg/dang, tests, cmd/dang, …) and it passed: 1197 tests passed, 6 skipped because nvim and tree-sitter aren't installed. The run didn't use the usual dang:base image: that build is currently broken by an upstream apko SBOM error on libLLVM-23, so I ran the tests on a plain golang base image instead. The LSP/editor tests that need the dang base image didn't run.

SPIKE of timing primitives. sleep(duration) pauses for the given duration and
returns early when its context is cancelled; timeout(duration) { ... } runs a
block under a deadline and yields null when the deadline wins, cancelling
in-flight sleeps and GraphQL requests.

Durations are Go duration strings ("300ms", "1.5s") as a stopgap; a proper
Duration scalar is the intended replacement.

loop now checks for cancellation between iterations, so a loop that never
touches the network can still be stopped by a timeout, a lost race, or a failing
{{ }} sibling.

Signed-off-by: Alex Suraci <[email protected]>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

Latest commit: 44fd29d
Status: ✅  Deploy successful!
Preview URL: https://557a7e38.dang-3kk.pages.dev
Branch Preview URL: https://sleep-timeout.dang-3kk.pages.dev

View logs

`dang fmt` splits multi-statement blocks and the long rescue clause onto
their own lines; the test was hand-written compactly, so `fmt/stale`
flagged it.

The formatter needed two passes to reach a fixed point here: the first
pass broke the timeout blocks inside a one-line `assert { ... }`, the
second then expanded the assert bodies too.

Signed-off-by: Alex Suraci <[email protected]>
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