Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

les: fix fetcher syncing logic #18072

Merged
merged 1 commit into from
Nov 26, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 29 additions & 27 deletions les/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,36 +141,39 @@ func (f *lightFetcher) syncLoop() {
s := requesting
requesting = false
var (
rq *distReq
reqID uint64
rq *distReq
reqID uint64
syncing bool
)
if !f.syncing && !(newAnnounce && s) {
rq, reqID = f.nextRequest()
rq, reqID, syncing = f.nextRequest()
}
syncing := f.syncing
f.lock.Unlock()

if rq != nil {
requesting = true
_, ok := <-f.pm.reqDist.queue(rq)
if !ok {
if _, ok := <-f.pm.reqDist.queue(rq); ok {
if syncing {
f.lock.Lock()
f.syncing = true
f.lock.Unlock()
} else {
go func() {
time.Sleep(softRequestTimeout)
f.reqMu.Lock()
req, ok := f.requested[reqID]
if ok {
req.timeout = true
f.requested[reqID] = req
}
f.reqMu.Unlock()
// keep starting new requests while possible
f.requestChn <- false
}()
}
} else {
f.requestChn <- false
}

if !syncing {
go func() {
time.Sleep(softRequestTimeout)
f.reqMu.Lock()
req, ok := f.requested[reqID]
if ok {
req.timeout = true
f.requested[reqID] = req
}
f.reqMu.Unlock()
// keep starting new requests while possible
f.requestChn <- false
}()
}
}
case reqID := <-f.timeoutChn:
f.reqMu.Lock()
Expand Down Expand Up @@ -209,6 +212,7 @@ func (f *lightFetcher) syncLoop() {
f.checkSyncedHeaders(p)
f.syncing = false
f.lock.Unlock()
f.requestChn <- false
rjl493456442 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down Expand Up @@ -405,7 +409,7 @@ func (f *lightFetcher) requestedID(reqID uint64) bool {

// nextRequest selects the peer and announced head to be requested next, amount
// to be downloaded starting from the head backwards is also returned
func (f *lightFetcher) nextRequest() (*distReq, uint64) {
func (f *lightFetcher) nextRequest() (*distReq, uint64, bool) {
var (
bestHash common.Hash
bestAmount uint64
Expand All @@ -427,14 +431,12 @@ func (f *lightFetcher) nextRequest() (*distReq, uint64) {
}
}
if bestTd == f.maxConfirmedTd {
return nil, 0
return nil, 0, false
}

f.syncing = bestSyncing

var rq *distReq
reqID := genReqID()
if f.syncing {
if bestSyncing {
rq = &distReq{
getCost: func(dp distPeer) uint64 {
return 0
Expand Down Expand Up @@ -500,7 +502,7 @@ func (f *lightFetcher) nextRequest() (*distReq, uint64) {
},
}
}
return rq, reqID
return rq, reqID, bestSyncing
}

// deliverHeaders delivers header download request responses for processing
Expand Down