cubefs
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 context9
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 context18// should be canceled. Deadline returns ok==false when no deadline is19// set. Successive calls to Deadline return the same results.20Deadline() (deadline time.Time, ok bool)21
22// Done returns a channel that's closed when work done on behalf of this23// context should be canceled. Done may return nil if this context can24// 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 deadline28// expires; WithTimeout arranges for Done to be closed when the timeout29// elapses.30//31// Done is provided for use in select statements:32//33// // Stream generates values with DoSomething and sends them to out34// // 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 err40// }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 use50// a Done channel for cancelation.51Done() <-chan struct{}52
53// Err returns a non-nil error value after Done is closed. Err returns54// Canceled if the context was canceled or DeadlineExceeded if the55// context's deadline passed. No other values for Err are defined.56// After Done is closed, successive calls to Err return the same value.57Err() error58
59// Value returns the value associated with this context for key, or nil60// if no value is associated with key. Successive calls to Value with61// the same key returns the same result.62//63// Use context values only for request-scoped data that transits64// processes and API boundaries, not for passing optional parameters to65// functions.66//67// A key identifies a specific value in a Context. Functions that wish68// to store values in Context typically allocate a key in a global69// variable then use that key as the argument to context.WithValue and70// Context.Value. A key can be any type that supports equality;71// packages should define keys as an unexported type to avoid72// collisions.73//74// Packages that define a Context key should provide type-safe accessors75// for the values stores using that key:76//77// // Package user defines a User type that's stored in Contexts.78// package user79//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 int88//89// // userKey is the key for user.User values in Contexts. It is90// // unexported; clients use user.NewContext and user.FromContext91// // instead of using this key directly.92// var userKey key = 093//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, ok103// }104Value(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