blob: 344bd1433450276a19067009a20f0a217106c96f [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001// Copyright 2016 The Go Authors. 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
David K. Bainbridgebd6b2882021-08-26 13:31:02 +00005//go:build go1.7
Zack Williamse940c7a2019-08-21 14:25:39 -07006// +build go1.7
7
8package context
9
10import (
11 "context" // standard library's context, as of Go 1.7
12 "time"
13)
14
15var (
16 todo = context.TODO()
17 background = context.Background()
18)
19
20// Canceled is the error returned by Context.Err when the context is canceled.
21var Canceled = context.Canceled
22
23// DeadlineExceeded is the error returned by Context.Err when the context's
24// deadline passes.
25var DeadlineExceeded = context.DeadlineExceeded
26
27// WithCancel returns a copy of parent with a new Done channel. The returned
28// context's Done channel is closed when the returned cancel function is called
29// or when the parent context's Done channel is closed, whichever happens first.
30//
31// Canceling this context releases resources associated with it, so code should
32// call cancel as soon as the operations running in this Context complete.
33func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {
34 ctx, f := context.WithCancel(parent)
35 return ctx, CancelFunc(f)
36}
37
38// WithDeadline returns a copy of the parent context with the deadline adjusted
39// to be no later than d. If the parent's deadline is already earlier than d,
40// WithDeadline(parent, d) is semantically equivalent to parent. The returned
41// context's Done channel is closed when the deadline expires, when the returned
42// cancel function is called, or when the parent context's Done channel is
43// closed, whichever happens first.
44//
45// Canceling this context releases resources associated with it, so code should
46// call cancel as soon as the operations running in this Context complete.
47func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) {
48 ctx, f := context.WithDeadline(parent, deadline)
49 return ctx, CancelFunc(f)
50}
51
52// WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)).
53//
54// Canceling this context releases resources associated with it, so code should
55// call cancel as soon as the operations running in this Context complete:
56//
57// func slowOperationWithTimeout(ctx context.Context) (Result, error) {
58// ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
59// defer cancel() // releases resources if slowOperation completes before timeout elapses
60// return slowOperation(ctx)
61// }
62func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
63 return WithDeadline(parent, time.Now().Add(timeout))
64}
65
66// WithValue returns a copy of parent in which the value associated with key is
67// val.
68//
69// Use context Values only for request-scoped data that transits processes and
70// APIs, not for passing optional parameters to functions.
71func WithValue(parent Context, key interface{}, val interface{}) Context {
72 return context.WithValue(parent, key, val)
73}