-
Notifications
You must be signed in to change notification settings - Fork 4
/
inmem.go
143 lines (128 loc) · 3.53 KB
/
inmem.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
package inmem
import (
"context"
"runtime/debug"
"sync"
"github.com/aperturerobotics/controllerbus/bus"
"github.com/aperturerobotics/controllerbus/controller"
"github.com/aperturerobotics/controllerbus/directive"
"github.com/pkg/errors"
)
// Bus is an in-memory controller bus.
type Bus struct {
// Controller is the directive controller.
directive.Controller
// mtx guards below fields
mtx sync.Mutex
// controllers is the set of attached controllers
controllers []*attachedCtrl
}
// NewBus constructs a new in-memory Bus with a directive controller.
func NewBus(dc directive.Controller) *Bus {
return &Bus{Controller: dc}
}
// GetControllers returns a list of all currently active controllers.
func (b *Bus) GetControllers() []controller.Controller {
b.mtx.Lock()
c := make([]controller.Controller, len(b.controllers))
for i := range b.controllers {
c[i] = b.controllers[i].ctrl
}
b.mtx.Unlock()
return c
}
// AddController adds a controller to the bus and calls Execute().
// Returns a release function for the controller instance.
// Any fatal error in the controller is written to the callback.
// The controller will receive directive callbacks until removed.
// cb can be nil
func (b *Bus) AddController(ctx context.Context, ctrl controller.Controller, cb func(exitErr error)) (func(), error) {
subCtx, subCtxCancel := context.WithCancel(ctx)
relFunc := func() {
subCtxCancel()
b.removeController(ctrl)
}
if err := b.addController(ctrl); err != nil {
subCtxCancel()
_ = ctrl.Close()
return nil, err
}
go func() {
var err error
defer func() {
b.handleControllerPanic(&err)
if err != nil {
subCtxCancel()
b.removeController(ctrl)
if cb != nil {
cb(err)
}
}
}()
err = ctrl.Execute(subCtx)
}()
return relFunc, nil
}
// handleControllerPanic handles recover for a paniced controller.
func (b *Bus) handleControllerPanic(outErr *error) {
if rerr := recover(); rerr != nil {
debug.PrintStack()
e, eOk := rerr.(error)
if eOk {
if outErr != nil {
*outErr = errors.Wrap(e, "controller paniced")
}
} else if outErr != nil && *outErr == nil {
*outErr = errors.New("controller paniced")
}
}
}
// ExecuteController adds a controller to the bus and calls Execute().
// Any fatal error in the controller is returned.
// The controller will receive directive callbacks.
// If the controller returns nil, call RemoveController to remove the controller.
func (b *Bus) ExecuteController(ctx context.Context, c controller.Controller) (err error) {
if err := b.addController(c); err != nil {
return err
}
defer func() {
b.handleControllerPanic(&err)
if err != nil {
b.removeController(c)
}
}()
return c.Execute(ctx)
}
// RemoveController removes the controller from the bus.
func (b *Bus) RemoveController(c controller.Controller) {
b.removeController(c)
}
// addController adds a controller to the bus
func (b *Bus) addController(c controller.Controller) error {
b.mtx.Lock()
rel, err := b.Controller.AddHandler(c)
if err == nil {
b.controllers = append(b.controllers, &attachedCtrl{
ctrl: c,
rel: rel,
})
}
b.mtx.Unlock()
return err
}
// removeController removes a controller from the bus
func (b *Bus) removeController(c controller.Controller) {
b.mtx.Lock()
for i, ci := range b.controllers {
if ci.ctrl == c {
b.controllers[i] = b.controllers[len(b.controllers)-1]
b.controllers[len(b.controllers)-1] = nil
b.controllers = b.controllers[:len(b.controllers)-1]
ci.rel()
break
}
}
b.mtx.Unlock()
}
// _ is a type assertion
var _ bus.Bus = ((*Bus)(nil))