Files
via/routine.go
Ryan Hamamura 532651552a refactor: simplify OnInterval API to auto-start and return stop func
Replace the exported OnIntervalRoutine struct (Start/Stop/UpdateInterval)
with a single function that auto-starts the goroutine and returns an
idempotent stop closure. Uses close(channel) instead of send-on-channel,
fixing a potential deadlock when the goroutine exits via context disposal.

Closes #5 item 4.
2026-02-12 12:27:50 -10:00

33 lines
513 B
Go

package via
import (
"sync/atomic"
"time"
)
func newOnInterval(ctxDisposedChan chan struct{}, duration time.Duration, handler func()) func() {
localInterrupt := make(chan struct{})
var stopped atomic.Bool
go func() {
tkr := time.NewTicker(duration)
defer tkr.Stop()
for {
select {
case <-ctxDisposedChan:
return
case <-localInterrupt:
return
case <-tkr.C:
handler()
}
}
}()
return func() {
if stopped.CompareAndSwap(false, true) {
close(localInterrupt)
}
}
}