123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- package context
- import (
- "errors"
- "fmt"
- "sync"
- "time"
- )
- type emptyCtx int
- func (*emptyCtx) Deadline() (deadline time.Time, ok bool) {
- return
- }
- func (*emptyCtx) Done() <-chan struct{} {
- return nil
- }
- func (*emptyCtx) Err() error {
- return nil
- }
- func (*emptyCtx) Value(key interface{}) interface{} {
- return nil
- }
- func (e *emptyCtx) String() string {
- switch e {
- case background:
- return "context.Background"
- case todo:
- return "context.TODO"
- }
- return "unknown empty Context"
- }
- var (
- background = new(emptyCtx)
- todo = new(emptyCtx)
- )
- var Canceled = errors.New("context canceled")
- var DeadlineExceeded = errors.New("context deadline exceeded")
- func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {
- c := newCancelCtx(parent)
- propagateCancel(parent, c)
- return c, func() { c.cancel(true, Canceled) }
- }
- func newCancelCtx(parent Context) *cancelCtx {
- return &cancelCtx{
- Context: parent,
- done: make(chan struct{}),
- }
- }
- func propagateCancel(parent Context, child canceler) {
- if parent.Done() == nil {
- return
- }
- if p, ok := parentCancelCtx(parent); ok {
- p.mu.Lock()
- if p.err != nil {
-
- child.cancel(false, p.err)
- } else {
- if p.children == nil {
- p.children = make(map[canceler]bool)
- }
- p.children[child] = true
- }
- p.mu.Unlock()
- } else {
- go func() {
- select {
- case <-parent.Done():
- child.cancel(false, parent.Err())
- case <-child.Done():
- }
- }()
- }
- }
- func parentCancelCtx(parent Context) (*cancelCtx, bool) {
- for {
- switch c := parent.(type) {
- case *cancelCtx:
- return c, true
- case *timerCtx:
- return c.cancelCtx, true
- case *valueCtx:
- parent = c.Context
- default:
- return nil, false
- }
- }
- }
- func removeChild(parent Context, child canceler) {
- p, ok := parentCancelCtx(parent)
- if !ok {
- return
- }
- p.mu.Lock()
- if p.children != nil {
- delete(p.children, child)
- }
- p.mu.Unlock()
- }
- type canceler interface {
- cancel(removeFromParent bool, err error)
- Done() <-chan struct{}
- }
- type cancelCtx struct {
- Context
- done chan struct{}
- mu sync.Mutex
- children map[canceler]bool
- err error
- }
- func (c *cancelCtx) Done() <-chan struct{} {
- return c.done
- }
- func (c *cancelCtx) Err() error {
- c.mu.Lock()
- defer c.mu.Unlock()
- return c.err
- }
- func (c *cancelCtx) String() string {
- return fmt.Sprintf("%v.WithCancel", c.Context)
- }
- func (c *cancelCtx) cancel(removeFromParent bool, err error) {
- if err == nil {
- panic("context: internal error: missing cancel error")
- }
- c.mu.Lock()
- if c.err != nil {
- c.mu.Unlock()
- return
- }
- c.err = err
- close(c.done)
- for child := range c.children {
-
- child.cancel(false, err)
- }
- c.children = nil
- c.mu.Unlock()
- if removeFromParent {
- removeChild(c.Context, c)
- }
- }
- func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) {
- if cur, ok := parent.Deadline(); ok && cur.Before(deadline) {
-
- return WithCancel(parent)
- }
- c := &timerCtx{
- cancelCtx: newCancelCtx(parent),
- deadline: deadline,
- }
- propagateCancel(parent, c)
- d := deadline.Sub(time.Now())
- if d <= 0 {
- c.cancel(true, DeadlineExceeded)
- return c, func() { c.cancel(true, Canceled) }
- }
- c.mu.Lock()
- defer c.mu.Unlock()
- if c.err == nil {
- c.timer = time.AfterFunc(d, func() {
- c.cancel(true, DeadlineExceeded)
- })
- }
- return c, func() { c.cancel(true, Canceled) }
- }
- type timerCtx struct {
- *cancelCtx
- timer *time.Timer
- deadline time.Time
- }
- func (c *timerCtx) Deadline() (deadline time.Time, ok bool) {
- return c.deadline, true
- }
- func (c *timerCtx) String() string {
- return fmt.Sprintf("%v.WithDeadline(%s [%s])", c.cancelCtx.Context, c.deadline, c.deadline.Sub(time.Now()))
- }
- func (c *timerCtx) cancel(removeFromParent bool, err error) {
- c.cancelCtx.cancel(false, err)
- if removeFromParent {
-
- removeChild(c.cancelCtx.Context, c)
- }
- c.mu.Lock()
- if c.timer != nil {
- c.timer.Stop()
- c.timer = nil
- }
- c.mu.Unlock()
- }
- func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
- return WithDeadline(parent, time.Now().Add(timeout))
- }
- func WithValue(parent Context, key interface{}, val interface{}) Context {
- return &valueCtx{parent, key, val}
- }
- type valueCtx struct {
- Context
- key, val interface{}
- }
- func (c *valueCtx) String() string {
- return fmt.Sprintf("%v.WithValue(%#v, %#v)", c.Context, c.key, c.val)
- }
- func (c *valueCtx) Value(key interface{}) interface{} {
- if c.key == key {
- return c.val
- }
- return c.Context.Value(key)
- }
|