-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlistener.go
197 lines (163 loc) · 4.55 KB
/
listener.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
package flash
import (
"errors"
"sync"
)
// TODO SORTIR VERIFICATION AU NIVEAU LISTENER, PBM oblige à envoyer les columns dans l'event
type ListenerCondition struct {
Column string
//Operator string //TODO actually only equals are implemented
Value any
}
type ListenerConfig struct {
Table string // Can be prefixed by schema - e.g: public.posts
Fields []string // Empty fields means all ( SELECT * )
MaxParallelProcess int // Default to 1 (not parallel) -> use -1 for Infinity
Conditions []*ListenerCondition
}
type CreateEventCallback func(event Operation) error
type DeleteEventCallback func(event Operation) error
type EventCallback func(event Event)
type Listener struct {
Config *ListenerConfig
// Internals
sync.Mutex
callbacks map[*EventCallback]Operation
listenedOperations Operation // Use bitwise comparison to check for listened events
semaphore chan struct{}
// Trigger client
_clientCreateEventCallback CreateEventCallback
_clientDeleteEventCallback DeleteEventCallback
_clientInitialized bool
}
func NewListener(config *ListenerConfig) (*Listener, error) {
if config == nil {
return nil, errors.New("config cannot be nil")
}
if config.MaxParallelProcess == 0 {
config.MaxParallelProcess = 1
}
var semaphore chan struct{} = nil
if config.MaxParallelProcess != -1 {
semaphore = make(chan struct{}, config.MaxParallelProcess)
}
return &Listener{
Config: config,
callbacks: make(map[*EventCallback]Operation),
semaphore: semaphore,
}, nil
}
/* Callback management */
func (l *Listener) On(operation Operation, callback EventCallback) (func() error, error) {
if callback == nil {
return nil, errors.New("callback cannot be nil")
}
// TODO NOTIFY CLIENT FROM UPDATE BUT DO NOT SEND INSERT/DELETE
if err := l.addListenedEventIfNeeded(operation); err != nil {
return nil, err
}
l.callbacks[&callback] = operation
removeFunc := func() error {
delete(l.callbacks, &callback) // Important keep before removeListenedOperationIfNeeded
if err := l.removeListenedOperationIfNeeded(operation); err != nil {
return err
}
callback = nil
return nil
}
return removeFunc, nil
}
func (l *Listener) Dispatch(event *Event) {
for callback, listenedOperations := range l.callbacks {
if listenedOperations.IncludeOne((*event).GetOperation()) {
if l.Config.MaxParallelProcess == -1 {
go (*callback)(*event)
continue
}
// Acquire semaphore
l.semaphore <- struct{}{}
if l.Config.MaxParallelProcess == 1 {
(*callback)(*event)
<-l.semaphore
continue
}
go func() {
(*callback)(*event)
<-l.semaphore
}()
}
}
}
// Init emit all event for first boot */
func (l *Listener) Init(_createCallback CreateEventCallback, _deleteCallback DeleteEventCallback) error {
l.Lock()
defer l.Unlock()
l._clientCreateEventCallback = _createCallback
l._clientDeleteEventCallback = _deleteCallback
// Emit all events for initialization
for targetEvent := Operation(1); targetEvent != 0 && targetEvent <= OperationAll; targetEvent <<= 1 {
if l.listenedOperations&targetEvent == 0 {
continue
}
if err := _createCallback(targetEvent); err != nil {
return err
}
}
l._clientInitialized = true
return nil
}
func (l *Listener) addListenedEventIfNeeded(event Operation) error {
initialEvents := l.listenedOperations
l.listenedOperations |= event
// Trigger event if change appears
diff := initialEvents ^ l.listenedOperations
if diff == 0 {
return nil
}
for targetEvent := Operation(1); targetEvent != 0 && targetEvent <= OperationAll; targetEvent <<= 1 {
if targetEvent&diff == 0 || targetEvent&event == 0 {
continue
}
l.Lock()
if l._clientInitialized {
if err := l._clientCreateEventCallback(targetEvent); err != nil {
return err
}
}
l.Unlock()
}
return nil
}
func (l *Listener) removeListenedOperationIfNeeded(event Operation) error {
for targetEvent := Operation(1); targetEvent != 0 && targetEvent <= event; targetEvent <<= 1 {
if targetEvent&l.listenedOperations == 0 {
continue
}
if l.hasListenersForEvent(targetEvent) {
continue
}
l.listenedOperations &= ^targetEvent
if l._clientInitialized {
l.Lock()
if err := l._clientDeleteEventCallback(targetEvent); err != nil {
return err
}
l.Unlock()
}
}
return nil
}
func (l *Listener) Close() error {
l.Lock()
defer l.Unlock()
l._clientInitialized = false
return nil
}
func (l *Listener) hasListenersForEvent(event Operation) bool {
for _, listens := range l.callbacks {
if listens&event > 0 {
return true
}
}
return false
}