1. 程式人生 > >Golang程式設計之context命令使用

Golang程式設計之context命令使用

Package context

import "context"

context包定義了Context上下文型別,它跨API邊界和程序之間承載截止日期、取消訊號和其他請求範圍的值。

對於伺服器的傳入請求應該建立一個Context上下文,而對伺服器的傳出呼叫也應該接受該Context上下文。它們之間的函式呼叫鏈必須傳播Context上下文,可以選擇將其替換為使用WithCancel、WithDeadline、WithTimeout或WithValue建立的派生Context上下文。當一個Context上下文被取消時,從它派生的所有Context上下文也被取消。

WithCancel、WithDeadline和WithTimeout函式接受一個Context上下文(父級)並返回派生Context上下文(子級)和CancelFunc。呼叫CancelFunc將取消子級及其子級,刪除父級對子級的引用,並停止任何關聯的計時器。如果不呼叫CancelFunc,則會洩漏子級及其子級,直到父級被取消或計時器觸發。Go-Vet工具檢查取消功能是否用於所有控制流路徑。

使用Context上下文的程式應該遵循這些規則,以保持包之間的介面一致,並允許靜態分析工具檢查上下文傳播:

不要將上下文儲存在結構型別中;而是將Context上下文顯式傳遞給每個需要它的函式。並且Context上下文應該是第一個引數,通常名為ctx:

func DoSomething(ctx context.Context, arg Arg) error {
	// ... use ctx ...
}

即使函式允許,也不要傳遞nil上下文。如果不確定要使用哪個Context上下文,請傳遞context.TODO。

只對傳輸程序和API的請求範圍資料使用Context上下文值,而不用於向函式傳遞可選引數。

同一Context上下文可以傳遞給在不同goroutine中執行的函式;上下文對於多個goroutine同時使用是安全的。

請參閱 https://blog.golang.org/context ,以獲取使用上下文的伺服器的示例程式碼。

Index

Variables

type CancelFunc

type Context

func Background

func TODO

func WithCancel

func WithDeadline

func WithTimeout

func WithValue

Variables

var Canceled = errors.New("context canceled")

var DeadlineExceeded error = deadlineExceededError{}

type CancelFunc

type CancelFunc func()

type Context

type Context interface {
    // Deadline returns the time when work done on behalf of this context
    // should be canceled. Deadline returns ok==false when no deadline is
    // set. Successive calls to Deadline return the same results.
    Deadline() (deadline time.Time, ok bool)

    // Done returns a channel that's closed when work done on behalf of this
    // context should be canceled. Done may return nil if this context can
    // never be canceled. Successive calls to Done return the same value.
    //
    // WithCancel arranges for Done to be closed when cancel is called;
    // WithDeadline arranges for Done to be closed when the deadline
    // expires; WithTimeout arranges for Done to be closed when the timeout
    // elapses.
    //
    // Done is provided for use in select statements:
    //
    //  // Stream generates values with DoSomething and sends them to out
    //  // until DoSomething returns an error or ctx.Done is closed.
    //  func Stream(ctx context.Context, out chan<- Value) error {
    //  	for {
    //  		v, err := DoSomething(ctx)
    //  		if err != nil {
    //  			return err
    //  		}
    //  		select {
    //  		case <-ctx.Done():
    //  			return ctx.Err()
    //  		case out <- v:
    //  		}
    //  	}
    //  }
    //
    // See https://blog.golang.org/pipelines for more examples of how to use
    // a Done channel for cancelation.
    Done() <-chan struct{}

    // Err returns a non-nil error value after Done is closed. Err returns
    // Canceled if the context was canceled or DeadlineExceeded if the
    // context's deadline passed. No other values for Err are defined.
    // After Done is closed, successive calls to Err return the same value.
    Err() error

    // Value returns the value associated with this context for key, or nil
    // if no value is associated with key. Successive calls to Value with
    // the same key returns the same result.
    //
    // Use context values only for request-scoped data that transits
    // processes and API boundaries, not for passing optional parameters to
    // functions.
    //
    // A key identifies a specific value in a Context. Functions that wish
    // to store values in Context typically allocate a key in a global
    // variable then use that key as the argument to context.WithValue and
    // Context.Value. A key can be any type that supports equality;
    // packages should define keys as an unexported type to avoid
    // collisions.
    //
    // Packages that define a Context key should provide type-safe accessors
    // for the values stored using that key:
    //
    // 	// Package user defines a User type that's stored in Contexts.
    // 	package user
    //
    // 	import "context"
    //
    // 	// User is the type of value stored in the Contexts.
    // 	type User struct {...}
    //
    // 	// key is an unexported type for keys defined in this package.
    // 	// This prevents collisions with keys defined in other packages.
    // 	type key int
    //
    // 	// userKey is the key for user.User values in Contexts. It is
    // 	// unexported; clients use user.NewContext and user.FromContext
    // 	// instead of using this key directly.
    // 	var userKey key = 0
    //
    // 	// NewContext returns a new Context that carries value u.
    // 	func NewContext(ctx context.Context, u *User) context.Context {
    // 		return context.WithValue(ctx, userKey, u)
    // 	}
    //
    // 	// FromContext returns the User value stored in ctx, if any.
    // 	func FromContext(ctx context.Context) (*User, bool) {
    // 		u, ok := ctx.Value(userKey).(*User)
    // 		return u, ok
    // 	}
    Value(key interface{}) interface{}
}

func Background

func Background() Context

func TODO

func TODO() Context

func WithCancel

func WithCancel(parent Context) (ctx Context, cancel CancelFunc)

func WithDeadline

func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc)

func WithTimeout

func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)

func WithValue

func WithValue(parent Context, key, val interf