@@ -43,19 +43,28 @@ import (
43
43
44
44
*/
45
45
46
- const (
47
- DEF_DIGEST_WAIT_TIME = time .Duration (4 ) * time .Second
48
- DEF_REQUEST_WAIT_TIME = time .Duration (4 ) * time .Second
49
- DEF_RESPONSE_WAIT_TIME = time .Duration (7 ) * time .Second
50
- )
51
-
52
46
func init () {
53
47
rand .Seed (42 )
54
48
}
55
49
56
- var defaultDigestWaitTime = DEF_DIGEST_WAIT_TIME
57
- var defaultRequestWaitTime = DEF_REQUEST_WAIT_TIME
58
- var defaultResponseWaitTime = DEF_RESPONSE_WAIT_TIME
50
+ var digestWaitTime = time .Duration (4 ) * time .Second
51
+ var requestWaitTime = time .Duration (4 ) * time .Second
52
+ var responseWaitTime = time .Duration (7 ) * time .Second
53
+
54
+ // SetDigestWaitTime sets the digest wait time
55
+ func SetDigestWaitTime (time time.Duration ) {
56
+ digestWaitTime = time
57
+ }
58
+
59
+ // SetRequestWaitTime sets the request wait time
60
+ func SetRequestWaitTime (time time.Duration ) {
61
+ requestWaitTime = time
62
+ }
63
+
64
+ // SetResponseWaitTime sets the response wait time
65
+ func SetResponseWaitTime (time time.Duration ) {
66
+ responseWaitTime = time
67
+ }
59
68
60
69
// PullAdapter is needed by the PullEngine in order to
61
70
// send messages to the remote PullEngine instances.
@@ -83,6 +92,8 @@ type PullAdapter interface {
83
92
SendRes (items []uint64 , context interface {}, nonce uint64 )
84
93
}
85
94
95
+ // PullEngine is the component that actually invokes the pull algorithm
96
+ // with the help of the PullAdapter
86
97
type PullEngine struct {
87
98
PullAdapter
88
99
stopFlag int32
@@ -93,20 +104,24 @@ type PullEngine struct {
93
104
acceptingDigests int32
94
105
acceptingResponses int32
95
106
lock sync.Mutex
96
- nonces * util.Set
107
+ outgoingNONCES * util.Set
108
+ incomingNONCES * util.Set
97
109
}
98
110
111
+ // NewPullEngine creates an instance of a PullEngine with a certain sleep time
112
+ // between pull initiations
99
113
func NewPullEngine (participant PullAdapter , sleepTime time.Duration ) * PullEngine {
100
114
engine := & PullEngine {
101
- PullAdapter : participant ,
115
+ PullAdapter : participant ,
102
116
stopFlag : int32 (0 ),
103
117
state : util .NewSet (),
104
118
item2owners : make (map [uint64 ][]string ),
105
119
peers2nonces : make (map [string ]uint64 ),
106
120
nonces2peers : make (map [uint64 ]string ),
107
121
acceptingDigests : int32 (0 ),
108
122
acceptingResponses : int32 (0 ),
109
- nonces : util .NewSet (),
123
+ incomingNONCES : util .NewSet (),
124
+ outgoingNONCES : util .NewSet (),
110
125
}
111
126
112
127
go func () {
@@ -144,6 +159,7 @@ func (engine *PullEngine) ignoreDigests() {
144
159
atomic .StoreInt32 (& (engine .acceptingDigests ), int32 (0 ))
145
160
}
146
161
162
+ // Stop stops the engine
147
163
func (engine * PullEngine ) Stop () {
148
164
atomic .StoreInt32 (& (engine .stopFlag ), int32 (1 ))
149
165
}
@@ -155,13 +171,13 @@ func (engine *PullEngine) initiatePull() {
155
171
engine .acceptDigests ()
156
172
for _ , peer := range engine .SelectPeers () {
157
173
nonce := engine .newNONCE ()
158
- engine .nonces .Add (nonce )
174
+ engine .outgoingNONCES .Add (nonce )
159
175
engine .nonces2peers [nonce ] = peer
160
176
engine .peers2nonces [peer ] = nonce
161
177
engine .Hello (peer , nonce )
162
178
}
163
179
164
- time .AfterFunc (defaultDigestWaitTime , func () {
180
+ time .AfterFunc (digestWaitTime , func () {
165
181
engine .processIncomingDigests ()
166
182
})
167
183
}
@@ -189,7 +205,7 @@ func (engine *PullEngine) processIncomingDigests() {
189
205
engine .SendReq (dest , seqsToReq , engine .peers2nonces [dest ])
190
206
}
191
207
192
- time .AfterFunc (defaultResponseWaitTime , engine .endPull )
208
+ time .AfterFunc (responseWaitTime , engine .endPull )
193
209
194
210
}
195
211
@@ -198,15 +214,16 @@ func (engine *PullEngine) endPull() {
198
214
defer engine .lock .Unlock ()
199
215
200
216
atomic .StoreInt32 (& (engine .acceptingResponses ), int32 (0 ))
201
- engine .nonces .Clear ()
217
+ engine .outgoingNONCES .Clear ()
202
218
203
219
engine .item2owners = make (map [uint64 ][]string )
204
220
engine .peers2nonces = make (map [string ]uint64 )
205
221
engine .nonces2peers = make (map [uint64 ]string )
206
222
}
207
223
224
+ // OnDigest notifies the engine that a digest has arrived
208
225
func (engine * PullEngine ) OnDigest (digest []uint64 , nonce uint64 , context interface {}) {
209
- if ! engine .isAcceptingDigests () || ! engine .nonces .Exists (nonce ) {
226
+ if ! engine .isAcceptingDigests () || ! engine .outgoingNONCES .Exists (nonce ) {
210
227
return
211
228
}
212
229
@@ -226,22 +243,25 @@ func (engine *PullEngine) OnDigest(digest []uint64, nonce uint64, context interf
226
243
}
227
244
}
228
245
246
+ // Add adds items to the state
229
247
func (engine * PullEngine ) Add (seqs ... uint64 ) {
230
248
for _ , seq := range seqs {
231
249
engine .state .Add (seq )
232
250
}
233
251
}
234
252
253
+ // Remove removes items from the state
235
254
func (engine * PullEngine ) Remove (seqs ... uint64 ) {
236
255
for _ , seq := range seqs {
237
256
engine .state .Remove (seq )
238
257
}
239
258
}
240
259
260
+ // OnHello notifies the engine a hello has arrived
241
261
func (engine * PullEngine ) OnHello (nonce uint64 , context interface {}) {
242
- engine .nonces .Add (nonce )
243
- time .AfterFunc (defaultRequestWaitTime , func () {
244
- engine .nonces .Remove (nonce )
262
+ engine .incomingNONCES .Add (nonce )
263
+ time .AfterFunc (requestWaitTime , func () {
264
+ engine .incomingNONCES .Remove (nonce )
245
265
})
246
266
247
267
a := engine .state .ToArray ()
@@ -252,14 +272,15 @@ func (engine *PullEngine) OnHello(nonce uint64, context interface{}) {
252
272
engine .SendDigest (digest , nonce , context )
253
273
}
254
274
275
+ // OnReq notifies the engine a request has arrived
255
276
func (engine * PullEngine ) OnReq (items []uint64 , nonce uint64 , context interface {}) {
256
- if ! engine .nonces .Exists (nonce ) {
277
+ if ! engine .incomingNONCES .Exists (nonce ) {
257
278
return
258
279
}
259
280
engine .lock .Lock ()
260
281
defer engine .lock .Unlock ()
261
282
262
- items2Send := make ( []uint64 , 0 )
283
+ var items2Send []uint64
263
284
for _ , item := range items {
264
285
if engine .state .Exists (item ) {
265
286
items2Send = append (items2Send , item )
@@ -269,8 +290,9 @@ func (engine *PullEngine) OnReq(items []uint64, nonce uint64, context interface{
269
290
engine .SendRes (items2Send , context , nonce )
270
291
}
271
292
293
+ // OnRes notifies the engine a response has arrived
272
294
func (engine * PullEngine ) OnRes (items []uint64 , nonce uint64 ) {
273
- if ! engine .nonces .Exists (nonce ) || ! engine .isAcceptingResponses () {
295
+ if ! engine .outgoingNONCES .Exists (nonce ) || ! engine .isAcceptingResponses () {
274
296
return
275
297
}
276
298
@@ -281,7 +303,7 @@ func (engine *PullEngine) newNONCE() uint64 {
281
303
n := uint64 (0 )
282
304
for {
283
305
n = uint64 (rand .Int63 ())
284
- if ! engine .nonces .Exists (n ) {
306
+ if ! engine .outgoingNONCES .Exists (n ) {
285
307
return n
286
308
}
287
309
}
0 commit comments