@@ -28,12 +28,13 @@ import (
28
28
// Client implements the ChainSync client
29
29
type Client struct {
30
30
* protocol.Protocol
31
- config * Config
32
- callbackContext CallbackContext
33
- busyMutex sync.Mutex
34
- readyForNextBlockChan chan bool
35
- onceStart sync.Once
36
- onceStop sync.Once
31
+ config * Config
32
+ callbackContext CallbackContext
33
+ busyMutex sync.Mutex
34
+ readyForNextBlockChan chan bool
35
+ onceStart sync.Once
36
+ onceStop sync.Once
37
+ syncPipelinedRequestNext int
37
38
38
39
// waitingForCurrentTipChan will process all the requests for the current tip until the channel
39
40
// is empty.
@@ -404,8 +405,8 @@ func (c *Client) Sync(intersectPoints []common.Point) error {
404
405
}
405
406
406
407
intersectResultChan , cancel := c .wantIntersectFound ()
407
- msg := NewMsgFindIntersect (intersectPoints )
408
- if err := c .SendMessage (msg ); err != nil {
408
+ msgFindIntersect := NewMsgFindIntersect (intersectPoints )
409
+ if err := c .SendMessage (msgFindIntersect ); err != nil {
409
410
cancel ()
410
411
return err
411
412
}
@@ -418,14 +419,14 @@ func (c *Client) Sync(intersectPoints []common.Point) error {
418
419
}
419
420
}
420
421
421
- // Pipeline the initial block requests to speed things up a bit
422
- // Using a value higher than 10 seems to cause problems with NtN
423
- for i := 0 ; i <= c .config .PipelineLimit ; i ++ {
424
- msg := NewMsgRequestNext ()
425
- if err := c .SendMessage (msg ); err != nil {
426
- return err
427
- }
422
+ // Send initial RequestNext
423
+ msgRequestNext := NewMsgRequestNext ()
424
+ if err := c .SendMessage (msgRequestNext ); err != nil {
425
+ return err
428
426
}
427
+ // Reset pipelined message counter
428
+ c .syncPipelinedRequestNext = 0
429
+ // Start sync loop
429
430
go c .syncLoop ()
430
431
return nil
431
432
}
@@ -441,15 +442,23 @@ func (c *Client) syncLoop() {
441
442
return
442
443
}
443
444
c .busyMutex .Lock ()
444
- // Request the next block
445
- // In practice we already have multiple block requests pipelined
446
- // and this just adds another one to the pile
447
- msg := NewMsgRequestNext ()
448
- if err := c .SendMessage (msg ); err != nil {
449
- c .SendError (err )
445
+ // Wait for next block if we have pipelined messages
446
+ if c .syncPipelinedRequestNext > 0 {
447
+ c .syncPipelinedRequestNext --
450
448
c .busyMutex .Unlock ()
451
- return
449
+ continue
450
+ }
451
+ // Request the next block(s)
452
+ msgCount := max (c .config .PipelineLimit , 1 )
453
+ for i := 0 ; i < msgCount ; i ++ {
454
+ msg := NewMsgRequestNext ()
455
+ if err := c .SendMessage (msg ); err != nil {
456
+ c .SendError (err )
457
+ c .busyMutex .Unlock ()
458
+ return
459
+ }
452
460
}
461
+ c .syncPipelinedRequestNext = msgCount - 1
453
462
c .busyMutex .Unlock ()
454
463
}
455
464
}
0 commit comments