-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.go
171 lines (143 loc) · 3.43 KB
/
action.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
package baker
import (
"context"
"log/slog"
)
type EventType int
const (
_ EventType = iota
pingerEvent
addEvent
updateEvent
removeEvent
getEvent
)
type Event struct {
Type EventType
Container *Container
Endpoint *Endpoint
Result chan struct {
Container *Container
Endpoint *Endpoint
}
}
type ActionRunner struct {
pingerCallback func()
addCallback func(*Container)
updateCallback func(*Container, *Endpoint)
removeCallback func(*Container)
getCallback func(string, string) (*Container, *Endpoint)
events chan *Event
close chan struct{} // using this to make sure pushing to events stops when Close() is called
}
var _ Driver = (*ActionRunner)(nil)
func (ar *ActionRunner) Pinger() {
ar.push(&Event{Type: pingerEvent})
}
func (ar *ActionRunner) Add(container *Container) {
ar.push(&Event{Type: addEvent, Container: container})
}
func (ar *ActionRunner) Update(container *Container, endpoint *Endpoint) {
ar.push(&Event{Type: updateEvent, Container: container, Endpoint: endpoint})
}
func (ar *ActionRunner) Remove(container *Container) {
ar.push(&Event{Type: removeEvent, Container: container})
}
func (ar *ActionRunner) Get(ctx context.Context, endpoint *Endpoint) (*Container, *Endpoint) {
evt := &Event{
Type: getEvent,
Endpoint: endpoint,
Result: make(chan struct {
Container *Container
Endpoint *Endpoint
}, 1),
}
ar.push(evt)
select {
case r := <-evt.Result:
return r.Container, r.Endpoint
case <-ctx.Done():
return nil, nil
case <-ar.close:
return nil, nil
}
}
func (ar *ActionRunner) push(event *Event) {
select {
case <-ar.close:
return
case ar.events <- event:
default:
slog.Error("ActionRunner: events channel is full, dropping event")
}
}
func (ar *ActionRunner) Close() {
close(ar.close)
}
func WithPingerCallback(callback func()) func(*ActionRunner) {
return func(ar *ActionRunner) {
ar.pingerCallback = callback
}
}
func WithAddCallback(callback func(*Container)) func(*ActionRunner) {
return func(ar *ActionRunner) {
ar.addCallback = callback
}
}
func WithUpdateCallback(callback func(*Container, *Endpoint)) func(*ActionRunner) {
return func(ar *ActionRunner) {
ar.updateCallback = callback
}
}
func WithRemoveCallback(callback func(*Container)) func(*ActionRunner) {
return func(ar *ActionRunner) {
ar.removeCallback = callback
}
}
func WithGetCallback(callback func(string, string) (*Container, *Endpoint)) func(*ActionRunner) {
return func(ar *ActionRunner) {
ar.getCallback = callback
}
}
type ActionCallback func(*ActionRunner)
func NewActionRunner(bufferSize int, cbs ...ActionCallback) *ActionRunner {
ar := &ActionRunner{
events: make(chan *Event, bufferSize),
close: make(chan struct{}),
}
for _, cb := range cbs {
cb(ar)
}
go func() {
defer slog.Debug("ActionRunner: stopped")
for {
select {
case <-ar.close:
return
case event, ok := <-ar.events:
if !ok {
return
}
switch event.Type {
case pingerEvent:
ar.pingerCallback()
case addEvent:
ar.addCallback(event.Container)
case updateEvent:
ar.updateCallback(event.Container, event.Endpoint)
case removeEvent:
ar.removeCallback(event.Container)
case getEvent:
container, endpoint := ar.getCallback(event.Endpoint.Domain, event.Endpoint.Path)
event.Result <- struct {
Container *Container
Endpoint *Endpoint
}{container, endpoint}
default:
continue
}
}
}
}()
return ar
}