Source File
context.go
Belonging Package
context
package context
import (
)
Done() <-chan struct{}
Err() error
Value(key interface{}) interface{}
}
var DeadlineExceeded error = deadlineExceededError{}
type deadlineExceededError struct{}
func (deadlineExceededError) () string { return "context deadline exceeded" }
func (deadlineExceededError) () bool { return true }
func (deadlineExceededError) () bool { return true }
type emptyCtx int
func (*emptyCtx) () ( time.Time, bool) {
return
}
func (*emptyCtx) () <-chan struct{} {
return nil
}
func (*emptyCtx) () error {
return nil
}
func (*emptyCtx) ( interface{}) interface{} {
return nil
}
func ( *emptyCtx) () string {
switch {
case background:
return "context.Background"
case todo:
return "context.TODO"
}
return "unknown empty Context"
}
var (
background = new(emptyCtx)
todo = new(emptyCtx)
)
func () Context {
return background
}
type CancelFunc func()
func ( Context) ( Context, CancelFunc) {
if == nil {
panic("cannot create context from nil parent")
}
:= newCancelCtx()
propagateCancel(, &)
return &, func() { .cancel(true, Canceled) }
}
var goroutines int32
var cancelCtxKey int
var closedchan = make(chan struct{})
func () {
close(closedchan)
}
type cancelCtx struct {
Context
mu sync.Mutex // protects following fields
done chan struct{} // created lazily, closed by first cancel call
children map[canceler]struct{} // set to nil by the first cancel call
err error // set to non-nil by the first cancel call
}
func ( *cancelCtx) ( interface{}) interface{} {
if == &cancelCtxKey {
return
}
return .Context.Value()
}
func ( *cancelCtx) () <-chan struct{} {
.mu.Lock()
if .done == nil {
.done = make(chan struct{})
}
:= .done
.mu.Unlock()
return
}
func ( *cancelCtx) () error {
.mu.Lock()
:= .err
.mu.Unlock()
return
}
type stringer interface {
String() string
}
func ( Context) string {
if , := .(stringer); {
return .String()
}
return reflectlite.TypeOf().String()
}
func ( *cancelCtx) () string {
return contextName(.Context) + ".WithCancel"
}
return WithCancel()
}
:= &timerCtx{
cancelCtx: newCancelCtx(),
deadline: ,
}
propagateCancel(, )
:= time.Until()
if <= 0 {
.cancel(true, DeadlineExceeded) // deadline has already passed
return , func() { .cancel(false, Canceled) }
}
.mu.Lock()
defer .mu.Unlock()
if .err == nil {
.timer = time.AfterFunc(, func() {
.cancel(true, DeadlineExceeded)
})
}
return , func() { .cancel(true, Canceled) }
}
type timerCtx struct {
cancelCtx
timer *time.Timer // Under cancelCtx.mu.
deadline time.Time
}
func ( *timerCtx) () ( time.Time, bool) {
return .deadline, true
}
func ( *timerCtx) () string {
return contextName(.cancelCtx.Context) + ".WithDeadline(" +
.deadline.String() + " [" +
time.Until(.deadline).String() + "])"
}
func ( *timerCtx) ( bool, error) {
.cancelCtx.cancel(false, )
func ( Context, time.Duration) (Context, CancelFunc) {
return WithDeadline(, time.Now().Add())
}
func ( interface{}) string {
switch s := .(type) {
case stringer:
return .String()
case string:
return
}
return "<not Stringer>"
}
func ( *valueCtx) () string {
return contextName(.Context) + ".WithValue(type " +
reflectlite.TypeOf(.key).String() +
", val " + stringify(.val) + ")"
}
func ( *valueCtx) ( interface{}) interface{} {
if .key == {
return .val
}
return .Context.Value()
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |