-
Notifications
You must be signed in to change notification settings - Fork 9
/
initialdispatch.go
156 lines (130 loc) · 3.78 KB
/
initialdispatch.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package isolate
import (
"fmt"
"sync/atomic"
"golang.org/x/net/context"
)
const (
spool = 0
replySpoolOk = 0
replySpoolError = 1
spawn = 1
replySpawnWrite = 0
replySpawnError = 1
replySpawnClose = 2
)
type initialDispatch struct {
ctx context.Context
}
func newInitialDispatch(ctx context.Context) Dispatcher {
return &initialDispatch{ctx: ctx}
}
func (d *initialDispatch) Handle(msg *message) (Dispatcher, error) {
switch msg.Number {
case spool:
return d.onSpool(msg)
case spawn:
return d.onSpawn(msg)
default:
return nil, fmt.Errorf("unknown transition id: %d", msg.Number)
}
}
func (d *initialDispatch) onSpool(msg *message) (Dispatcher, error) {
var (
opts Profile
name string
)
if err := unpackArgs(d.ctx, msg.Args, &opts, &name); err != nil {
GetLogger(d.ctx).WithError(err).Error("unable to unpack a message.args")
reply(d.ctx, replySpawnError, errBadMsg, err.Error())
return nil, err
}
isolateType := opts.Type()
if isolateType == "" {
err := fmt.Errorf("corrupted profile: %v", opts)
GetLogger(d.ctx).Error("unable to detect isolate type from a profile")
reply(d.ctx, replySpawnError, errBadProfile, err.Error())
return nil, err
}
box, ok := getBoxes(d.ctx)[isolateType]
if !ok {
GetLogger(d.ctx).WithField("isolatetype", isolateType).Error("requested isolate type is not available")
err := fmt.Errorf("isolate type %s is not available", isolateType)
reply(d.ctx, replySpawnError, errUnknownIsolate, err.Error())
return nil, err
}
ctx, cancel := context.WithCancel(d.ctx)
go func() {
if err := box.Spool(ctx, name, opts); err != nil {
reply(ctx, replySpoolError, errSpoolingFailed, err.Error())
return
}
// NOTE: make sure that nil is packed as []interface{}
reply(ctx, replySpoolOk, nil)
}()
return newSpoolCancelationDispatch(ctx, cancel), nil
}
func (d *initialDispatch) onSpawn(msg *message) (Dispatcher, error) {
var (
opts Profile
name, executable string
args, env map[string]string
)
if err := unpackArgs(d.ctx, msg.Args, &opts, &name, &executable, &args, &env); err != nil {
GetLogger(d.ctx).WithError(err).Error("unable to unpack a message.args")
reply(d.ctx, replySpawnError, errBadMsg, err.Error())
return nil, err
}
isolateType := opts.Type()
if isolateType == "" {
err := fmt.Errorf("corrupted profile: %v", opts)
GetLogger(d.ctx).Error("unable to detect isolate type from a profile")
reply(d.ctx, replySpawnError, errBadProfile, err.Error())
return nil, err
}
box, ok := getBoxes(d.ctx)[isolateType]
if !ok {
GetLogger(d.ctx).WithField("isolatetype", isolateType).Error("requested isolate type is not available")
err := fmt.Errorf("isolate type %s is not available", isolateType)
reply(d.ctx, replySpawnError, errUnknownIsolate, err.Error())
return nil, err
}
prCh := make(chan Process, 1)
flagKilled := uint32(0)
go func() {
defer close(prCh)
pr, err := box.Spawn(d.ctx, opts, name, executable, args, env)
if err != nil {
GetLogger(d.ctx).WithError(err).Error("unable to spawn")
reply(d.ctx, replySpawnError, errSpawningFailed, err.Error())
return
}
d.trackOutput(pr, &flagKilled)
prCh <- pr
}()
return newSpawnDispatch(d.ctx, prCh, &flagKilled), nil
}
func (d *initialDispatch) trackOutput(pr Process, flagKilled *uint32) {
for {
select {
case output, ok := <-pr.Output():
if !ok {
if atomic.CompareAndSwapUint32(flagKilled, 0, 1) {
reply(d.ctx, replySpawnError, errOutputError, "output has been closed")
}
return
}
if output.Err != nil {
if atomic.CompareAndSwapUint32(flagKilled, 0, 1) {
reply(d.ctx, replySpawnError, errOutputError, output.Err.Error())
}
} else {
if atomic.LoadUint32(flagKilled) == 0 {
reply(d.ctx, replySpawnWrite, output.Data)
}
}
case <-d.ctx.Done():
return
}
}
}