Documentation
¶
Overview ¶
Package timers exposes efficient data structures for managing timers.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // HighRes is a timeline configured for high resolution timers, with 10 // millisecond accuracy. HighRes = Timeline{ Resolution: 10 * time.Millisecond, } // LowRes is a timeline configured for low resolution timers, with 1 second // accuracy. This timeline is typically useful for network timeouts. // // Here is an example of how the timeline may be used to set a timeout on an // http request: // // req = req.WithContext(timers.LowRes.Timeout(10 * time.Second)) // res, err := httpClient.Do(req) // LowRes = Timeline{ Resolution: 1 * time.Second, } )
Functions ¶
Types ¶
type Timeline ¶
type Timeline struct { // Resolution represents the accuracy of timers managed by this timeline. // The lower the resolution the more accurate the timers are, but it also // means the timeline will put more pressure on the runtime and use more // memory. Resolution time.Duration // Background configures the background context used by contexts created by // the timeline. If nil, the default background context is used instead. Background context.Context // contains filtered or unexported fields }
Timeline is a data structure that maintains a cache of deadlines represented by background contexts. A Timeline has a resolution attribute representing the accuracy of the deadlines it maintains. All deadlines that fall within the same resolution window share the same context, making it very efficient to create thousands, or even millions of them since the runtime only needs to maintain a single timer per resolution window.
Timelines are safe to use concurrently from multiple goroutines, however they should not be copied after being first used.
The zero-value is a valid timeline with a resolution of 100ms.
func (*Timeline) Cancel ¶
func (t *Timeline) Cancel()
Cancel cancels all contexts and releases all internal resources managed by the timeline.
func (*Timeline) Context ¶ added in v1.1.0
Context returns a context which expires when the given deadline is reached, using `now` as the current time.