-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.go
293 lines (268 loc) · 7.23 KB
/
logic.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package logic
import (
"context"
"sync"
"time"
"github.com/antonio-alexander/go-bludgeon/changes/data"
"github.com/antonio-alexander/go-bludgeon/changes/meta"
"github.com/antonio-alexander/go-bludgeon/internal"
"github.com/antonio-alexander/go-queue/finite"
healthcheckdata "github.com/antonio-alexander/go-bludgeon/healthcheck/data"
healthchecklogic "github.com/antonio-alexander/go-bludgeon/healthcheck/logic"
"github.com/antonio-alexander/go-bludgeon/internal/logger"
goqueue "github.com/antonio-alexander/go-queue"
uuid "github.com/google/uuid"
errors "github.com/pkg/errors"
)
type handler struct {
stopper chan struct{}
queue interface {
goqueue.Enqueuer
goqueue.Dequeuer
goqueue.Event
goqueue.Owner
}
}
type logic struct {
sync.RWMutex
sync.WaitGroup
logger.Logger
meta.Change
meta.Registration
meta.RegistrationChange
ctx context.Context
cancel context.CancelFunc
handlersMux sync.RWMutex
handlers map[string]handler
initialized bool
}
func changeDequeue(queue goqueue.Dequeuer) (*data.Change, bool) {
item, underflow := queue.Dequeue()
if underflow {
return nil, true
}
change, _ := item.(*data.Change)
return change, false
}
// New will generate a new instance of logic that implements
// the interfaces Logic and Owner, from the provided parameters
// we can set the logger and the employee meta (required)
func New() interface {
Logic
healthchecklogic.Logic
internal.Initializer
internal.Parameterizer
} {
return &logic{
handlers: make(map[string]handler),
Logger: logger.NewNullLogger(),
ctx: context.Background(),
}
}
func (l *logic) handlersWrite(stopper chan struct{}, queue interface {
goqueue.Dequeuer
goqueue.Enqueuer
goqueue.Event
goqueue.Owner
}) (handlerId string) {
l.handlersMux.Lock()
defer l.handlersMux.Unlock()
handlerId = uuid.Must(uuid.NewRandom()).String()
l.handlers[handlerId] = handler{
stopper: stopper,
queue: queue,
}
return
}
func (l *logic) handlersRead() (handlers map[string]goqueue.Enqueuer) {
l.handlersMux.RLock()
defer l.handlersMux.RUnlock()
handlers = make(map[string]goqueue.Enqueuer)
for handlerId, handler := range l.handlers {
handlers[handlerId] = handler.queue
}
return
}
func (l *logic) handlersDelete(handlerId string) error {
l.handlersMux.Lock()
defer l.handlersMux.Unlock()
handler, ok := l.handlers[handlerId]
if !ok {
return errors.New("handler not found")
}
delete(l.handlers, handlerId)
select {
default:
close(handler.stopper)
case <-handler.stopper:
}
handler.queue.Close()
return nil
}
func (l *logic) launchHandler(handlerId string, handleFx HandlerFx, stopper chan struct{}, queue interface {
goqueue.Dequeuer
goqueue.Event
goqueue.Owner
}) {
started := make(chan struct{})
l.Add(1)
go func() {
defer l.Done()
defer func() { l.Debug(logAlias+"stopped handler: %s", handlerId) }()
defer func() {
select {
default:
close(stopper)
case <-stopper:
}
}()
signalIn := queue.GetSignalIn()
close(started)
l.Debug(logAlias+"started handler: %s", handlerId)
for {
select {
case <-stopper:
return
case <-l.ctx.Done():
return
case <-signalIn:
if change, underflow := changeDequeue(queue); !underflow {
handleFx(l.ctx, handlerId, []*data.Change{change})
}
}
}
}()
<-started
}
func (l *logic) changeBroadcast(change *data.Change) {
handlers := l.handlersRead()
if len(handlers) == 0 {
l.Debug(logAlias + "received change to broadcast, but no handlers")
return
}
for handlerId, queue := range l.handlersRead() {
if overflow := queue.Enqueue(change); overflow {
l.Trace(logAlias+"overflow while attempting to broadcast change %s to %s", change.Id, handlerId)
continue
}
l.Trace(logAlias+"broadcasted change %s to %s", change.Id, handlerId)
}
}
func (l *logic) SetUtilities(parameters ...interface{}) {
for _, parameter := range parameters {
switch p := parameter.(type) {
case logger.Logger:
l.Logger = p
}
}
}
func (l *logic) SetParameters(parameters ...interface{}) {
for _, parameter := range parameters {
switch p := parameter.(type) {
case interface {
meta.Change
meta.Registration
meta.RegistrationChange
}:
l.Change = p
l.Registration = p
l.RegistrationChange = p
case meta.Registration:
l.Registration = p
case meta.RegistrationChange:
l.RegistrationChange = p
case meta.Change:
l.Change = p
}
}
switch {
case l.Registration == nil:
panic(PanicRegistrationMetaNotSet)
case l.RegistrationChange == nil:
panic(PanicRegistrationChangeMetaNotSet)
case l.Change == nil:
panic(PanicChangeMetaNotSet)
}
}
func (l *logic) Initialize() error {
l.Lock()
defer l.Unlock()
if l.initialized {
return errors.New("logic already initialized")
}
l.ctx, l.cancel = context.WithCancel(context.Background())
l.initialized = true
l.Info(logAlias + "initialized")
return nil
}
func (l *logic) Shutdown() {
l.Lock()
defer l.Unlock()
if !l.initialized {
return
}
l.cancel()
for handlerId := range l.handlersRead() {
l.handlersDelete(handlerId)
}
l.Wait()
l.Info(logAlias + "shutdown")
l.initialized = false
}
func (l *logic) ChangeUpsert(ctx context.Context, changePartial data.ChangePartial) (*data.Change, error) {
change, err := l.Change.ChangeCreate(ctx, changePartial)
if err != nil {
return nil, err
}
if err := l.RegistrationChangeUpsert(ctx, change.Id); err != nil {
return nil, err
}
l.Trace(logAlias+"upserted change: %s", change.Id)
l.changeBroadcast(change)
return change, nil
}
func (l *logic) RegistrationChangesRead(ctx context.Context, registrationId string) ([]*data.Change, error) {
changeIds, err := l.RegistrationChange.RegistrationChangesRead(ctx, registrationId)
if err != nil {
return nil, err
}
//KIM: because this change is inclusive, if we provide an empty list of change ids
// it will return ALL changes
if len(changeIds) == 0 {
return nil, nil
}
return l.ChangesRead(ctx, data.ChangeSearch{ChangeIds: changeIds})
}
func (l *logic) RegistrationChangeAcknowledge(ctx context.Context, serviceName string, changeIds ...string) error {
//REVIEW: there's an opportunity for data inconsistency if the
// deletion of the changes fail, maybe store them in memory?
changeIdsToDelete, err := l.RegistrationChange.RegistrationChangeAcknowledge(ctx, serviceName, changeIds...)
if err != nil {
return err
}
l.Trace(logAlias+"acknowledged change(s) %v for %s", changeIds, serviceName)
if len(changeIdsToDelete) > 0 {
if err := l.ChangesDelete(ctx, changeIdsToDelete...); err != nil {
return err
}
l.Trace(logAlias+"deleted change(s): %v", changeIdsToDelete)
}
return nil
}
func (l *logic) HandlerCreate(ctx context.Context, handleFx HandlerFx) (string, error) {
stopper, queue := make(chan struct{}), finite.New(QueueSize)
handlerId := l.handlersWrite(stopper, queue)
l.launchHandler(handlerId, handleFx, stopper, queue)
l.Trace(logAlias+"created handler: %s", handlerId)
return handlerId, nil
}
func (l *logic) HandlerDelete(ctx context.Context, handlerId string) error {
if err := l.handlersDelete(handlerId); err != nil {
return err
}
l.Trace(logAlias+"deleted handler %s", handlerId)
return nil
}
func (l *logic) HealthCheck(ctx context.Context) (*healthcheckdata.HealthCheck, error) {
return &healthcheckdata.HealthCheck{Time: time.Now().UnixNano()}, nil
}