@@ -141,36 +141,39 @@ func (f *lightFetcher) syncLoop() {
141
141
s := requesting
142
142
requesting = false
143
143
var (
144
- rq * distReq
145
- reqID uint64
144
+ rq * distReq
145
+ reqID uint64
146
+ syncing bool
146
147
)
147
148
if ! f .syncing && ! (newAnnounce && s ) {
148
- rq , reqID = f .nextRequest ()
149
+ rq , reqID , syncing = f .nextRequest ()
149
150
}
150
- syncing := f .syncing
151
151
f .lock .Unlock ()
152
152
153
153
if rq != nil {
154
154
requesting = true
155
- _ , ok := <- f .pm .reqDist .queue (rq )
156
- if ! ok {
155
+ if _ , ok := <- f .pm .reqDist .queue (rq ); ok {
156
+ if syncing {
157
+ f .lock .Lock ()
158
+ f .syncing = true
159
+ f .lock .Unlock ()
160
+ } else {
161
+ go func () {
162
+ time .Sleep (softRequestTimeout )
163
+ f .reqMu .Lock ()
164
+ req , ok := f .requested [reqID ]
165
+ if ok {
166
+ req .timeout = true
167
+ f .requested [reqID ] = req
168
+ }
169
+ f .reqMu .Unlock ()
170
+ // keep starting new requests while possible
171
+ f .requestChn <- false
172
+ }()
173
+ }
174
+ } else {
157
175
f .requestChn <- false
158
176
}
159
-
160
- if ! syncing {
161
- go func () {
162
- time .Sleep (softRequestTimeout )
163
- f .reqMu .Lock ()
164
- req , ok := f .requested [reqID ]
165
- if ok {
166
- req .timeout = true
167
- f .requested [reqID ] = req
168
- }
169
- f .reqMu .Unlock ()
170
- // keep starting new requests while possible
171
- f .requestChn <- false
172
- }()
173
- }
174
177
}
175
178
case reqID := <- f .timeoutChn :
176
179
f .reqMu .Lock ()
@@ -209,6 +212,7 @@ func (f *lightFetcher) syncLoop() {
209
212
f .checkSyncedHeaders (p )
210
213
f .syncing = false
211
214
f .lock .Unlock ()
215
+ f .requestChn <- false
212
216
}
213
217
}
214
218
}
@@ -405,7 +409,7 @@ func (f *lightFetcher) requestedID(reqID uint64) bool {
405
409
406
410
// nextRequest selects the peer and announced head to be requested next, amount
407
411
// to be downloaded starting from the head backwards is also returned
408
- func (f * lightFetcher ) nextRequest () (* distReq , uint64 ) {
412
+ func (f * lightFetcher ) nextRequest () (* distReq , uint64 , bool ) {
409
413
var (
410
414
bestHash common.Hash
411
415
bestAmount uint64
@@ -427,14 +431,12 @@ func (f *lightFetcher) nextRequest() (*distReq, uint64) {
427
431
}
428
432
}
429
433
if bestTd == f .maxConfirmedTd {
430
- return nil , 0
434
+ return nil , 0 , false
431
435
}
432
436
433
- f .syncing = bestSyncing
434
-
435
437
var rq * distReq
436
438
reqID := genReqID ()
437
- if f . syncing {
439
+ if bestSyncing {
438
440
rq = & distReq {
439
441
getCost : func (dp distPeer ) uint64 {
440
442
return 0
@@ -500,7 +502,7 @@ func (f *lightFetcher) nextRequest() (*distReq, uint64) {
500
502
},
501
503
}
502
504
}
503
- return rq , reqID
505
+ return rq , reqID , bestSyncing
504
506
}
505
507
506
508
// deliverHeaders delivers header download request responses for processing
0 commit comments