-
Notifications
You must be signed in to change notification settings - Fork 33
/
middleware.go
224 lines (209 loc) · 8.27 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
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
package chord
import (
"errors"
"github.com/nknorg/nnet/middleware"
"github.com/nknorg/nnet/node"
"github.com/nknorg/nnet/overlay"
)
// SuccessorAdded is called when a new remote node has been added to the
// successor list. This does not necessarily mean the remote node has just
// established a new connection with local node, but may also because another
// remote node previously in successor list was disconnected or removed. When
// being called, it will also pass the index of the remote node in successor
// list after it is added. Returns if we should proceed to the next middleware.
type SuccessorAdded struct {
Func func(*node.RemoteNode, int) bool
Priority int32
}
// SuccessorRemoved is called when a remote node has been removed from the
// successor list. This does not necessarily mean the remote node has just
// disconnected with local node, but may also because another remote node is
// added to the successor list. Returns if we should proceed to the next
// middleware.
type SuccessorRemoved struct {
Func func(*node.RemoteNode) bool
Priority int32
}
// PredecessorAdded is called when a new remote node has been added to the
// predecessor list. This does not necessarily mean the remote node has just
// established a new connection with local node, but may also because another
// remote node previously in predecessor list was disconnected or removed. When
// being called, it will also pass the index of the remote node in predecessor
// list after it is added. Returns if we should proceed to the next middleware.
type PredecessorAdded struct {
Func func(*node.RemoteNode, int) bool
Priority int32
}
// PredecessorRemoved is called when a remote node has been removed from the
// predecessor list. This does not necessarily mean the remote node has just
// disconnected with local node, but may also because another remote node is
// added to the predecessor list. Returns if we should proceed to the next
// middleware.
type PredecessorRemoved struct {
Func func(*node.RemoteNode) bool
Priority int32
}
// FingerTableAdded is called when a new remote node has been added to the
// finger table. This does not necessarily mean the remote node has just
// established a new connection with local node, but may also because another
// remote node previously in finger table was disconnected or removed. When
// being called, it will also pass the index of finger table and the index of
// the remote node in that finger table after it is added. Returns if we should
// proceed to the next middleware.
type FingerTableAdded struct {
Func func(*node.RemoteNode, int, int) bool
Priority int32
}
// FingerTableRemoved is called when a remote node has been removed from the
// finger table. This does not necessarily mean the remote node has just
// disconnected with local node, but may also because another remote node is
// added to the finger table. Returns if we should proceed to the next
// middleware.
type FingerTableRemoved struct {
Func func(*node.RemoteNode, int) bool
Priority int32
}
// NeighborAdded is called when a new remote node has been added to the neighbor
// list. When being called, it will also pass the index of the remote node in
// neighbor list after it is added. Returns if we should proceed to the next
// middleware.
type NeighborAdded struct {
Func func(*node.RemoteNode, int) bool
Priority int32
}
// NeighborRemoved is called when a remote node has been removed from the
// neighbor list. Returns if we should proceed to the next middleware.
type NeighborRemoved struct {
Func func(*node.RemoteNode) bool
Priority int32
}
// RelayPriority is called when computing the priority of next hop candidate.
// Node that has the highest priority value will be selected. Initial priority
// is negative latency in unit of ms. When being called, it will also pass the
// current priority value. Returns the new priority value and whether we should
// proceed to the next middleware.
type RelayPriority struct {
Func func(*node.RemoteNode, float64) (float64, bool)
Priority int32
}
// middlewareStore stores the functions that will be called when certain events
// are triggered or in some pipeline
type middlewareStore struct {
networkWillStart []overlay.NetworkWillStart
networkStarted []overlay.NetworkStarted
networkWillStop []overlay.NetworkWillStop
networkStopped []overlay.NetworkStopped
successorAdded []SuccessorAdded
successorRemoved []SuccessorRemoved
predecessorAdded []PredecessorAdded
predecessorRemoved []PredecessorRemoved
fingerTableAdded []FingerTableAdded
fingerTableRemoved []FingerTableRemoved
neighborAdded []NeighborAdded
neighborRemoved []NeighborRemoved
relayPriority []RelayPriority
}
// newMiddlewareStore creates a middlewareStore
func newMiddlewareStore() *middlewareStore {
return &middlewareStore{
networkWillStart: make([]overlay.NetworkWillStart, 0),
networkStarted: make([]overlay.NetworkStarted, 0),
networkWillStop: make([]overlay.NetworkWillStop, 0),
networkStopped: make([]overlay.NetworkStopped, 0),
successorAdded: make([]SuccessorAdded, 0),
successorRemoved: make([]SuccessorRemoved, 0),
predecessorAdded: make([]PredecessorAdded, 0),
predecessorRemoved: make([]PredecessorRemoved, 0),
fingerTableAdded: make([]FingerTableAdded, 0),
fingerTableRemoved: make([]FingerTableRemoved, 0),
neighborAdded: make([]NeighborAdded, 0),
neighborRemoved: make([]NeighborRemoved, 0),
relayPriority: make([]RelayPriority, 0),
}
}
// ApplyMiddleware add a middleware to the store
func (store *middlewareStore) ApplyMiddleware(mw interface{}) error {
switch mw := mw.(type) {
case overlay.NetworkWillStart:
if mw.Func == nil {
return errors.New("middleware function is nil")
}
store.networkWillStart = append(store.networkWillStart, mw)
middleware.Sort(store.networkWillStart)
case overlay.NetworkStarted:
if mw.Func == nil {
return errors.New("middleware function is nil")
}
store.networkStarted = append(store.networkStarted, mw)
middleware.Sort(store.networkStarted)
case overlay.NetworkWillStop:
if mw.Func == nil {
return errors.New("middleware function is nil")
}
store.networkWillStop = append(store.networkWillStop, mw)
middleware.Sort(store.networkWillStop)
case overlay.NetworkStopped:
if mw.Func == nil {
return errors.New("middleware function is nil")
}
store.networkStopped = append(store.networkStopped, mw)
middleware.Sort(store.networkStopped)
case SuccessorAdded:
if mw.Func == nil {
return errors.New("middleware function is nil")
}
store.successorAdded = append(store.successorAdded, mw)
middleware.Sort(store.successorAdded)
case SuccessorRemoved:
if mw.Func == nil {
return errors.New("middleware function is nil")
}
store.successorRemoved = append(store.successorRemoved, mw)
middleware.Sort(store.successorRemoved)
case PredecessorAdded:
if mw.Func == nil {
return errors.New("middleware function is nil")
}
store.predecessorAdded = append(store.predecessorAdded, mw)
middleware.Sort(store.predecessorAdded)
case PredecessorRemoved:
if mw.Func == nil {
return errors.New("middleware function is nil")
}
store.predecessorRemoved = append(store.predecessorRemoved, mw)
middleware.Sort(store.predecessorRemoved)
case FingerTableAdded:
if mw.Func == nil {
return errors.New("middleware function is nil")
}
store.fingerTableAdded = append(store.fingerTableAdded, mw)
middleware.Sort(store.fingerTableAdded)
case FingerTableRemoved:
if mw.Func == nil {
return errors.New("middleware function is nil")
}
store.fingerTableRemoved = append(store.fingerTableRemoved, mw)
middleware.Sort(store.fingerTableRemoved)
case NeighborAdded:
if mw.Func == nil {
return errors.New("middleware function is nil")
}
store.neighborAdded = append(store.neighborAdded, mw)
middleware.Sort(store.neighborAdded)
case NeighborRemoved:
if mw.Func == nil {
return errors.New("middleware function is nil")
}
store.neighborRemoved = append(store.neighborRemoved, mw)
middleware.Sort(store.neighborRemoved)
case RelayPriority:
if mw.Func == nil {
return errors.New("middleware function is nil")
}
store.relayPriority = append(store.relayPriority, mw)
middleware.Sort(store.relayPriority)
default:
return errors.New("unknown middleware type")
}
return nil
}