aboutsummaryrefslogtreecommitdiff
path: root/src/context
AgeCommit message (Collapse)Author
2021-12-13all: gofmt -w -r 'interface{} -> any' srcRuss Cox
And then revert the bootstrap cmd directories and certain testdata. And adjust tests as needed. Not reverting the changes in std that are bootstrapped, because some of those changes would appear in API docs, and we want to use any consistently. Instead, rewrite 'any' to 'interface{}' in cmd/dist for those directories when preparing the bootstrap copy. A few files changed as a result of running gofmt -w not because of interface{} -> any but because they hadn't been updated for the new //go:build lines. Fixes #49884. Change-Id: Ie8045cba995f65bd79c694ec77a1b3d1fe01bb09 Reviewed-on: https://go-review.googlesource.com/c/go/+/368254 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
2021-10-02context: implement Context.Value using iteration rather than recursionroudkerk
In profiles of a production server, 2.3% of CPU time is spent in runtime.newstack for stacks with 12 or more chained Context.Value calls. Using iteration will avoid some needless stack resizes. When calling Context.Value in the same goroutine (see DeepValueSameGoRoutine) , no stack resizing is needed (after warming up), and this change reduces time/op by around 10%. The time to start a new goroutine and call Context.Value (see DeepValueNewGoRoutine) is reduced by over 75% if a stack resize is needed. If you factor out the overhead of starting the goroutine (about 960ns) then, avoiding the stack resize saves about 95%. ``` name old time/op new time/op delta CommonParentCancel-12 960ns ± 1% 958ns ± 1% ~ (p=0.561 n=9+9) WithTimeout/concurrency=40-12 1.31µs ± 2% 1.29µs ± 6% ~ (p=0.305 n=9+10) WithTimeout/concurrency=4000-12 1.30µs ± 2% 1.30µs ± 2% ~ (p=0.343 n=10+10) WithTimeout/concurrency=400000-12 1.03µs ± 1% 1.02µs ± 2% ~ (p=0.213 n=9+9) CancelTree/depth=1/Root=Background-12 123ns ± 5% 126ns ± 2% +2.61% (p=0.023 n=10+9) CancelTree/depth=1/Root=OpenCanceler-12 781ns ± 4% 806ns ± 4% +3.20% (p=0.022 n=9+10) CancelTree/depth=1/Root=ClosedCanceler-12 370ns ± 4% 369ns ± 3% ~ (p=0.497 n=9+10) CancelTree/depth=10/Root=Background-12 4.74µs ± 4% 4.78µs ± 3% ~ (p=0.516 n=10+10) CancelTree/depth=10/Root=OpenCanceler-12 6.31µs ± 4% 6.29µs ± 4% ~ (p=1.000 n=10+10) CancelTree/depth=10/Root=ClosedCanceler-12 2.10µs ± 5% 2.09µs ± 5% ~ (p=0.839 n=10+10) CancelTree/depth=100/Root=Background-12 51.0µs ± 3% 51.2µs ± 2% ~ (p=0.631 n=10+10) CancelTree/depth=100/Root=OpenCanceler-12 60.8µs ± 1% 61.6µs ± 4% ~ (p=0.274 n=8+10) CancelTree/depth=100/Root=ClosedCanceler-12 19.3µs ± 2% 19.0µs ± 3% ~ (p=0.123 n=10+10) CancelTree/depth=1000/Root=Background-12 504µs ± 4% 512µs ± 4% ~ (p=0.123 n=10+10) CancelTree/depth=1000/Root=OpenCanceler-12 615µs ± 6% 619µs ± 4% ~ (p=1.000 n=10+10) CancelTree/depth=1000/Root=ClosedCanceler-12 190µs ± 2% 192µs ± 3% ~ (p=0.190 n=9+9) CheckCanceled/Err-12 12.1ns ± 2% 12.1ns ± 2% ~ (p=0.615 n=10+10) CheckCanceled/Done-12 7.27ns ± 1% 7.26ns ± 1% ~ (p=0.698 n=10+10) ContextCancelDone-12 1.03ns ± 1% 1.03ns ± 1% ~ (p=0.474 n=9+9) DeepValueNewGoRoutine/depth=10-12 1.02µs ± 3% 0.99µs ± 2% -3.41% (p=0.000 n=10+10) DeepValueNewGoRoutine/depth=20-12 1.11µs ± 3% 1.08µs ± 2% -2.51% (p=0.004 n=10+10) DeepValueNewGoRoutine/depth=30-12 5.55µs ±10% 1.17µs ± 4% -78.91% (p=0.000 n=10+10) DeepValueNewGoRoutine/depth=50-12 5.70µs ±13% 1.35µs ± 2% -76.31% (p=0.000 n=10+10) DeepValueNewGoRoutine/depth=100-12 9.69µs ± 4% 1.82µs ± 2% -81.18% (p=0.000 n=10+10) DeepValueSameGoRoutine/depth=10-12 54.2ns ± 2% 46.8ns ± 2% -13.71% (p=0.000 n=9+9) DeepValueSameGoRoutine/depth=20-12 109ns ± 2% 97ns ± 2% -11.11% (p=0.000 n=10+10) DeepValueSameGoRoutine/depth=30-12 155ns ± 3% 140ns ± 1% -9.49% (p=0.000 n=10+10) DeepValueSameGoRoutine/depth=50-12 256ns ± 2% 226ns ± 2% -11.83% (p=0.000 n=10+10) DeepValueSameGoRoutine/depth=100-12 492ns ± 3% 442ns ± 1% -10.15% (p=0.000 n=10+10) ``` Fixes #47292 Change-Id: I6bdeb234c979fb8fd6bfb91fd345cb5038f52c75 Reviewed-on: https://go-review.googlesource.com/c/go/+/335790 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Damien Neil <dneil@google.com>
2021-04-10all: fix spellingsNaman Gera
This follows the spelling choices that the Go project has made for English words. https://github.com/golang/go/wiki/Spelling Change-Id: Ie7c586d2cf23020cb492cfff58c0831d2d8d3a78 GitHub-Last-Rev: e16a32cd225a275f73d236bcb33703986d110ded GitHub-Pull-Request: golang/go#45442 Reviewed-on: https://go-review.googlesource.com/c/go/+/308291 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Emmanuel Odeke <emmanuel@orijtech.com>
2021-02-24context: avoid importing context package twiceKevin Burke
Change-Id: Id0a127e080dda8ee62738922c6de8caf3719dd68 Reviewed-on: https://go-review.googlesource.com/c/go/+/295949 Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com> Trust: Josh Bleecher Snyder <josharian@gmail.com> Trust: Kevin Burke <kev@inburke.com> Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com> TryBot-Result: Go Bot <gobot@golang.org>
2021-02-24context: reduce contention in cancelCtx.DoneJosh Bleecher Snyder
Use an atomic.Value to hold the done channel. Conveniently, we have a mutex handy to coordinate writes to it. name old time/op new time/op delta ContextCancelDone-8 67.5ns ±10% 2.2ns ±11% -96.74% (p=0.000 n=30+28) Fixes #42564 Change-Id: I5d72e0e87fb221d4e230209e5fb4698bea4053c6 Reviewed-on: https://go-review.googlesource.com/c/go/+/288193 Trust: Josh Bleecher Snyder <josharian@gmail.com> Trust: Sameer Ajmani <sameer@golang.org> Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
2021-02-23context: fix XTestInterlockedCancelsKeiichi Hirobe
The test does not use Done channel, so fix that. Change-Id: I795feab2e95de815b8b6ee7a7fd90f19f7af7db1 Reviewed-on: https://go-review.googlesource.com/c/go/+/294749 Reviewed-by: Sameer Ajmani <sameer@golang.org> Trust: Sameer Ajmani <sameer@golang.org> Trust: Cody Oss <codyoss@google.com> Run-TryBot: Sameer Ajmani <sameer@golang.org> TryBot-Result: Go Bot <gobot@golang.org>
2020-03-31context: fix a flaky timeout in TestLayersTimeoutBryan C. Mills
In CL 223019, I reduced the short timeout in the testLayers helper to be even shorter than it was. That exposed a racy (time-dependent) select later in the function, which failed in one of the slower builders (android-386-emu). Also streamline the test to make it easier to test with a very high -count flag: - Run tests that sleep for shortDuration in parallel to reduce latency. - Use shorter durations in examples to reduce test running time. - Avoid mutating global state (in package math/rand) in testLayers. After this change (but not before it), 'go test -run=TestLayersTimeout -count=100000 context' passes on my workstation. Fixes #38161 Change-Id: Iaf4abe7ac308b2100d8828267cda9f4f8ae4be82 Reviewed-on: https://go-review.googlesource.com/c/go/+/226457 Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-03-18context: prevent creation of invalid contextsKyle Nusbaum
This commit makes it impossible to create derived contexts with nil parents. Previously it was possible to create derived contexts with nil parents, and invalid contexts could propogate through the program. Eventually this can cause a panic downstream, which is difficult to trace back to the source of the error. Although `WithCancel` and `WithDeadline` already panic if `parent` is `nil`, this adds explicit checks to give a useful message in the panic. Fixes #37908 Change-Id: I70fd01f6539c1b0da0e775fc5457e32e7075e52c GitHub-Last-Rev: 1b7dadd7db9ba42952644ad5e9a49591d6a5191f GitHub-Pull-Request: golang/go#37898 Reviewed-on: https://go-review.googlesource.com/c/go/+/223777 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-03-13context: deflake time-sensitive testsBryan C. Mills
Many of tests in this package assumed reasonable scheduling latency. Unfortunately, scheduling latency on builders and CI systems is not always reasonable. Rather than expecting that a timeout is detected within a fixed short interval, we can use (*testing.T).Deadline to portably scale the time we're willing to wait to something appropriate to the builder. Some of the tests also included arbitrary-duration sleeps, which are no longer needed after CL 196521; we can remove those instead of extending them. Promptness of timeouts is also an important property, but testing that property is better suited to benchmarks than to tests proper: unlike tests, we generally expect benchmarks to be run in a quiet, low-contention environment. Fixes #13956 Change-Id: I0797e2267fb778c8ad94add56d797de9e2c885e5 Reviewed-on: https://go-review.googlesource.com/c/go/+/223019 Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-10-29context: mark testDeadline as a testing helper methodIan Lance Taylor
Change-Id: Ie6fc3e9789aea6e5949e66186db6f2b071b6fdff Reviewed-on: https://go-review.googlesource.com/c/go/+/204037 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-09-26context: use fewer goroutines in WithCancel/WithTimeoutRuss Cox
If the parent context passed to WithCancel or WithTimeout is a known context implementation (one created by this package), we attach the child to the parent by editing data structures directly; otherwise, for unknown parent implementations, we make a goroutine that watches for the parent to finish and propagates the cancellation. A common problem with this scheme, before this CL, is that users who write custom context implementations to manage their value sets cause WithCancel/WithTimeout to start goroutines that would have not been started before. This CL changes the way we map a parent context back to the underlying data structure. Instead of walking up through known context implementations to reach the *cancelCtx, we look up parent.Value(&cancelCtxKey) to return the innermost *cancelCtx, which we use if it matches parent.Done(). This way, a custom context implementation wrapping a *cancelCtx but not changing Done-ness (and not refusing to return wrapped keys) will not require a goroutine anymore in WithCancel/WithTimeout. For #28728. Change-Id: Idba2f435c81b19fe38d0dbf308458ca87c7381e9 Reviewed-on: https://go-review.googlesource.com/c/go/+/196521 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-09-18context: mention asynchronous cancellation propagationEgon Elbre
Fixes #33185 Change-Id: I0adcffa5d1c9e55ae52309c59f961b0710166098 Reviewed-on: https://go-review.googlesource.com/c/go/+/187921 Reviewed-by: Sameer Ajmani <sameer@golang.org> Run-TryBot: Sameer Ajmani <sameer@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-08-02all: remove os.ErrTimeoutDamien Neil
It is unclear whether the current definition of os.IsTimeout is desirable or not. Drop ErrTimeout for now so we can consider adding it (or some other error) in a future release with a corrected definition. Fixes #33411 Change-Id: I8b880da7d22afc343a08339eb5f0efd1075ecafe Reviewed-on: https://go-review.googlesource.com/c/go/+/188758 Reviewed-by: Russ Cox <rsc@golang.org> Run-TryBot: Damien Neil <dneil@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-08-01all: remove os.ErrTemporaryDamien Neil
As discussed in https://github.com/golang/go/issues/32463#issuecomment-506833421 the classification of deadline-based timeouts as "temporary" errors is a historical accident. I/O timeouts used to be duration-based, so they really were temporary--retrying a timed-out operation could succeed. Now that they're deadline-based, timeouts aren't temporary unless you reset the deadline. Drop ErrTemporary from Go 1.13, since its definition is wrong. We'll consider putting it back in Go 1.14 with a clear definition and deprecate net.OpError.Temporary. Fixes #32463 Change-Id: I70cda664590d8872541e17409a5780da76920891 Reviewed-on: https://go-review.googlesource.com/c/go/+/188398 Reviewed-by: Jonathan Amsterdam <jba@google.com>
2019-05-20context: document CancelFunc to be safe for simultaneous use by multiple ↵Alex Myasoedov
goroutines Fixes #32145 Change-Id: If4c9dd3a2af748974141ad6e571f80efcbaad772 Reviewed-on: https://go-review.googlesource.com/c/go/+/177899 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-05-04all: add Unwrap and Is methods to various error typesDamien Neil
Add Unwrap methods to types which wrap an underlying error: "encodinc/csv".ParseError "encoding/json".MarshalerError "net/http".transportReadFromServerError "net".OpError "net".DNSConfigError "net/url".Error "os/exec".Error "signal/internal/pty".PtyError "text/template".ExecError Add os.ErrTemporary. A case could be made for putting this error value in package net, since no exported error types in package os include a Temporary method. However, syscall errors returned from the os package do include this method. Add Is methods to error types with a Timeout or Temporary method, making errors.Is(err, os.Err{Timeout,Temporary}) equivalent to testing the corresponding method: "context".DeadlineExceeded "internal/poll".TimeoutError "net".adrinfoErrno "net".OpError "net".DNSError "net/http".httpError "net/http".tlsHandshakeTimeoutError "net/pipe".timeoutError "net/url".Error Updates #30322 Updates #29934 Change-Id: I409fb20c072ea39116ebfb8c7534d493483870dc Reviewed-on: https://go-review.googlesource.com/c/go/+/170037 Run-TryBot: Damien Neil <dneil@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
2019-04-16all: s/cancelation/cancellation/Josh Bleecher Snyder
Though there is variation in the spelling of canceled, cancellation is always spelled with a double l. Reference: https://www.grammarly.com/blog/canceled-vs-cancelled/ Change-Id: I240f1a297776c8e27e74f3eca566d2bc4c856f2f Reviewed-on: https://go-review.googlesource.com/c/go/+/170060 Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-04-16context: simplify stringify with a type switch喜欢兰花山丘
Minor style change. Change-Id: Ib30243a71a83de1a67d3d005bfdd1e04265fca1e GitHub-Last-Rev: 9d654de10eaa6f01ece29790fb81bc41dfd61eaf GitHub-Pull-Request: golang/go#31479 Reviewed-on: https://go-review.googlesource.com/c/go/+/172199 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Sameer Ajmani <sameer@golang.org>
2019-03-27context: don't depend on fmtBrad Fitzpatrick
So the net package doesn't indirectly depend on unicode tables. But we're still not quite there, because a new test added in this CL reveals that we still have a path to unicode via: deps_test.go:570: TODO(issue 30440): policy violation: net => sort => reflect => unicode Updates #30440 Change-Id: I710c2061dfbaa8e866c92e6c824bd8df35784165 Reviewed-on: https://go-review.googlesource.com/c/go/+/169080 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-03-25context: remove dependency on reflectMichael Fraenkel
Make context depend on reflectlite instead of reflect in effort to eventually make net no longer depend on unicode tables. With this CL we're down to just: net -> context -> fmt -> unicode tables The next CL can remove context -> fmt. Updates #30440 Change-Id: I7f5df15f975d9dc862c59aa8477c1cfd6ff4967e Reviewed-on: https://go-review.googlesource.com/c/go/+/164239 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-10-03context: avoid duplicate removeChildWeerasak Chongnguluam
When deadline has already passed, current context is canceled before return cancel function. So is unnecessary to call cancel with remove from parent again in return cancel function. Change-Id: I37c687c57a29d9f139c7fb648ce7de69093ed623 Reviewed-on: https://go-review.googlesource.com/c/50410 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-08-22context: don't talk about tools that don't existDominik Honnef
This comment has been the source of much confusion and broken dreams. We can add it back if a tool ever gets released. Updates #16742 Change-Id: I4b9c179b7c60274e6ff1bcb607b82029dd9a893f Reviewed-on: https://go-review.googlesource.com/130876 Reviewed-by: Matt Layher <mdlayher@gmail.com> Reviewed-by: Alan Donovan <adonovan@google.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-06-12context: add docs to ExampleWithValueIoannis Georgoulas
Change-Id: I3a83c63f4db2e46fd96f373378a429896e93f9d1 Reviewed-on: https://go-review.googlesource.com/48861 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-05-29context: add benchmarks for context cancellationCarl Mastrangelo
Change-Id: I539c9226eb7e493b52c50e1e431954567d43bcfb Reviewed-on: https://go-review.googlesource.com/100847 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-04-15context: avoid defer in the cancelCtx.Err methodKeegan Carruthers-Smith
name old time/op new time/op delta CheckCanceled/Err-4 53.5ns ± 2% 20.8ns ± 0% -61.05% (p=0.008 n=5+5) CheckCanceled/Done-4 44.4ns ± 1% 44.5ns ± 0% ~ (p=0.889 n=5+5) Change-Id: I2c68700a2b33f8feb3d307ce7c966590a3e960af Reviewed-on: https://go-review.googlesource.com/107137 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-09-21context: fix references to "d" in WithDeadline docsMichael Darakananda
Docs of WithDeadline refers to variable "d" which does not exist in the docs. This commit renames the time argument to "d" to make the doc work. Change-Id: Ifd2c1be7d2e3f7dfb21cd9bb8ff7fc5039c8d3bd Reviewed-on: https://go-review.googlesource.com/65130 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2017-09-15all: fix article typosKunpei Sakai
a -> an Change-Id: I7362bdc199e83073a712be657f5d9ba16df3077e Reviewed-on: https://go-review.googlesource.com/63850 Reviewed-by: Rob Pike <r@golang.org>
2017-09-12runtime: improve timers scalability on multi-CPU systemsAliaksandr Valialkin
Use per-P timers, so each P may work with its own timers. This CL improves performance on multi-CPU systems in the following cases: - When serving high number of concurrent connections with read/write deadlines set (for instance, highly loaded net/http server). - When using high number of concurrent timers. These timers may be implicitly created via context.WithDeadline or context.WithTimeout. Production servers should usually set timeout on connections and external requests in order to prevent from resource leakage. See https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/ Below are relevant benchmark results for various GOMAXPROCS values on linux/amd64: context package: name old time/op new time/op delta WithTimeout/concurrency=40 4.92µs ± 0% 5.17µs ± 1% +5.07% (p=0.000 n=9+9) WithTimeout/concurrency=4000 6.03µs ± 1% 6.49µs ± 0% +7.63% (p=0.000 n=8+10) WithTimeout/concurrency=400000 8.58µs ± 7% 9.02µs ± 4% +5.02% (p=0.019 n=10+10) name old time/op new time/op delta WithTimeout/concurrency=40-2 3.70µs ± 1% 2.78µs ± 4% -24.90% (p=0.000 n=8+9) WithTimeout/concurrency=4000-2 4.49µs ± 4% 3.67µs ± 5% -18.26% (p=0.000 n=10+10) WithTimeout/concurrency=400000-2 6.16µs ±10% 5.15µs ±13% -16.30% (p=0.000 n=10+10) name old time/op new time/op delta WithTimeout/concurrency=40-4 3.58µs ± 1% 2.64µs ± 2% -26.13% (p=0.000 n=9+10) WithTimeout/concurrency=4000-4 4.17µs ± 0% 3.32µs ± 1% -20.36% (p=0.000 n=10+10) WithTimeout/concurrency=400000-4 5.57µs ± 9% 4.83µs ±10% -13.27% (p=0.001 n=10+10) time package: name old time/op new time/op delta AfterFunc 6.15ms ± 3% 6.07ms ± 2% ~ (p=0.133 n=10+9) AfterFunc-2 3.43ms ± 1% 3.56ms ± 1% +3.91% (p=0.000 n=10+9) AfterFunc-4 5.04ms ± 2% 2.36ms ± 0% -53.20% (p=0.000 n=10+9) After 6.54ms ± 2% 6.49ms ± 3% ~ (p=0.393 n=10+10) After-2 3.68ms ± 1% 3.87ms ± 0% +5.14% (p=0.000 n=9+9) After-4 6.66ms ± 1% 2.87ms ± 1% -56.89% (p=0.000 n=10+10) Stop 698µs ± 2% 689µs ± 1% -1.26% (p=0.011 n=10+10) Stop-2 729µs ± 2% 434µs ± 3% -40.49% (p=0.000 n=10+10) Stop-4 837µs ± 3% 333µs ± 2% -60.20% (p=0.000 n=10+10) SimultaneousAfterFunc 694µs ± 1% 692µs ± 7% ~ (p=0.481 n=10+10) SimultaneousAfterFunc-2 714µs ± 3% 569µs ± 2% -20.33% (p=0.000 n=10+10) SimultaneousAfterFunc-4 782µs ± 2% 386µs ± 2% -50.67% (p=0.000 n=10+10) StartStop 267µs ± 3% 274µs ± 0% +2.64% (p=0.000 n=8+9) StartStop-2 238µs ± 2% 140µs ± 3% -40.95% (p=0.000 n=10+8) StartStop-4 320µs ± 1% 125µs ± 1% -61.02% (p=0.000 n=9+9) Reset 75.0µs ± 1% 77.5µs ± 2% +3.38% (p=0.000 n=10+10) Reset-2 150µs ± 2% 40µs ± 5% -73.09% (p=0.000 n=10+9) Reset-4 226µs ± 1% 33µs ± 1% -85.42% (p=0.000 n=10+10) Sleep 857µs ± 6% 878µs ± 9% ~ (p=0.079 n=10+9) Sleep-2 617µs ± 4% 585µs ± 2% -5.21% (p=0.000 n=10+10) Sleep-4 689µs ± 3% 465µs ± 4% -32.53% (p=0.000 n=10+10) Ticker 55.9ms ± 2% 55.9ms ± 2% ~ (p=0.971 n=10+10) Ticker-2 28.7ms ± 2% 28.1ms ± 1% -2.06% (p=0.000 n=10+10) Ticker-4 14.6ms ± 0% 13.6ms ± 1% -6.80% (p=0.000 n=9+10) Fixes #15133 Change-Id: I6f4b09d2db8c5bec93146db6501b44dbfe5c0ac4 Reviewed-on: https://go-review.googlesource.com/34784 Reviewed-by: Austin Clements <austin@google.com>
2017-08-30context: fix lint warning “drop = 0 from declaration”Michael Stapelberg
Previously, the suggested code would result in the following golint warning: “should drop = 0 from declaration of var errorsOnlyKey; it is the zero value” Change-Id: I1a302c1e40ca89acbc76897e39097ecd04865460 Reviewed-on: https://go-review.googlesource.com/60290 Reviewed-by: Ian Lance Taylor <iant@golang.org>
2017-04-26context: define behavior for Err before Done is closedRuss Cox
The Context definition to date has not defined what Err returns before the Done channel is closed. Define that it returns nil, as most implementations do. All the standard context implementations (those in package context and in golang.org/x/net/context) return Err() == nil when Done is not yet closed. However, some non-standard implementations may exist that return Err() != nil in this case, as permitted by the Context definition before this date. Call these "errorful implementations". Because all the standard context implementations ensure that Err() == nil when Done is not yet closed, clients now exist that assume Err() != nil implies Done is closed and use calling Err as a quick short-circuit check instead of first doing a non-blocking receive from Done and then, if that succeeds, needing to call Err. This assumption holds for all the standard Context implementations, so these clients work fine in practice, even though they are making unwarranted assumptions about the Context implementations. Call these "technically incorrect clients". If a technically incorrect client encounters an errorful implementation, the client misbehaves. Because there are few errorful implementations, over time we expect that many clients will end up being technically incorrect without realizing it, leading to latent, subtle bugs. If we want to eliminate these latent, subtle bugs, there are two ways to do this: either make errorful implementations more common (exposing the client bugs more often) or redefine the Context interface so that the clients are not buggy after all. If we make errorful implementations more common, such as by changing the standard context implementations to return ErrNotDone instead of nil when Err is called before Done is closed, this will shake out essentially all of the technically incorrect clients, forcing people to find and fix those clients during the transition to Go 1.9. Technically this is allowed by the compatibility policy, but we expect there are many pieces of code assuming that Err() != nil means done, so updating will cause real pain. If instead we disallow errorful implementations, then they will need to be fixed as they are discovered, but the fault will officially lie in the errorful Context implementation, not in the clients. Technically this is disallowed by the compatibility policy, because these errorful implementations were "correct" in earlier versions of Go, except that they didn't work with common client code. We expect there are hardly any errorful implementations, so that disallowing them will be less disruptive and more in the spirit of the compatibility policy. This CL takes the path of expected least disruption, narrowing the Context interface semantics and potentially invalidating existing implementations. A survey of the go-corpus v0.01 turned up only five Context implementations, all trivial and none errorful (details in #19856). We are aware of one early Context implementation inside Google, from before even golang.org/x/net/context existed, that is errorful. The misbehavior of an open-source library when passed such a context is what prompted #19856. That context implementation would be disallowed after this CL and would need to be corrected. We are aware of no other affected context implementations. On the other hand, a survey of the go-corpus v0.01 turned up many instances of client code assuming that Err() == nil implies not done yet (details also in #19856). On balance, narrowing Context and thereby allowing Err() == nil checks should invalidate significantly less code than a push to flush out all the currently technically incorrect Err() == nil checks. If release feedback shows that we're wrong about this balance, we can roll back this CL and try again in Go 1.10. Fixes #19856. Change-Id: Id45d126fac70e1fcc42d73e5a87ca1b66935b831 Reviewed-on: https://go-review.googlesource.com/40291 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Sameer Ajmani <sameer@golang.org>
2017-04-03all: fix minor misspellingsEric Lagergren
Change-Id: I1f1cfb161640eb8756fb1a283892d06b30b7a8fa Reviewed-on: https://go-review.googlesource.com/39356 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-02-22context: document that Err is unspecified before DoneIan Lance Taylor
It could have been defined the other way, but since the behavior has been unspecified, this is the conservative approach for people writing different implementations of the Context interface. Change-Id: I7334a4c674bc2330cca6874f7cac1eb0eaea3cff Reviewed-on: https://go-review.googlesource.com/37375 Reviewed-by: Matt Layher <mdlayher@gmail.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Sameer Ajmani <sameer@golang.org> Run-TryBot: Sameer Ajmani <sameer@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-02-01context: lazily initialize cancelCtx done channelJosh Bleecher Snyder
This CL reduces allocations when a context created with WithCancel either (1) never has its Done channel used or (2) gets cancelled before its Done channel is used This is not uncommon. Many contexts are created for tasks that end up not using them. name old time/op new time/op delta ContextCancelTree/depth=1/Root=Background-8 112ns ± 2% 74ns ± 1% -34.03% (p=0.000 n=17+18) ContextCancelTree/depth=1/Root=OpenCanceler-8 601ns ± 3% 544ns ± 1% -9.56% (p=0.000 n=20+20) ContextCancelTree/depth=1/Root=ClosedCanceler-8 367ns ± 4% 257ns ± 1% -30.01% (p=0.000 n=20+20) ContextCancelTree/depth=10/Root=Background-8 2.91µs ± 2% 2.87µs ± 0% -1.38% (p=0.000 n=20+18) ContextCancelTree/depth=10/Root=OpenCanceler-8 4.36µs ± 2% 4.26µs ± 1% -2.34% (p=0.000 n=20+18) ContextCancelTree/depth=10/Root=ClosedCanceler-8 2.02µs ± 2% 1.51µs ± 1% -25.18% (p=0.000 n=19+19) ContextCancelTree/depth=100/Root=Background-8 30.5µs ± 6% 30.5µs ± 1% ~ (p=0.941 n=20+20) ContextCancelTree/depth=100/Root=OpenCanceler-8 39.8µs ± 1% 41.1µs ± 1% +3.15% (p=0.000 n=18+19) ContextCancelTree/depth=100/Root=ClosedCanceler-8 17.8µs ± 1% 13.9µs ± 1% -21.61% (p=0.000 n=18+20) ContextCancelTree/depth=1000/Root=Background-8 302µs ± 1% 313µs ± 0% +3.62% (p=0.000 n=20+18) ContextCancelTree/depth=1000/Root=OpenCanceler-8 412µs ± 2% 427µs ± 1% +3.55% (p=0.000 n=18+19) ContextCancelTree/depth=1000/Root=ClosedCanceler-8 178µs ± 1% 139µs ± 1% -21.80% (p=0.000 n=19+17) name old alloc/op new alloc/op delta ContextCancelTree/depth=1/Root=Background-8 176B ± 0% 80B ± 0% -54.55% (p=0.000 n=20+20) ContextCancelTree/depth=1/Root=OpenCanceler-8 544B ± 0% 448B ± 0% -17.65% (p=0.000 n=20+20) ContextCancelTree/depth=1/Root=ClosedCanceler-8 352B ± 0% 160B ± 0% -54.55% (p=0.000 n=20+20) ContextCancelTree/depth=10/Root=Background-8 3.49kB ± 0% 3.39kB ± 0% -2.75% (p=0.000 n=20+20) ContextCancelTree/depth=10/Root=OpenCanceler-8 3.86kB ± 0% 3.76kB ± 0% -2.49% (p=0.000 n=20+20) ContextCancelTree/depth=10/Root=ClosedCanceler-8 1.94kB ± 0% 0.88kB ± 0% -54.55% (p=0.000 n=20+20) ContextCancelTree/depth=100/Root=Background-8 36.6kB ± 0% 36.5kB ± 0% -0.26% (p=0.000 n=20+20) ContextCancelTree/depth=100/Root=OpenCanceler-8 37.0kB ± 0% 36.9kB ± 0% -0.26% (p=0.000 n=20+20) ContextCancelTree/depth=100/Root=ClosedCanceler-8 17.8kB ± 0% 8.1kB ± 0% -54.55% (p=0.000 n=20+20) ContextCancelTree/depth=1000/Root=Background-8 368kB ± 0% 368kB ± 0% -0.03% (p=0.000 n=20+20) ContextCancelTree/depth=1000/Root=OpenCanceler-8 368kB ± 0% 368kB ± 0% -0.03% (p=0.000 n=20+20) ContextCancelTree/depth=1000/Root=ClosedCanceler-8 176kB ± 0% 80kB ± 0% -54.55% (p=0.000 n=20+20) name old allocs/op new allocs/op delta ContextCancelTree/depth=1/Root=Background-8 3.00 ± 0% 2.00 ± 0% -33.33% (p=0.000 n=20+20) ContextCancelTree/depth=1/Root=OpenCanceler-8 8.00 ± 0% 7.00 ± 0% -12.50% (p=0.000 n=20+20) ContextCancelTree/depth=1/Root=ClosedCanceler-8 6.00 ± 0% 4.00 ± 0% -33.33% (p=0.000 n=20+20) ContextCancelTree/depth=10/Root=Background-8 48.0 ± 0% 47.0 ± 0% -2.08% (p=0.000 n=20+20) ContextCancelTree/depth=10/Root=OpenCanceler-8 53.0 ± 0% 52.0 ± 0% -1.89% (p=0.000 n=20+20) ContextCancelTree/depth=10/Root=ClosedCanceler-8 33.0 ± 0% 22.0 ± 0% -33.33% (p=0.000 n=20+20) ContextCancelTree/depth=100/Root=Background-8 498 ± 0% 497 ± 0% -0.20% (p=0.000 n=20+20) ContextCancelTree/depth=100/Root=OpenCanceler-8 503 ± 0% 502 ± 0% -0.20% (p=0.000 n=20+20) ContextCancelTree/depth=100/Root=ClosedCanceler-8 303 ± 0% 202 ± 0% -33.33% (p=0.000 n=20+20) ContextCancelTree/depth=1000/Root=Background-8 5.00k ± 0% 5.00k ± 0% -0.02% (p=0.000 n=20+20) ContextCancelTree/depth=1000/Root=OpenCanceler-8 5.00k ± 0% 5.00k ± 0% -0.02% (p=0.000 n=20+20) ContextCancelTree/depth=1000/Root=ClosedCanceler-8 3.00k ± 0% 2.00k ± 0% -33.33% (p=0.000 n=20+20) Change-Id: Ibd7a0c3d5c847861cf1497f8fead34329413d26d Reviewed-on: https://go-review.googlesource.com/34979 Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Sameer Ajmani <sameer@golang.org>
2016-12-08all: make spelling consistentBrad Fitzpatrick
Fixes #17938 Change-Id: Iad12155f4976846bd4a9a53869f89e40e5b3deb3 Reviewed-on: https://go-review.googlesource.com/34147 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2016-11-12context: document appropriate WithValue key type moreBrad Fitzpatrick
Fixes #17826 Updates #17302 Change-Id: I7c1ebd965e679e7169a97e62d27ae3ede2473aa1 Reviewed-on: https://go-review.googlesource.com/33152 Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-11-03context: adjust tests to avoid importing "testing" in package contextRuss Cox
So that testing can use context in its public API. For #16221. Change-Id: I6263fa7266c336c9490f20164ce79336df44a57e Reviewed-on: https://go-review.googlesource.com/32648 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-10-26context: add comments to the WithCancel example, apply minor improvementsJaana Burcu Dogan
Fixes #17534. Change-Id: I28af74b287a5a09d5f6607a012f3d5d133b04ed2 Reviewed-on: https://go-review.googlesource.com/32017 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-10-05context: make DeadlineExceeded implement net.ErrorRuss Cox
It already implemented the Timeout method, but implementing the full net.Error is more convenient. Fixes #14238 (again). Change-Id: Ia87f897f0f35bcb49865e2355964049227951ca6 Reviewed-on: https://go-review.googlesource.com/30370 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-09-30context: discourage use of basic types as keys in WithValueMatt Layher
Fixes #17302 Change-Id: I375d5d4f2714ff415542f4fe56a548e53c5e8ba6 Reviewed-on: https://go-review.googlesource.com/30134 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-09-09context: add examplesCarlos C
Add function level examples to the package. Partially addresses #16360 Change-Id: I7162aed4e4a969743c19b79c9ffaf9217d2c1c08 Reviewed-on: https://go-review.googlesource.com/26930 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-09-09context: reduce memory usage of context treeJack Lindamood
Modifies context package to use map[]struct{} rather than map[]bool, since the map is intended as a set object. Also adds Benchmarks to the context package switching between different types of root nodes and a tree with different depths. Included below are bytes deltas between the old and new code, using these benchmarks. benchmark old bytes new bytes delta BenchmarkContextCancelTree/depth=1/Root=Background-8 176 176 +0.00% BenchmarkContextCancelTree/depth=1/Root=OpenCanceler-8 560 544 -2.86% BenchmarkContextCancelTree/depth=1/Root=ClosedCanceler-8 352 352 +0.00% BenchmarkContextCancelTree/depth=10/Root=Background-8 3632 3488 -3.96% BenchmarkContextCancelTree/depth=10/Root=OpenCanceler-8 4016 3856 -3.98% BenchmarkContextCancelTree/depth=10/Root=ClosedCanceler-8 1936 1936 +0.00% BenchmarkContextCancelTree/depth=100/Root=Background-8 38192 36608 -4.15% BenchmarkContextCancelTree/depth=100/Root=OpenCanceler-8 38576 36976 -4.15% BenchmarkContextCancelTree/depth=100/Root=ClosedCanceler-8 17776 17776 +0.00% BenchmarkContextCancelTree/depth=1000/Root=Background-8 383792 367808 -4.16% BenchmarkContextCancelTree/depth=1000/Root=OpenCanceler-8 384176 368176 -4.16% BenchmarkContextCancelTree/depth=1000/Root=ClosedCanceler-8 176176 176176 +0.00% Change-Id: I699ad704d9f7b461214e1651d24941927315b525 Reviewed-on: https://go-review.googlesource.com/25270 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
2016-08-30all: use time.Until where applicableBrad Fitzpatrick
Updates #14595 Change-Id: Idf60b3004c7a0ebb59dd48389ab62c854069e09f Reviewed-on: https://go-review.googlesource.com/28073 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Andrew Gerrand <adg@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-08-19context: test WithCancel with canceled parentJaana Burcu Dogan
Change-Id: I32079cc12cfffb8520f0073a8b5119705dc0cd1b Reviewed-on: https://go-review.googlesource.com/27401 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-07-26context: add test for WithDeadline in the pastJack Lindamood
Adds a test case for calling context.WithDeadline() where the deadline exists in the past. This change increases the code coverage of the context package. Change-Id: Ib486bf6157e779fafd9dab2b7364cdb5a06be36e Reviewed-on: https://go-review.googlesource.com/25007 Reviewed-by: Sameer Ajmani <sameer@golang.org> Run-TryBot: Sameer Ajmani <sameer@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-06-30context: cancel the context in ExampleWithTimeout, with explanationAlan Donovan
Fixes #16230 Change-Id: Ibb10234a6c3ab8bd0cfd93c2ebe8cfa66f80f6b0 Reviewed-on: https://go-review.googlesource.com/24682 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-06-24context: update documentation on cancelation and go vet check.Sameer Ajmani
Also replace double spaces after periods with single spaces. Change-Id: Iedaea47595c5ce64e7e8aa3a368f36d49061c555 Reviewed-on: https://go-review.googlesource.com/24431 Reviewed-by: Alan Donovan <adonovan@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-06-15context: document how to release resources associated with Contexts.Sameer Ajmani
Some users don't realize that creating a Context with a CancelFunc attaches a subtree to the parent, and that that subtree is not released until the CancelFunc is called or the parent is canceled. Make this clear early in the package docs, so that people learning about this package have the right conceptual model. Change-Id: I7c77a546c19c3751dd1f3a5bc827ad106dd1afbf Reviewed-on: https://go-review.googlesource.com/24090 Reviewed-by: Alan Donovan <adonovan@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-06-01context: fix typo in commentsKenny Grant
Change-Id: I41310ec88c889fda79d80eaf4a742a1000284f60 Reviewed-on: https://go-review.googlesource.com/23591 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-05-19context: make DeadlineExceeded have a Timeout methodBrad Fitzpatrick
Fixes #14238 Change-Id: I1538bfb5cfa63e36a89df1f6eb9f5a0dcafb6ce5 Reviewed-on: https://go-review.googlesource.com/23256 Reviewed-by: Dave Cheney <dave@cheney.net> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-05-05context: use https in docsShenghou Ma
Change-Id: I9354712768702e3b083c77f30165a34cb414d686 Reviewed-on: https://go-review.googlesource.com/22810 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>