1// Copyright 2014 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//go:build !go1.9
6// +build !go1.9
7
8package context
9
10import "time"
11
12// A Context carries a deadline, a cancelation signal, and other values across
13// API boundaries.
14//
15// Context's methods may be called by multiple goroutines simultaneously.
16type Context interface {
17	// Deadline returns the time when work done on behalf of this context
18	// should be canceled. Deadline returns ok==false when no deadline is
19	// set. Successive calls to Deadline return the same results.
20	Deadline() (deadline time.Time, ok bool)
21
22	// Done returns a channel that's closed when work done on behalf of this
23	// context should be canceled. Done may return nil if this context can
24	// never be canceled. Successive calls to Done return the same value.
25	//
26	// WithCancel arranges for Done to be closed when cancel is called;
27	// WithDeadline arranges for Done to be closed when the deadline
28	// expires; WithTimeout arranges for Done to be closed when the timeout
29	// elapses.
30	//
31	// Done is provided for use in select statements:
32	//
33	//  // Stream generates values with DoSomething and sends them to out
34	//  // until DoSomething returns an error or ctx.Done is closed.
35	//  func Stream(ctx context.Context, out chan<- Value) error {
36	//  	for {
37	//  		v, err := DoSomething(ctx)
38	//  		if err != nil {
39	//  			return err
40	//  		}
41	//  		select {
42	//  		case <-ctx.Done():
43	//  			return ctx.Err()
44	//  		case out <- v:
45	//  		}
46	//  	}
47	//  }
48	//
49	// See http://blog.golang.org/pipelines for more examples of how to use
50	// a Done channel for cancelation.
51	Done() <-chan struct{}
52
53	// Err returns a non-nil error value after Done is closed. Err returns
54	// Canceled if the context was canceled or DeadlineExceeded if the
55	// context's deadline passed. No other values for Err are defined.
56	// After Done is closed, successive calls to Err return the same value.
57	Err() error
58
59	// Value returns the value associated with this context for key, or nil
60	// if no value is associated with key. Successive calls to Value with
61	// the same key returns the same result.
62	//
63	// Use context values only for request-scoped data that transits
64	// processes and API boundaries, not for passing optional parameters to
65	// functions.
66	//
67	// A key identifies a specific value in a Context. Functions that wish
68	// to store values in Context typically allocate a key in a global
69	// variable then use that key as the argument to context.WithValue and
70	// Context.Value. A key can be any type that supports equality;
71	// packages should define keys as an unexported type to avoid
72	// collisions.
73	//
74	// Packages that define a Context key should provide type-safe accessors
75	// for the values stores using that key:
76	//
77	// 	// Package user defines a User type that's stored in Contexts.
78	// 	package user
79	//
80	// 	import "golang.org/x/net/context"
81	//
82	// 	// User is the type of value stored in the Contexts.
83	// 	type User struct {...}
84	//
85	// 	// key is an unexported type for keys defined in this package.
86	// 	// This prevents collisions with keys defined in other packages.
87	// 	type key int
88	//
89	// 	// userKey is the key for user.User values in Contexts. It is
90	// 	// unexported; clients use user.NewContext and user.FromContext
91	// 	// instead of using this key directly.
92	// 	var userKey key = 0
93	//
94	// 	// NewContext returns a new Context that carries value u.
95	// 	func NewContext(ctx context.Context, u *User) context.Context {
96	// 		return context.WithValue(ctx, userKey, u)
97	// 	}
98	//
99	// 	// FromContext returns the User value stored in ctx, if any.
100	// 	func FromContext(ctx context.Context) (*User, bool) {
101	// 		u, ok := ctx.Value(userKey).(*User)
102	// 		return u, ok
103	// 	}
104	Value(key interface{}) interface{}
105}
106
107// A CancelFunc tells an operation to abandon its work.
108// A CancelFunc does not wait for the work to stop.
109// After the first call, subsequent calls to a CancelFunc do nothing.
110type CancelFunc func()
111