@@ -77,18 +77,18 @@ type Session struct {
77
77
tickDelayReqs chan time.Duration
78
78
79
79
// do not touch outside run loop
80
- tofetch * cidQueue
81
- interest * lru.Cache
82
- pastWants * cidQueue
83
- liveWants map [cid.Cid ]time.Time
84
- tick * time.Timer
85
- rebroadcast * time.Timer
86
- baseTickDelay time.Duration
87
- latTotal time.Duration
88
- fetchcnt int
89
- consecutiveTicks int
90
- provSearchDelay time.Duration
91
- rebroadcastDelay delay.D
80
+ tofetch * cidQueue
81
+ interest * lru.Cache
82
+ pastWants * cidQueue
83
+ liveWants map [cid.Cid ]time.Time
84
+ idleTick * time.Timer
85
+ periodicSearchTimer * time.Timer
86
+ baseTickDelay time.Duration
87
+ latTotal time.Duration
88
+ fetchcnt int
89
+ consecutiveTicks int
90
+ initialSearchDelay time.Duration
91
+ periodicSearchDelay delay.D
92
92
// identifiers
93
93
notif notifications.PubSub
94
94
uuid logging.Loggable
@@ -102,28 +102,28 @@ func New(ctx context.Context,
102
102
wm WantManager ,
103
103
pm PeerManager ,
104
104
srs RequestSplitter ,
105
- provSearchDelay time.Duration ,
106
- rebroadcastDelay delay.D ) * Session {
105
+ initialSearchDelay time.Duration ,
106
+ periodicSearchDelay delay.D ) * Session {
107
107
s := & Session {
108
- liveWants : make (map [cid.Cid ]time.Time ),
109
- newReqs : make (chan []cid.Cid ),
110
- cancelKeys : make (chan []cid.Cid ),
111
- tofetch : newCidQueue (),
112
- pastWants : newCidQueue (),
113
- interestReqs : make (chan interestReq ),
114
- latencyReqs : make (chan chan time.Duration ),
115
- tickDelayReqs : make (chan time.Duration ),
116
- ctx : ctx ,
117
- wm : wm ,
118
- pm : pm ,
119
- srs : srs ,
120
- incoming : make (chan blkRecv ),
121
- notif : notifications .New (),
122
- uuid : loggables .Uuid ("GetBlockRequest" ),
123
- baseTickDelay : time .Millisecond * 500 ,
124
- id : id ,
125
- provSearchDelay : provSearchDelay ,
126
- rebroadcastDelay : rebroadcastDelay ,
108
+ liveWants : make (map [cid.Cid ]time.Time ),
109
+ newReqs : make (chan []cid.Cid ),
110
+ cancelKeys : make (chan []cid.Cid ),
111
+ tofetch : newCidQueue (),
112
+ pastWants : newCidQueue (),
113
+ interestReqs : make (chan interestReq ),
114
+ latencyReqs : make (chan chan time.Duration ),
115
+ tickDelayReqs : make (chan time.Duration ),
116
+ ctx : ctx ,
117
+ wm : wm ,
118
+ pm : pm ,
119
+ srs : srs ,
120
+ incoming : make (chan blkRecv ),
121
+ notif : notifications .New (),
122
+ uuid : loggables .Uuid ("GetBlockRequest" ),
123
+ baseTickDelay : time .Millisecond * 500 ,
124
+ id : id ,
125
+ initialSearchDelay : initialSearchDelay ,
126
+ periodicSearchDelay : periodicSearchDelay ,
127
127
}
128
128
129
129
cache , _ := lru .New (2048 )
@@ -239,8 +239,8 @@ func (s *Session) SetBaseTickDelay(baseTickDelay time.Duration) {
239
239
// Session run loop -- everything function below here should not be called
240
240
// of this loop
241
241
func (s * Session ) run (ctx context.Context ) {
242
- s .tick = time .NewTimer (s .provSearchDelay )
243
- s .rebroadcast = time .NewTimer (s .rebroadcastDelay . Get ())
242
+ s .idleTick = time .NewTimer (s .initialSearchDelay )
243
+ s .periodicSearchTimer = time .NewTimer (s .periodicSearchDelay . NextWaitTime ())
244
244
for {
245
245
select {
246
246
case blk := <- s .incoming :
@@ -253,10 +253,10 @@ func (s *Session) run(ctx context.Context) {
253
253
s .handleNewRequest (ctx , keys )
254
254
case keys := <- s .cancelKeys :
255
255
s .handleCancel (keys )
256
- case <- s .tick .C :
257
- s .handleTick (ctx )
258
- case <- s .rebroadcast .C :
259
- s .handleRebroadcast (ctx )
256
+ case <- s .idleTick .C :
257
+ s .handleIdleTick (ctx )
258
+ case <- s .periodicSearchTimer .C :
259
+ s .handlePeriodicSearch (ctx )
260
260
case lwchk := <- s .interestReqs :
261
261
lwchk .resp <- s .cidIsWanted (lwchk .c )
262
262
case resp := <- s .latencyReqs :
@@ -271,15 +271,15 @@ func (s *Session) run(ctx context.Context) {
271
271
}
272
272
273
273
func (s * Session ) handleIncomingBlock (ctx context.Context , blk blkRecv ) {
274
- s .tick .Stop ()
274
+ s .idleTick .Stop ()
275
275
276
276
if blk .from != "" {
277
277
s .pm .RecordPeerResponse (blk .from , blk .blk .Cid ())
278
278
}
279
279
280
280
s .receiveBlock (ctx , blk .blk )
281
281
282
- s .resetTick ()
282
+ s .resetIdleTick ()
283
283
}
284
284
285
285
func (s * Session ) handleNewRequest (ctx context.Context , keys []cid.Cid ) {
@@ -307,7 +307,7 @@ func (s *Session) handleCancel(keys []cid.Cid) {
307
307
}
308
308
}
309
309
310
- func (s * Session ) handleTick (ctx context.Context ) {
310
+ func (s * Session ) handleIdleTick (ctx context.Context ) {
311
311
312
312
live := make ([]cid.Cid , 0 , len (s .liveWants ))
313
313
now := time .Now ()
@@ -321,28 +321,29 @@ func (s *Session) handleTick(ctx context.Context) {
321
321
s .wm .WantBlocks (ctx , live , nil , s .id )
322
322
323
323
// do no find providers on consecutive ticks
324
- // -- just rely on periodic rebroadcast
324
+ // -- just rely on periodic search widening
325
325
if len (live ) > 0 && (s .consecutiveTicks == 0 ) {
326
326
s .pm .FindMorePeers (ctx , live [0 ])
327
327
}
328
- s .resetTick ()
328
+ s .resetIdleTick ()
329
329
330
330
if len (s .liveWants ) > 0 {
331
331
s .consecutiveTicks ++
332
332
}
333
333
}
334
334
335
- func (s * Session ) handleRebroadcast (ctx context.Context ) {
336
-
337
- if len ( s . liveWants ) == 0 {
335
+ func (s * Session ) handlePeriodicSearch (ctx context.Context ) {
336
+ randomWant := s . randomLiveWant ()
337
+ if ! randomWant . Defined () {
338
338
return
339
339
}
340
340
341
341
// TODO: come up with a better strategy for determining when to search
342
342
// for new providers for blocks.
343
- s .pm .FindMorePeers (ctx , s .randomLiveWant ())
343
+ s .pm .FindMorePeers (ctx , randomWant )
344
+ s .wm .WantBlocks (ctx , []cid.Cid {randomWant }, nil , s .id )
344
345
345
- s .rebroadcast .Reset (s .rebroadcastDelay . Get ())
346
+ s .periodicSearchTimer .Reset (s .periodicSearchDelay . NextWaitTime ())
346
347
}
347
348
348
349
func (s * Session ) randomLiveWant () cid.Cid {
@@ -357,7 +358,7 @@ func (s *Session) randomLiveWant() cid.Cid {
357
358
return cid.Cid {}
358
359
}
359
360
func (s * Session ) handleShutdown () {
360
- s .tick .Stop ()
361
+ s .idleTick .Stop ()
361
362
s .notif .Shutdown ()
362
363
363
364
live := make ([]cid.Cid , 0 , len (s .liveWants ))
@@ -436,16 +437,16 @@ func (s *Session) averageLatency() time.Duration {
436
437
return s .latTotal / time .Duration (s .fetchcnt )
437
438
}
438
439
439
- func (s * Session ) resetTick () {
440
+ func (s * Session ) resetIdleTick () {
440
441
var tickDelay time.Duration
441
442
if s .latTotal == 0 {
442
- tickDelay = s .provSearchDelay
443
+ tickDelay = s .initialSearchDelay
443
444
} else {
444
445
avLat := s .averageLatency ()
445
446
tickDelay = s .baseTickDelay + (3 * avLat )
446
447
}
447
448
tickDelay = tickDelay * time .Duration (1 + s .consecutiveTicks )
448
- s .tick .Reset (tickDelay )
449
+ s .idleTick .Reset (tickDelay )
449
450
}
450
451
451
452
func (s * Session ) wantBudget () int {
0 commit comments