2011-08-26 13:15:23 -06:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package time
|
|
|
|
|
|
|
|
func init() {
|
2022-03-09 22:19:05 -07:00
|
|
|
// Force US/Pacific for time zone tests.
|
2013-01-14 15:09:42 -07:00
|
|
|
ForceUSPacificForTesting()
|
2011-08-26 13:15:23 -06:00
|
|
|
}
|
|
|
|
|
2017-09-18 11:22:29 -06:00
|
|
|
func initTestingZone() {
|
2022-03-09 22:19:05 -07:00
|
|
|
// For hermeticity, use only tzinfo source from the test's GOROOT,
|
|
|
|
// not the system sources and not whatever GOROOT may happen to be
|
|
|
|
// set in the process's environment (if any).
|
|
|
|
// This test runs in GOROOT/src/time, so GOROOT is "../..",
|
|
|
|
// but it is theoretically possible
|
|
|
|
sources := []string{"../../lib/time/zoneinfo.zip"}
|
|
|
|
z, err := loadLocation("America/Los_Angeles", sources)
|
2017-09-18 11:22:29 -06:00
|
|
|
if err != nil {
|
2021-07-14 14:21:11 -06:00
|
|
|
panic("cannot load America/Los_Angeles for testing: " + err.Error() + "; you may want to use -tags=timetzdata")
|
2017-09-18 11:22:29 -06:00
|
|
|
}
|
|
|
|
z.name = "Local"
|
|
|
|
localLoc = *z
|
|
|
|
}
|
|
|
|
|
2022-03-09 22:19:05 -07:00
|
|
|
var origPlatformZoneSources []string = platformZoneSources
|
2017-09-18 11:22:29 -06:00
|
|
|
|
2022-03-09 22:19:05 -07:00
|
|
|
func disablePlatformSources() (undo func()) {
|
|
|
|
platformZoneSources = nil
|
|
|
|
return func() {
|
|
|
|
platformZoneSources = origPlatformZoneSources
|
2017-09-18 11:22:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-26 13:15:23 -06:00
|
|
|
var Interrupt = interrupt
|
2011-12-07 12:47:25 -07:00
|
|
|
var DaysIn = daysIn
|
2013-09-06 13:47:39 -06:00
|
|
|
|
time: garbage collect unstopped Tickers and Timers
From the beginning of Go, the time package has had a gotcha:
if you use a select on <-time.After(1*time.Minute), even if the select
finishes immediately because some other case is ready, the underlying
timer from time.After keeps running until the minute is over. This
pins the timer in the timer heap, which keeps it from being garbage
collected and in extreme cases also slows down timer operations.
The lack of garbage collection is the more important problem.
The docs for After warn against this scenario and suggest using
NewTimer with a call to Stop after the select instead, purely to work
around this garbage collection problem.
Oddly, the docs for NewTimer and NewTicker do not mention this
problem, but they have the same issue: they cannot be collected until
either they are Stopped or, in the case of Timer, the timer expires.
(Tickers repeat, so they never expire.) People have built up a shared
knowledge that timers and tickers need to defer t.Stop even though the
docs do not mention this (it is somewhat implied by the After docs).
This CL fixes the garbage collection problem, so that a timer that is
unreferenced can be GC'ed immediately, even if it is still running.
The approach is to only insert the timer into the heap when some
channel operation is blocked on it; the last channel operation to stop
using the timer takes it back out of the heap. When a timer's channel
is no longer referenced, there are no channel operations blocked on
it, so it's not in the heap, so it can be GC'ed immediately.
This CL adds an undocumented GODEBUG asynctimerchan=1
that will disable the change. The documentation happens in
the CL 568341.
Fixes #8898.
Fixes #61542.
Change-Id: Ieb303b6de1fb3527d3256135151a9e983f3c27e6
Reviewed-on: https://go-review.googlesource.com/c/go/+/512355
Reviewed-by: Austin Clements <austin@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Russ Cox <rsc@golang.org>
2024-02-14 18:36:47 -07:00
|
|
|
func empty(arg any, seq uintptr, delta int64) {}
|
2013-09-06 13:47:39 -06:00
|
|
|
|
2020-12-02 10:19:13 -07:00
|
|
|
// Test that a runtimeTimer with a period that would overflow when on
|
|
|
|
// expiration does not throw or cause other timers to hang.
|
2013-09-06 13:47:39 -06:00
|
|
|
//
|
|
|
|
// This test has to be in internal_test.go since it fiddles with
|
|
|
|
// unexported data structures.
|
2020-12-02 10:19:13 -07:00
|
|
|
func CheckRuntimeTimerPeriodOverflow() {
|
|
|
|
// We manually create a runtimeTimer with huge period, but that expires
|
|
|
|
// immediately. The public Timer interface would require waiting for
|
|
|
|
// the entire period before the first update.
|
time: garbage collect unstopped Tickers and Timers
From the beginning of Go, the time package has had a gotcha:
if you use a select on <-time.After(1*time.Minute), even if the select
finishes immediately because some other case is ready, the underlying
timer from time.After keeps running until the minute is over. This
pins the timer in the timer heap, which keeps it from being garbage
collected and in extreme cases also slows down timer operations.
The lack of garbage collection is the more important problem.
The docs for After warn against this scenario and suggest using
NewTimer with a call to Stop after the select instead, purely to work
around this garbage collection problem.
Oddly, the docs for NewTimer and NewTicker do not mention this
problem, but they have the same issue: they cannot be collected until
either they are Stopped or, in the case of Timer, the timer expires.
(Tickers repeat, so they never expire.) People have built up a shared
knowledge that timers and tickers need to defer t.Stop even though the
docs do not mention this (it is somewhat implied by the After docs).
This CL fixes the garbage collection problem, so that a timer that is
unreferenced can be GC'ed immediately, even if it is still running.
The approach is to only insert the timer into the heap when some
channel operation is blocked on it; the last channel operation to stop
using the timer takes it back out of the heap. When a timer's channel
is no longer referenced, there are no channel operations blocked on
it, so it's not in the heap, so it can be GC'ed immediately.
This CL adds an undocumented GODEBUG asynctimerchan=1
that will disable the change. The documentation happens in
the CL 568341.
Fixes #8898.
Fixes #61542.
Change-Id: Ieb303b6de1fb3527d3256135151a9e983f3c27e6
Reviewed-on: https://go-review.googlesource.com/c/go/+/512355
Reviewed-by: Austin Clements <austin@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Russ Cox <rsc@golang.org>
2024-02-14 18:36:47 -07:00
|
|
|
t := (*Timer)(newTimer(runtimeNano(), 1<<63-1, empty, nil, nil))
|
2024-02-29 16:03:23 -07:00
|
|
|
defer t.Stop()
|
2013-09-06 13:47:39 -06:00
|
|
|
|
2020-12-02 10:19:13 -07:00
|
|
|
// If this test fails, we will either throw (when siftdownTimer detects
|
|
|
|
// bad when on update), or other timers will hang (if the timer in a
|
|
|
|
// heap is in a bad state). There is no reliable way to test this, but
|
|
|
|
// we wait on a short timer here as a smoke test (alternatively, timers
|
|
|
|
// in later tests may hang).
|
|
|
|
<-After(25 * Millisecond)
|
2013-09-06 13:47:39 -06:00
|
|
|
}
|
2018-04-14 01:50:52 -06:00
|
|
|
|
|
|
|
var (
|
|
|
|
MinMonoTime = Time{wall: 1 << 63, ext: -1 << 63, loc: UTC}
|
|
|
|
MaxMonoTime = Time{wall: 1 << 63, ext: 1<<63 - 1, loc: UTC}
|
2021-03-12 04:40:46 -07:00
|
|
|
|
|
|
|
NotMonoNegativeTime = Time{wall: 0, ext: -1<<63 + 50}
|
2018-04-14 01:50:52 -06:00
|
|
|
)
|