Skip to content

feat: add Parallel.withTimeout(Duration) for per-task deadlines#23

Merged
jimbao merged 8 commits intomainfrom
feat/timeout
May 4, 2026
Merged

feat: add Parallel.withTimeout(Duration) for per-task deadlines#23
jimbao merged 8 commits intomainfrom
feat/timeout

Conversation

@praveer-rai
Copy link
Copy Markdown
Contributor

@praveer-rai praveer-rai commented Apr 18, 2026

Summary

Per-task deadline that interrupts tasks exceeding the duration. A lightweight virtual thread timer fires the interrupt; it's cancelled if the task completes in time.

var parallel = Parallel.create(new GrpcContextPropagator())
.withMaxConcurrency(10)
.withTimeout(Duration.ofSeconds(5));

Composes with withMaxConcurrency. Applies to all operations (map, asMap, async, toEither). Complements gRPC deadlines — gRPC deadlines protect individual RPCs, withTimeout protects the overall task (which may
involve multiple RPCs, retries, etc.).

Test plan

  • Tasks completing within timeout return normally
  • Slow tasks are interrupted
  • Fast tasks unaffected by timeout setting
  • Composes with withMaxConcurrency
  • Rejects zero and negative durations
  • README updated

Returns a new Parallel that interrupts tasks exceeding the duration. A
virtual thread timer fires the interrupt; the finally block cancels the
timer if the task completes in time. Composes with withMaxConcurrency.
@praveer-rai praveer-rai requested a review from jimbao April 18, 2026 18:57
Comment thread blockless/src/main/java/io/github/pjlabs/blockless/Parallel.java
Race condition: if Thread.sleep(timeout) returns naturally between task
completion and the timer's interrupt call, the timer would interrupt a
thread whose task already finished — polluting its interrupt flag for
any subsequent blocking work on that thread.

Fix: AtomicBoolean done flag. Timer checks it before interrupting; the
finally block sets done=true before cancelling the timer.
@jimbao jimbao merged commit 70f6813 into main May 4, 2026
1 check passed
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