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