-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
49 lines (36 loc) · 1.03 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package txetnoc
import (
"context"
"reflect"
"time"
)
var _ context.Context = &parentCtx{}
type parentCtx struct {
children []context.Context
err error
}
// Deadline implements context.Context
func (*parentCtx) Deadline() (deadline time.Time, ok bool) { return }
// Done implements context.Context
func (p *parentCtx) Done() <-chan struct{} {
cases := make([]reflect.SelectCase, len(p.children))
for i, ctx := range p.children {
cases[i] = reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(ctx.Done()),
}
}
i, _, _ := reflect.Select(cases)
ctx := p.children[i]
p.err = ctx.Err()
return ctx.Done()
}
// Err implements context.Context
func (p *parentCtx) Err() error { return p.err }
// Value implements context.Context
func (*parentCtx) Value(key any) any { return nil }
// WithChildren returns a new context that wraps all the children and
// waits on all of them for the first one to be Done.
func WithChildren(children ...context.Context) context.Context {
return &parentCtx{children, nil}
}