-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathengine.go
72 lines (66 loc) · 1.6 KB
/
engine.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
package sand
import (
"context"
"io"
)
// Engine represents the command processor for the interpreter.
// The underlying type of the Engine implementation must be a
// hashable type (e.g. int, string, struct) in order for the UI
// to be able to use it. Sadly, this means a type EngineFunc
// can not be used due to funcs not being hashable.
//
type Engine interface {
// Exec should take the given line and execute the corresponding functionality.
Exec(ctx context.Context, line string, ui io.ReadWriter) (status int)
}
// execReq represents the parameters passed to an Engine.Exec call
type execReq struct {
ctx context.Context
line string
ui io.ReadWriter
respCh chan int
}
// exec sends the given line to the backing engine and awaits the results.
// this is a blocking call.
func (ui *UI) exec(ctx context.Context, line string, reqCh chan execReq) int {
req := execReq{
ctx: ctx,
line: line,
ui: ui,
respCh: make(chan int),
}
select {
case <-ctx.Done():
return 0
case reqCh <- req:
}
return <-req.respCh
}
// runEngine provides a container for an engine to run inside.
func runEngine(ctx context.Context, eng Engine, reqChs chan chan execReq) {
defer func() {
close(reqChs)
engines.Lock()
delete(engines.engs, eng)
engines.Unlock()
}()
for {
select {
case <-ctx.Done():
return
case reqCh := <-reqChs:
go func(rc chan execReq) {
for req := range rc {
resp := eng.Exec(req.ctx, req.line, req.ui)
select {
case <-ctx.Done():
close(req.respCh)
return
case req.respCh <- resp:
}
close(req.respCh)
}
}(reqCh)
}
}
}