| 1 |
package nebula |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"sync" |
| 6 |
"time" |
| 7 |
) |
| 8 |
|
| 9 |
// Scheduler is an allocation-conscious dispatch primitive for delayed work. |
| 10 |
// Pending items are handed to time.AfterFunc, and ready items land on a worker |
| 11 |
// channel for centralized dispatch in fire-time order. |
| 12 |
// |
| 13 |
// Pick a Scheduler when fire timing matters (exact deadlines, no bucketing) or when the scheduling |
| 14 |
// rate is uneven enough that idle CPU matters. Each fire is a runtime-spawned goroutine running the callback before |
| 15 |
// delivering to the worker, which is fine at sparse rates but adds up at line rate. |
| 16 |
// |
| 17 |
// Pick a TimerWheel when scheduling is high-rate and uniform: its O(1) insert, internal item cache, |
| 18 |
// and bucket-batched dispatch are cheaper at scale. |
| 19 |
// The caller drives the tick loop (Advance/Purge) and pays for fires at bucket boundaries rather than exact deadlines. |
| 20 |
type Scheduler[T any] struct { |
| 21 |
queue chan T |
| 22 |
pool sync.Pool |
| 23 |
} |
| 24 |
|
| 25 |
type schedItem[T any] struct { |
| 26 |
val T |
| 27 |
ctx context.Context |
| 28 |
s *Scheduler[T] |
| 29 |
timer *time.Timer |
| 30 |
fire func() |
| 31 |
} |
| 32 |
|
| 33 |
// NewScheduler builds a Scheduler whose worker channel is sized to queueSize. |
| 34 |
// The buffer absorbs bursts of timers firing close together without |
| 35 |
// blocking the runtime's callback goroutines on the worker. |
| 36 |
func NewScheduler[T any](queueSize int) *Scheduler[T] { |
| 37 |
s := &Scheduler[T]{ |
| 38 |
queue: make(chan T, queueSize), |
| 39 |
} |
| 40 |
s.pool.New = func() any { |
| 41 |
si := &schedItem[T]{s: s} |
| 42 |
// fire is allocated exactly once per pool-resident item. |
| 43 |
// The closure captures only `si`, which stays stable for the item's lifetime. |
| 44 |
si.fire = func() { |
| 45 |
select { |
| 46 |
case si.s.queue <- si.val: |
| 47 |
case <-si.ctx.Done(): |
| 48 |
} |
| 49 |
var zero T |
| 50 |
si.val = zero |
| 51 |
si.ctx = nil |
| 52 |
si.s.pool.Put(si) |
| 53 |
} |
| 54 |
return si |
| 55 |
} |
| 56 |
return s |
| 57 |
} |
| 58 |
|
| 59 |
// Schedule arranges item to be delivered to the worker after delay. |
| 60 |
// The runtime's timer heap handles the wait, so the scheduler itself burns no CPU while idle. |
| 61 |
// The callback observes ctx: if ctx is cancelled before the timer fires, the item is dropped instead of queued. |
| 62 |
func (s *Scheduler[T]) Schedule(ctx context.Context, item T, delay time.Duration) { |
| 63 |
si := s.pool.Get().(*schedItem[T]) |
| 64 |
si.val = item |
| 65 |
si.ctx = ctx |
| 66 |
if si.timer == nil { |
| 67 |
si.timer = time.AfterFunc(delay, si.fire) |
| 68 |
} else { |
| 69 |
si.timer.Reset(delay) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// Run drains the worker queue, calling fn for each item. Returns when ctx is cancelled. |
| 74 |
// Tests that want deterministic timing should drive the queue directly rather than going through Schedule + Run. |
| 75 |
func (s *Scheduler[T]) Run(ctx context.Context, fn func(T)) { |
| 76 |
for { |
| 77 |
select { |
| 78 |
case <-ctx.Done(): |
| 79 |
return |
| 80 |
case item := <-s.queue: |
| 81 |
fn(item) |
| 82 |
} |
| 83 |
} |
| 84 |
} |