-
Notifications
You must be signed in to change notification settings - Fork 9
/
isolate.go
85 lines (66 loc) · 1.88 KB
/
isolate.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
package isolate
import (
"io"
"golang.org/x/net/context"
)
type (
decoderInit func(io.Reader) Decoder
dispatcherInit func(context.Context) Dispatcher
Downstream interface {
Reply(code int, args ...interface{}) error
}
ArgsUnpacker interface {
Unpack(in interface{}, out ...interface{}) error
}
Box interface {
Spool(ctx context.Context, name string, opts Profile) error
Spawn(ctx context.Context, opts Profile, name, executable string, args, env map[string]string) (Process, error)
Close() error
}
Process interface {
Output() <-chan ProcessOutput
Kill() error
}
ProcessOutput struct {
Err error
Data []byte
}
Boxes map[string]Box
BoxConfig map[string]interface{}
)
const (
BoxesTag = "isolate.boxes.tag"
downstreamTag = "downstream.tag"
argsUnpackerTag = "args.unpacker.tag"
decoderInitTag = "decoder.init.tag"
)
func NotifyAbouStart(ch chan ProcessOutput) {
ch <- ProcessOutput{Data: []byte(""), Err: nil}
}
func withArgsUnpacker(ctx context.Context, au ArgsUnpacker) context.Context {
return context.WithValue(ctx, argsUnpackerTag, au)
}
func withDecoderInit(ctx context.Context, di decoderInit) context.Context {
return context.WithValue(ctx, decoderInitTag, di)
}
func withDownstream(ctx context.Context, dw Downstream) context.Context {
return context.WithValue(ctx, downstreamTag, dw)
}
func getBoxes(ctx context.Context) Boxes {
val := ctx.Value(BoxesTag)
box, ok := val.(Boxes)
if !ok {
panic("context.Context does not contain Box")
}
return box
}
func reply(ctx context.Context, code int, args ...interface{}) error {
downstream, ok := ctx.Value(downstreamTag).(Downstream)
if !ok {
panic("context.Context does not contain downstream")
}
return downstream.Reply(code, args...)
}
func unpackArgs(ctx context.Context, in interface{}, out ...interface{}) error {
return ctx.Value(argsUnpackerTag).(ArgsUnpacker).Unpack(in, out...)
}