-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandler.go
301 lines (254 loc) · 8.94 KB
/
handler.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
294
295
296
297
298
299
300
301
package lime
import (
"context"
"errors"
"fmt"
)
type EnvelopeMux struct {
msgHandlers []MessageHandler
notHandlers []NotificationHandler
reqCmdHandlers []RequestCommandHandler
respCmdHandlers []ResponseCommandHandler
}
func (m *EnvelopeMux) ListenServer(ctx context.Context, c *ServerChannel) error {
if err := m.listen(ctx, c.channel); err != nil {
return fmt.Errorf("listen server: %w", err)
}
return nil
}
func (m *EnvelopeMux) ListenClient(ctx context.Context, c *ClientChannel) error {
if err := m.listen(ctx, c.channel); err != nil {
return fmt.Errorf("listen client: %w", err)
}
return nil
}
func (m *EnvelopeMux) listen(ctx context.Context, c *channel) error {
if err := c.ensureEstablished("receive"); err != nil {
return err
}
for c.Established() && ctx.Err() == nil {
ctx := sessionContext(ctx, c)
select {
case <-ctx.Done():
return ctx.Err()
case <-c.RcvDone():
return nil
case msg, ok := <-c.MsgChan():
if !ok {
return errors.New("msg chan: channel closed")
}
if err := m.handleMessage(ctx, msg, c); err != nil {
return err
}
case not, ok := <-c.NotChan():
if !ok {
return errors.New("not chan: channel closed")
}
if err := m.handleNotification(ctx, not); err != nil {
return err
}
case reqCmd, ok := <-c.ReqCmdChan():
if !ok {
return errors.New("req cmd chan: channel closed")
}
if err := m.handleRequestCommand(ctx, reqCmd, c); err != nil {
return err
}
case respCmd, ok := <-c.RespCmdChan():
if !ok {
return errors.New("resp cmd chan: channel closed")
}
if err := m.handleResponseCommand(ctx, respCmd, c); err != nil {
return err
}
}
}
return ctx.Err()
}
func (m *EnvelopeMux) handleMessage(ctx context.Context, msg *Message, s Sender) error {
for _, h := range m.msgHandlers {
if !h.Match(msg) {
continue
}
if err := h.Handle(ctx, msg, s); err != nil {
return fmt.Errorf("handle message: %w", err)
}
break
}
return nil
}
func (m *EnvelopeMux) handleNotification(ctx context.Context, not *Notification) error {
for _, h := range m.notHandlers {
if !h.Match(not) {
continue
}
if err := h.Handle(ctx, not); err != nil {
return fmt.Errorf("handle notification: %w", err)
}
break
}
return nil
}
func (m *EnvelopeMux) handleRequestCommand(ctx context.Context, cmd *RequestCommand, s Sender) error {
for _, h := range m.reqCmdHandlers {
if !h.Match(cmd) {
continue
}
if err := h.Handle(ctx, cmd, s); err != nil {
return fmt.Errorf("handle command: %w", err)
}
break
}
return nil
}
func (m *EnvelopeMux) handleResponseCommand(ctx context.Context, cmd *ResponseCommand, s Sender) error {
for _, h := range m.respCmdHandlers {
if !h.Match(cmd) {
continue
}
if err := h.Handle(ctx, cmd, s); err != nil {
return fmt.Errorf("handle command: %w", err)
}
break
}
return nil
}
// MessageHandlerFunc allows the definition of a function for handling received messages that matches
// the specified predicate. Note that the registration order matters, since the receiving process stops when
// the first predicate match occurs.
func (m *EnvelopeMux) MessageHandlerFunc(predicate MessagePredicate, f MessageHandlerFunc) {
m.MessageHandler(&messageHandler{
predicate: predicate,
handlerFunc: f,
})
}
func (m *EnvelopeMux) MessageHandler(handler MessageHandler) {
m.msgHandlers = append(m.msgHandlers, handler)
}
func (m *EnvelopeMux) NotificationHandlerFunc(predicate NotificationPredicate, f NotificationHandlerFunc) {
m.NotificationHandler(¬ificationHandler{
predicate: predicate,
handlerFunc: f,
})
}
func (m *EnvelopeMux) NotificationHandler(handler NotificationHandler) {
m.notHandlers = append(m.notHandlers, handler)
}
func (m *EnvelopeMux) RequestCommandHandlerFunc(predicate RequestCommandPredicate, f RequestCommandHandlerFunc) {
m.RequestCommandHandler(&requestCommandHandler{
predicate: predicate,
handlerFunc: f,
})
}
func (m *EnvelopeMux) RequestCommandHandler(handler RequestCommandHandler) {
m.reqCmdHandlers = append(m.reqCmdHandlers, handler)
}
func (m *EnvelopeMux) ResponseCommandHandlerFunc(predicate ResponseCommandPredicate, f ResponseCommandHandlerFunc) {
m.ResponseCommandHandler(&responseCommandHandler{
predicate: predicate,
handlerFunc: f,
})
}
func (m *EnvelopeMux) ResponseCommandHandler(handler ResponseCommandHandler) {
m.respCmdHandlers = append(m.respCmdHandlers, handler)
}
// MessageHandler defines a handler for processing Message instances received from a channel.
type MessageHandler interface {
// Match indicates if the specified Message should be handled by the instance.
Match(msg *Message) bool
// Handle execute an action for the specified Message.
// If this method returns an error, it signals to the channel listener to stop.
Handle(ctx context.Context, msg *Message, s Sender) error
}
// MessagePredicate defines an expression for checking if the specified Message satisfies a condition.
type MessagePredicate func(msg *Message) bool
// MessageHandlerFunc defines an action to be executed to a Message.
type MessageHandlerFunc func(ctx context.Context, msg *Message, s Sender) error
type messageHandler struct {
predicate MessagePredicate
handlerFunc MessageHandlerFunc
}
func (h *messageHandler) Match(msg *Message) bool {
if h.predicate == nil {
return true
}
return h.predicate(msg)
}
func (h *messageHandler) Handle(ctx context.Context, msg *Message, s Sender) error {
return h.handlerFunc(ctx, msg, s)
}
// NotificationHandler defines a handler for processing Notification instances received from a channel.
type NotificationHandler interface {
// Match indicates if the specified Notification should be handled by the instance.
Match(not *Notification) bool
// Handle execute an action for the specified Notification.
// If this method returns an error, it signals to the channel listener to stop.
Handle(ctx context.Context, not *Notification) error
}
// NotificationPredicate defines an expression for checking if the specified Notification satisfies a condition.
type NotificationPredicate func(not *Notification) bool
// NotificationHandlerFunc defines an action to be executed to a Notification.
type NotificationHandlerFunc func(ctx context.Context, not *Notification) error
type notificationHandler struct {
predicate NotificationPredicate
handlerFunc NotificationHandlerFunc
}
func (h *notificationHandler) Match(not *Notification) bool {
if h.predicate == nil {
return true
}
return h.predicate(not)
}
func (h *notificationHandler) Handle(ctx context.Context, not *Notification) error {
return h.handlerFunc(ctx, not)
}
// RequestCommandHandler defines a handler for processing Command instances received from a channel.
type RequestCommandHandler interface {
// Match indicates if the specified RequestCommand should be handled by the instance.
Match(cmd *RequestCommand) bool
// Handle execute an action for the specified RequestCommand.
// If this method returns an error, it signals to the channel listener to stop.
Handle(ctx context.Context, cmd *RequestCommand, s Sender) error
}
// RequestCommandPredicate defines an expression for checking if the specified RequestCommand satisfies a condition.
type RequestCommandPredicate func(cmd *RequestCommand) bool
// RequestCommandHandlerFunc defines an action to be executed to a RequestCommand.
type RequestCommandHandlerFunc func(ctx context.Context, cmd *RequestCommand, s Sender) error
type requestCommandHandler struct {
predicate RequestCommandPredicate
handlerFunc RequestCommandHandlerFunc
}
func (h *requestCommandHandler) Match(cmd *RequestCommand) bool {
if h.predicate == nil {
return true
}
return h.predicate(cmd)
}
func (h *requestCommandHandler) Handle(ctx context.Context, cmd *RequestCommand, s Sender) error {
return h.handlerFunc(ctx, cmd, s)
}
// ResponseCommandHandler defines a handler for processing Command instances received from a channel.
type ResponseCommandHandler interface {
// Match indicates if the specified ResponseCommand should be handled by the instance.
Match(cmd *ResponseCommand) bool
// Handle execute an action for the specified ResponseCommand.
// If this method returns an error, it signals to the channel listener to stop.
Handle(ctx context.Context, cmd *ResponseCommand, s Sender) error
}
// ResponseCommandPredicate defines an expression for checking if the specified ResponseCommand satisfies a condition.
type ResponseCommandPredicate func(cmd *ResponseCommand) bool
// ResponseCommandHandlerFunc defines an action to be executed to a ResponseCommand.
type ResponseCommandHandlerFunc func(ctx context.Context, cmd *ResponseCommand, s Sender) error
type responseCommandHandler struct {
predicate ResponseCommandPredicate
handlerFunc ResponseCommandHandlerFunc
}
func (h *responseCommandHandler) Match(cmd *ResponseCommand) bool {
if h.predicate == nil {
return true
}
return h.predicate(cmd)
}
func (h *responseCommandHandler) Handle(ctx context.Context, cmd *ResponseCommand, s Sender) error {
return h.handlerFunc(ctx, cmd, s)
}