Skip to content
Open
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
16 changes: 8 additions & 8 deletions clock/clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ type PassiveClock interface {
type Clock interface {
PassiveClock
// After returns the channel of a new Timer.
// This method does not allow to free/GC the backing timer before it fires. Use
// NewTimer instead.
// As of Go 1.23, the garbage collector can recover unreferenced,
// unstopped timers. There is no reason to prefer NewTimer when After will do.
Comment on lines +34 to +35
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the change in Go 1.23, this information isn’t particularly useful for callers of After; it’s useful for callers of NewTimer who may want to reconsider and use After instead.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are just copied from the stdlib. I think this can be useful for people who are still using Go older than 1.23, as the comments implies there are GC issues before 1.23.

After(d time.Duration) <-chan time.Time
// NewTimer returns a new Timer.
NewTimer(d time.Duration) Timer
// Sleep sleeps for the provided duration d.
// Consider making the sleep interruptible by using 'select' on a context channel and a timer channel.
Sleep(d time.Duration)
// Tick returns the channel of a new Ticker.
// This method does not allow to free/GC the backing ticker. Use
// NewTicker from WithTicker instead.
// As of Go 1.23, the garbage collector can recover unreferenced
// tickers, even if they haven't been stopped.
Tick(d time.Duration) <-chan time.Time
}

Expand Down Expand Up @@ -95,8 +95,8 @@ func (RealClock) Since(ts time.Time) time.Duration {
}

// After is the same as time.After(d).
// This method does not allow to free/GC the backing timer before it fires. Use
// NewTimer instead.
// As of Go 1.23, the garbage collector can recover unreferenced,
// unstopped timers. There is no reason to prefer NewTimer when After will do.
func (RealClock) After(d time.Duration) <-chan time.Time {
return time.After(d)
}
Expand All @@ -116,8 +116,8 @@ func (RealClock) AfterFunc(d time.Duration, f func()) Timer {
}

// Tick is the same as time.Tick(d)
// This method does not allow to free/GC the backing ticker. Use
// NewTicker instead.
// As of Go 1.23, the garbage collector can recover unreferenced
// tickers, even if they haven't been stopped.
func (RealClock) Tick(d time.Duration) <-chan time.Time {
return time.Tick(d)
}
Expand Down