Skip to content

Commit

Permalink
feat(time): Use [time.Timer] instead since go1.23. Timer.Stop documen…
Browse files Browse the repository at this point in the history
…tation example no longer leads to deadlocks, as Timer/Ticker channels not receivable with old values after Stop or Reset returns.

golang/go#27169
golang/go#37196
golang/go#14383
  • Loading branch information
searKing committed Oct 9, 2024
1 parent 5a2ae66 commit 8c45a07
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go/time/sleep.go → go/time/sleep_go1.22.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build !go1.23

package time

import (
Expand Down
42 changes: 42 additions & 0 deletions go/time/sleep_go1.23.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2020 The searKing Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build go1.23

package time

import (
"time"
)

// Timer to fix time: Timer.Stop documentation example easily leads to deadlocks
// https://github.com/golang/go/issues/27169
//
// Deprecated: Use [time.Timer] instead since go1.23.
// https://github.com/golang/go/issues/37196
// https://github.com/golang/go/issues/14383
type Timer = time.Timer

// Deprecated: Use [time.NewTimer] instead since go1.23.
func NewTimer(d time.Duration) *Timer {
return time.NewTimer(d)
}

// Deprecated: Use [time.Timer] instead since go1.23.
func WrapTimer(t *time.Timer) *Timer {
return t
}

// Deprecated: Use [time.After] instead since go1.23.
func After(d time.Duration) <-chan time.Time {
return NewTimer(d).C
}

// Deprecated: Use [time.AfterFunc] instead since go1.23.
func AfterFunc(d time.Duration, f func()) *Timer {
t := time.AfterFunc(d, func() {
f()
})
return t
}

0 comments on commit 8c45a07

Please sign in to comment.