Skip to content

Commit f4a9ec8

Browse files
committed
feat(time): Use [time.Timer] instead since go1.23. Timer.Stop documentation 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
1 parent 842152e commit f4a9ec8

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

go/time/sleep.go renamed to go/time/sleep_go1.22.go

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
//go:build !go1.23
6+
57
package time
68

79
import (

go/time/sleep_go1.23.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2020 The searKing Author. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build go1.23
6+
7+
package time
8+
9+
import (
10+
"time"
11+
)
12+
13+
// Timer to fix time: Timer.Stop documentation example easily leads to deadlocks
14+
// https://github.com/golang/go/issues/27169
15+
//
16+
// Deprecated: Use [time.Timer] instead since go1.23.
17+
// https://github.com/golang/go/issues/37196
18+
// https://github.com/golang/go/issues/14383
19+
type Timer = time.Timer
20+
21+
// Deprecated: Use [time.NewTimer] instead since go1.23.
22+
func NewTimer(d time.Duration) *Timer {
23+
return time.NewTimer(d)
24+
}
25+
26+
// Deprecated: Use [time.Timer] instead since go1.23.
27+
func WrapTimer(t *time.Timer) *Timer {
28+
return t
29+
}
30+
31+
// Deprecated: Use [time.After] instead since go1.23.
32+
func After(d time.Duration) <-chan time.Time {
33+
return NewTimer(d).C
34+
}
35+
36+
// Deprecated: Use [time.AfterFunc] instead since go1.23.
37+
func AfterFunc(d time.Duration, f func()) *Timer {
38+
t := time.AfterFunc(d, func() {
39+
f()
40+
})
41+
return t
42+
}

0 commit comments

Comments
 (0)