-
Notifications
You must be signed in to change notification settings - Fork 4
/
controller.go
207 lines (185 loc) · 5.17 KB
/
controller.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package controller
import (
"context"
"sync"
"github.com/aperturerobotics/controllerbus/directive"
"github.com/sirupsen/logrus"
"golang.org/x/exp/slices"
)
// Controller manages running directives and handlers.
type Controller struct {
// ctx is the root context for running handlers & directives
ctx context.Context
// le is the logger for logging events related to the controller
le *logrus.Entry
// mtx guards below fields
mtx sync.Mutex
// dirID contains the next directive instance id
dirID uint32
// dir contains the list of running directive instances
// sorted by ID
dir []*directiveInstance
// hnd contains the list of attached handlers
hnd []*handler
}
// NewController builds a new directive controller.
func NewController(ctx context.Context, le *logrus.Entry) *Controller {
return &Controller{
ctx: ctx,
le: le,
}
}
// GetDirectives returns a list of all currently executing directives.
func (c *Controller) GetDirectives() []directive.Instance {
c.mtx.Lock()
dirs := make([]directive.Instance, len(c.dir))
for i, di := range c.dir {
dirs[i] = di
}
c.mtx.Unlock()
return dirs
}
// AddDirective adds a directive to the controller.
// This call de-duplicates equivalent directives.
//
// cb receives values in order as they are emitted.
// cb can be nil.
// cb should not block.
//
// Returns the instance, new reference, and anynke error.
func (c *Controller) AddDirective(
dir directive.Directive,
ref directive.ReferenceHandler,
) (directive.Instance, directive.Reference, error) {
c.mtx.Lock()
defer c.mtx.Unlock()
// Check if any equivalent directives exist, if applicable.
if eqDir, eqDirOk := dir.(directive.DirectiveWithEquiv); eqDirOk {
for diIdx, di := range c.dir {
if !di.released.Load() && eqDir.IsEquivalent(di.dir) {
// Directive is equivalent to di.
spDir, spDirOk := eqDir.(directive.DirectiveWithSuperceeds)
if spDirOk && spDir.Superceeds(di.dir) {
// Remove the other directive (superceed it)
di.removeLocked(diIdx)
} else {
// Add a reference to the other directive & return.
return di, di.addReferenceLocked(ref, false), nil
}
}
}
}
// Push the new directive to the list.
di, diRef := newDirectiveInstance(c, c.dirID, dir, ref)
c.dirID++
di.logger().Debug("added directive")
c.dir = append(c.dir, di)
// call all handlers while mtx is unlocked
handlers := make([]*handler, len(c.hnd))
copy(handlers, c.hnd)
c.mtx.Unlock()
var resolvers []*resolver
for _, hnd := range handlers {
if !hnd.rel.Load() {
nres, err := di.callHandlerUnlocked(hnd)
if err != nil {
di.logger().
WithError(err).
Warn("directive handler returned error")
continue
}
resolvers = append(resolvers, nres...)
}
}
// attach returned resolvers while mtx is locked
c.mtx.Lock() // note: unlocked in Defer above
di.ready = true
if !di.released.Load() {
for _, res := range resolvers {
di.attachStartResolverLocked(res)
}
}
return di, diRef, nil
}
// AddHandler adds a directive handler.
// The handler will receive calls for all existing directives (initial set).
// An error is returned only if adding the handler failed.
// Returns a function to remove the handler.
// The release function must be non-nil if err is nil, and nil if err != nil.
func (c *Controller) AddHandler(handler directive.Handler) (func(), error) {
c.mtx.Lock()
defer c.mtx.Unlock()
return c.addHandlerLocked(handler)
}
// addHandlersLocked adds a set of handlers while c.mtx is locked.
func (c *Controller) addHandlerLocked(handler directive.Handler) (func(), error) {
hnd := newHandler(handler)
c.hnd = append(c.hnd, hnd)
dirs := make([]*directiveInstance, len(c.dir))
copy(dirs, c.dir)
// unlock temporarily
c.mtx.Unlock()
var resolvers []*resolver
var dis []*directiveInstance
for _, di := range dirs {
if !hnd.rel.Load() {
nres, err := di.callHandlerUnlocked(hnd)
if err != nil {
di.logger().
WithError(err).
Warn("directive handler returned error")
continue
}
if len(nres) != 0 {
resolvers = append(resolvers, nres...)
// We have to use this loop instead of append because the type changes.
oldLen := len(dis)
dis = slices.Grow(dis, len(nres))[:oldLen+len(nres)]
for i := oldLen; i < len(dis); i++ {
dis[i] = di
}
}
}
}
// attach returned resolvers while mtx is locked
c.mtx.Lock()
for i, res := range resolvers {
dis[i].attachStartResolverLocked(res)
}
relHandler := func() {
if !hnd.rel.Swap(true) {
c.mtx.Lock()
c.removeHandlerLocked(hnd)
c.mtx.Unlock()
}
}
return relHandler, nil
}
// removeHandlerLocked removes a handler while c.mtx is locked
//
// returns if the handler was found in the set
func (c *Controller) removeHandlerLocked(hnd *handler) bool {
// mark released
hnd.rel.Store(true)
// remove from list
var found bool
for i, ih := range c.hnd {
if ih == hnd {
c.hnd[i] = c.hnd[len(c.hnd)-1]
c.hnd[len(c.hnd)-1] = nil
c.hnd = c.hnd[:len(c.hnd)-1]
found = true
break
}
}
if !found {
return false
}
// cancel all resolvers associated with the handler
for _, di := range c.dir {
di.removeHandlerLocked(hnd)
}
return true
}
// _ is a type assertion
var _ directive.Controller = ((*Controller)(nil))