forked from nknorg/nnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
85 lines (76 loc) · 3.25 KB
/
middleware.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
package routing
import (
"errors"
"github.com/nknorg/nnet/middleware"
"github.com/nknorg/nnet/node"
)
// RemoteMessageArrived is called when a new remote message arrives and prepare
// to be handled by the corresponding router. Message with the same message id
// will each trigger this middleware once. This can be used to process, modify
// or discard message. Returns the remote message to be used (or nil to discard
// the message) and if we should proceed to the next middleware.
type RemoteMessageArrived struct {
Func func(*node.RemoteMessage) (*node.RemoteMessage, bool)
Priority int32
}
// RemoteMessageRouted is called when the router has computed the node to route
// (could be the local node, remote nodes, or both), and before the message is
// dispatched to local or remote nodes. Message with the same message id will
// each trigger this middleware once. This can be used to process, modify or
// discard message, or change routes. Returns the remote message to be used (or
// nil to discard the message), local node and remote nodes where the message
// should be routed to, and if we should proceed to the next middleware.
type RemoteMessageRouted struct {
Func func(*node.RemoteMessage, *node.LocalNode, []*node.RemoteNode) (*node.RemoteMessage, *node.LocalNode, []*node.RemoteNode, bool)
Priority int32
}
// RemoteMessageReceived is called when a new remote message is received, routed
// to local node, and prepare to be handled by local node. Message with the same
// message id will only trigger this middleware once. This can be used to
// process, modify or discard message. Returns the remote message to be used (or
// nil to discard the message) and if we should proceed to the next middleware.
type RemoteMessageReceived struct {
Func func(*node.RemoteMessage) (*node.RemoteMessage, bool)
Priority int32
}
// middlewareStore stores the functions that will be called when certain events
// are triggered or in some pipeline
type middlewareStore struct {
remoteMessageArrived []RemoteMessageArrived
remoteMessageRouted []RemoteMessageRouted
remoteMessageReceived []RemoteMessageReceived
}
// newMiddlewareStore creates a middlewareStore
func newMiddlewareStore() *middlewareStore {
return &middlewareStore{
remoteMessageArrived: make([]RemoteMessageArrived, 0),
remoteMessageRouted: make([]RemoteMessageRouted, 0),
remoteMessageReceived: make([]RemoteMessageReceived, 0),
}
}
// ApplyMiddleware add a middleware to the store
func (store *middlewareStore) ApplyMiddleware(mw interface{}) error {
switch mw := mw.(type) {
case RemoteMessageArrived:
if mw.Func == nil {
return errors.New("middleware function is nil")
}
store.remoteMessageArrived = append(store.remoteMessageArrived, mw)
middleware.Sort(store.remoteMessageArrived)
case RemoteMessageRouted:
if mw.Func == nil {
return errors.New("middleware function is nil")
}
store.remoteMessageRouted = append(store.remoteMessageRouted, mw)
middleware.Sort(store.remoteMessageRouted)
case RemoteMessageReceived:
if mw.Func == nil {
return errors.New("middleware function is nil")
}
store.remoteMessageReceived = append(store.remoteMessageReceived, mw)
middleware.Sort(store.remoteMessageReceived)
default:
return errors.New("unknown middleware type")
}
return nil
}