-
Notifications
You must be signed in to change notification settings - Fork 9
/
spawndispatch.go
55 lines (47 loc) · 1012 Bytes
/
spawndispatch.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
50
51
52
53
54
55
package isolate
import (
"fmt"
"sync/atomic"
"golang.org/x/net/context"
)
const (
spawnKill = 0
replyKillOk = 0
replyKillError = 1
)
type spawnDispatch struct {
ctx context.Context
killed *uint32
process <-chan Process
}
func newSpawnDispatch(ctx context.Context, prCh <-chan Process, flagKilled *uint32) *spawnDispatch {
return &spawnDispatch{
ctx: ctx,
killed: flagKilled,
process: prCh,
}
}
func (d *spawnDispatch) Handle(msg *message) (Dispatcher, error) {
switch msg.Number {
case spawnKill:
go func() {
select {
case pr, ok := <-d.process:
if ok {
if atomic.CompareAndSwapUint32(d.killed, 0, 1) {
if err := pr.Kill(); err != nil {
reply(d.ctx, replyKillError, errKillError, err.Error())
return
}
reply(d.ctx, replyKillOk, nil)
}
}
case <-d.ctx.Done():
}
}()
// NOTE: do not return an err on purpose
return nil, nil
default:
return nil, fmt.Errorf("unknown transition id: %d", msg.Number)
}
}