-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhooks.go
More file actions
344 lines (276 loc) · 9.44 KB
/
Copy pathhooks.go
File metadata and controls
344 lines (276 loc) · 9.44 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package streaming
import (
"context"
"sync"
streaming "github.com/xraph/forge/extensions/streaming/internal"
)
// StreamingHook is the base interface for all streaming hooks.
// Hooks implement one or more of the optional hook interfaces below.
type StreamingHook interface {
Name() string
}
// ConnectionHook fires on connection lifecycle events.
type ConnectionHook interface {
StreamingHook
// OnConnect is called before registration. Return error to reject connection.
OnConnect(ctx context.Context, conn Connection) error
// OnDisconnect is called after unregistration.
OnDisconnect(ctx context.Context, conn Connection)
}
// MessageHook fires on message events.
type MessageHook interface {
StreamingHook
// OnMessageReceived is called before message processing. Can transform or block (return nil).
OnMessageReceived(ctx context.Context, conn Connection, msg *Message) (*Message, error)
// OnMessageDelivered is called after delivery (non-blocking, runs async).
OnMessageDelivered(ctx context.Context, conn Connection, msg *Message)
}
// RawMessageHook fires before deserialization on raw bytes from the connection.
type RawMessageHook interface {
StreamingHook
// OnRawMessage processes raw bytes before decoding. Return error to drop the message.
OnRawMessage(ctx context.Context, conn Connection, data []byte) ([]byte, error)
}
// RoomHook fires on room lifecycle events.
type RoomHook interface {
StreamingHook
// OnRoomJoin is called before join. Return error to reject.
OnRoomJoin(ctx context.Context, conn Connection, roomID string) error
// OnRoomLeave is called after leave.
OnRoomLeave(ctx context.Context, conn Connection, roomID string)
// OnRoomCreate is called before room creation. Return error to reject.
OnRoomCreate(ctx context.Context, room Room) error
// OnRoomDelete is called after room deletion.
OnRoomDelete(ctx context.Context, roomID string)
}
// PresenceHook fires on presence changes.
type PresenceHook interface {
StreamingHook
// OnPresenceChange is called after a user's presence status changes.
OnPresenceChange(ctx context.Context, userID, oldStatus, newStatus string)
}
// ErrorHook fires on message handling errors.
type ErrorHook interface {
StreamingHook
// OnError is called when a message handling error occurs.
OnError(ctx context.Context, conn Connection, err error)
}
// HookRegistry manages streaming hooks and dispatches events.
type HookRegistry struct {
mu sync.RWMutex
hooks map[string]StreamingHook
// Pre-categorized for fast dispatch (rebuilt on add/remove).
connectionHooks []ConnectionHook
messageHooks []MessageHook
rawMessageHooks []RawMessageHook
roomHooks []RoomHook
presenceHooks []PresenceHook
errorHooks []ErrorHook
}
// NewHookRegistry creates a new hook registry.
func NewHookRegistry() *HookRegistry {
return &HookRegistry{
hooks: make(map[string]StreamingHook),
}
}
// Add registers a hook. The hook is type-asserted to categorize it
// into the appropriate dispatch lists.
func (r *HookRegistry) Add(hook StreamingHook) {
r.mu.Lock()
defer r.mu.Unlock()
r.hooks[hook.Name()] = hook
r.rebuild()
}
// Remove unregisters a hook by name.
func (r *HookRegistry) Remove(name string) {
r.mu.Lock()
defer r.mu.Unlock()
delete(r.hooks, name)
r.rebuild()
}
// List returns all registered hooks.
func (r *HookRegistry) List() []StreamingHook {
r.mu.RLock()
defer r.mu.RUnlock()
result := make([]StreamingHook, 0, len(r.hooks))
for _, h := range r.hooks {
result = append(result, h)
}
return result
}
// rebuild categorizes hooks by interface. Must be called with lock held.
func (r *HookRegistry) rebuild() {
r.connectionHooks = r.connectionHooks[:0]
r.messageHooks = r.messageHooks[:0]
r.rawMessageHooks = r.rawMessageHooks[:0]
r.roomHooks = r.roomHooks[:0]
r.presenceHooks = r.presenceHooks[:0]
r.errorHooks = r.errorHooks[:0]
for _, h := range r.hooks {
if ch, ok := h.(ConnectionHook); ok {
r.connectionHooks = append(r.connectionHooks, ch)
}
if mh, ok := h.(MessageHook); ok {
r.messageHooks = append(r.messageHooks, mh)
}
if rmh, ok := h.(RawMessageHook); ok {
r.rawMessageHooks = append(r.rawMessageHooks, rmh)
}
if rh, ok := h.(RoomHook); ok {
r.roomHooks = append(r.roomHooks, rh)
}
if ph, ok := h.(PresenceHook); ok {
r.presenceHooks = append(r.presenceHooks, ph)
}
if eh, ok := h.(ErrorHook); ok {
r.errorHooks = append(r.errorHooks, eh)
}
}
}
// snapshot helpers — copy slice under read lock to avoid holding lock during dispatch.
func (r *HookRegistry) connectionHooksCopy() []ConnectionHook {
r.mu.RLock()
defer r.mu.RUnlock()
cp := make([]ConnectionHook, len(r.connectionHooks))
copy(cp, r.connectionHooks)
return cp
}
func (r *HookRegistry) messageHooksCopy() []MessageHook {
r.mu.RLock()
defer r.mu.RUnlock()
cp := make([]MessageHook, len(r.messageHooks))
copy(cp, r.messageHooks)
return cp
}
func (r *HookRegistry) rawMessageHooksCopy() []RawMessageHook {
r.mu.RLock()
defer r.mu.RUnlock()
cp := make([]RawMessageHook, len(r.rawMessageHooks))
copy(cp, r.rawMessageHooks)
return cp
}
func (r *HookRegistry) roomHooksCopy() []RoomHook {
r.mu.RLock()
defer r.mu.RUnlock()
cp := make([]RoomHook, len(r.roomHooks))
copy(cp, r.roomHooks)
return cp
}
func (r *HookRegistry) presenceHooksCopy() []PresenceHook {
r.mu.RLock()
defer r.mu.RUnlock()
cp := make([]PresenceHook, len(r.presenceHooks))
copy(cp, r.presenceHooks)
return cp
}
func (r *HookRegistry) errorHooksCopy() []ErrorHook {
r.mu.RLock()
defer r.mu.RUnlock()
cp := make([]ErrorHook, len(r.errorHooks))
copy(cp, r.errorHooks)
return cp
}
// --- Fire methods ---
// FireOnConnect fires ConnectionHook.OnConnect for all registered hooks.
// Returns the first error encountered, which should be used to reject the connection.
func (r *HookRegistry) FireOnConnect(ctx context.Context, conn streaming.EnhancedConnection) error {
for _, h := range r.connectionHooksCopy() {
if err := h.OnConnect(ctx, conn); err != nil {
return err
}
}
return nil
}
// FireOnDisconnect fires ConnectionHook.OnDisconnect for all registered hooks.
// Errors are ignored (post-hook).
func (r *HookRegistry) FireOnDisconnect(ctx context.Context, conn streaming.EnhancedConnection) {
for _, h := range r.connectionHooksCopy() {
h.OnDisconnect(ctx, conn)
}
}
// FireOnMessageReceived fires MessageHook.OnMessageReceived for all hooks in sequence.
// Each hook can transform or block (return nil) the message.
func (r *HookRegistry) FireOnMessageReceived(ctx context.Context, conn streaming.EnhancedConnection, msg *streaming.Message) (*streaming.Message, error) {
current := msg
for _, h := range r.messageHooksCopy() {
result, err := h.OnMessageReceived(ctx, conn, current)
if err != nil {
return nil, err
}
if result == nil {
return nil, nil // message blocked
}
current = result
}
return current, nil
}
// FireOnMessageDelivered fires MessageHook.OnMessageDelivered asynchronously.
// This is non-blocking to avoid slowing down message delivery.
func (r *HookRegistry) FireOnMessageDelivered(ctx context.Context, conn streaming.EnhancedConnection, msg *streaming.Message) {
hooks := r.messageHooksCopy()
if len(hooks) == 0 {
return
}
go func() {
for _, h := range hooks {
h.OnMessageDelivered(ctx, conn, msg)
}
}()
}
// FireOnRawMessage fires RawMessageHook.OnRawMessage for all hooks in sequence.
// Each hook can transform the bytes or block (return error) the message.
func (r *HookRegistry) FireOnRawMessage(ctx context.Context, conn streaming.EnhancedConnection, data []byte) ([]byte, error) {
current := data
for _, h := range r.rawMessageHooksCopy() {
result, err := h.OnRawMessage(ctx, conn, current)
if err != nil {
return nil, err
}
current = result
}
return current, nil
}
// FireOnRoomJoin fires RoomHook.OnRoomJoin for all hooks.
// Returns the first error encountered, which should be used to reject the join.
func (r *HookRegistry) FireOnRoomJoin(ctx context.Context, conn streaming.EnhancedConnection, roomID string) error {
for _, h := range r.roomHooksCopy() {
if err := h.OnRoomJoin(ctx, conn, roomID); err != nil {
return err
}
}
return nil
}
// FireOnRoomLeave fires RoomHook.OnRoomLeave for all hooks (post-hook).
func (r *HookRegistry) FireOnRoomLeave(ctx context.Context, conn streaming.EnhancedConnection, roomID string) {
for _, h := range r.roomHooksCopy() {
h.OnRoomLeave(ctx, conn, roomID)
}
}
// FireOnRoomCreate fires RoomHook.OnRoomCreate for all hooks.
// Returns the first error encountered, which should be used to reject creation.
func (r *HookRegistry) FireOnRoomCreate(ctx context.Context, room streaming.Room) error {
for _, h := range r.roomHooksCopy() {
if err := h.OnRoomCreate(ctx, room); err != nil {
return err
}
}
return nil
}
// FireOnRoomDelete fires RoomHook.OnRoomDelete for all hooks (post-hook).
func (r *HookRegistry) FireOnRoomDelete(ctx context.Context, roomID string) {
for _, h := range r.roomHooksCopy() {
h.OnRoomDelete(ctx, roomID)
}
}
// FireOnPresenceChange fires PresenceHook.OnPresenceChange for all hooks (post-hook).
func (r *HookRegistry) FireOnPresenceChange(ctx context.Context, userID, oldStatus, newStatus string) {
for _, h := range r.presenceHooksCopy() {
h.OnPresenceChange(ctx, userID, oldStatus, newStatus)
}
}
// FireOnError fires ErrorHook.OnError for all hooks (post-hook).
func (r *HookRegistry) FireOnError(ctx context.Context, conn streaming.EnhancedConnection, err error) {
for _, h := range r.errorHooksCopy() {
h.OnError(ctx, conn, err)
}
}