Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion utils/timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ func SetTimeout(fn func(), sleep time.Duration) *Timer {
stopCh: make(chan struct{}),
}
timer.fn = func() {
defer func() {
// Ensure channel is drained to prevent leaks
select {
case <-timer.stopCh:
default:
}
}()

select {
case <-timer.timer.C:
fn()
Expand All @@ -61,7 +69,12 @@ func ClearTimeout(timer *Timer) {

func (t *Timer) Stop() {
if t.timer.Stop() {
t.stopCh <- struct{}{}
// Use non-blocking send to avoid goroutine leak if no reader
select {
case t.stopCh <- struct{}{}:
default:
// Channel is full or no reader, timer already stopped
}
}
}

Expand All @@ -72,6 +85,14 @@ func SetInterval(fn func(), sleep time.Duration) *Timer {
stopCh: make(chan struct{}),
}
timer.fn = func() {
defer func() {
// Ensure channel is drained to prevent leaks
select {
case <-timer.stopCh:
default:
}
}()

for {
select {
case <-timer.timer.C:
Expand Down