mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-25 14:16:44 +00:00
fixed fetcher semaphores
This commit is contained in:
parent
947d696210
commit
8c3701c7c5
1 changed files with 21 additions and 9 deletions
|
|
@ -20,6 +20,7 @@ package les
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/core"
|
"github.com/ethereum/go-ethereum/core"
|
||||||
|
|
@ -35,6 +36,7 @@ type lightFetcher struct{
|
||||||
syncPoolMu sync.Mutex
|
syncPoolMu sync.Mutex
|
||||||
syncPool map[*peer]struct{}
|
syncPool map[*peer]struct{}
|
||||||
syncPoolNotify chan struct{}
|
syncPoolNotify chan struct{}
|
||||||
|
syncPoolNotified uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
func newLightFetcher(pm *ProtocolManager) *lightFetcher {
|
func newLightFetcher(pm *ProtocolManager) *lightFetcher {
|
||||||
|
|
@ -85,8 +87,10 @@ func (f *lightFetcher) notify(p *peer, block blockInfo) {
|
||||||
f.syncPoolMu.Lock()
|
f.syncPoolMu.Lock()
|
||||||
f.syncPool[p] = struct{}{}
|
f.syncPool[p] = struct{}{}
|
||||||
f.syncPoolMu.Unlock()
|
f.syncPoolMu.Unlock()
|
||||||
|
if atomic.SwapUint32(&f.syncPoolNotified, 1) == 0 {
|
||||||
f.syncPoolNotify <- struct{}{}
|
f.syncPoolNotify <- struct{}{}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *lightFetcher) fetchBestFromPool() *peer {
|
func (f *lightFetcher) fetchBestFromPool() *peer {
|
||||||
|
|
@ -113,20 +117,27 @@ func (f *lightFetcher) fetchBestFromPool() *peer {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *lightFetcher) syncLoop() {
|
func (f *lightFetcher) syncLoop() {
|
||||||
|
f.pm.wg.Add(1)
|
||||||
|
defer f.pm.wg.Done()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-f.pm.quitSync:
|
case <-f.pm.quitSync:
|
||||||
return
|
return
|
||||||
case <-f.syncPoolNotify:
|
case <-f.syncPoolNotify:
|
||||||
|
atomic.StoreUint32(&f.syncPoolNotified, 0)
|
||||||
chn := f.pm.getSyncLock(false)
|
chn := f.pm.getSyncLock(false)
|
||||||
if chn != nil {
|
if chn != nil {
|
||||||
|
if atomic.SwapUint32(&f.syncPoolNotified, 1) == 0 {
|
||||||
go func() {
|
go func() {
|
||||||
<-chn
|
<-chn
|
||||||
f.syncPoolNotify <- struct{}{}
|
f.syncPoolNotify <- struct{}{}
|
||||||
}()
|
}()
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if p := f.fetchBestFromPool(); p != nil {
|
if p := f.fetchBestFromPool(); p != nil {
|
||||||
go f.syncWithPeer(p)
|
go f.syncWithPeer(p)
|
||||||
|
if atomic.SwapUint32(&f.syncPoolNotified, 1) == 0 {
|
||||||
go func() {
|
go func() {
|
||||||
time.Sleep(softRequestTimeout)
|
time.Sleep(softRequestTimeout)
|
||||||
f.syncPoolNotify <- struct{}{}
|
f.syncPoolNotify <- struct{}{}
|
||||||
|
|
@ -135,6 +146,7 @@ func (f *lightFetcher) syncLoop() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *lightFetcher) syncWithPeer(p *peer) bool {
|
func (f *lightFetcher) syncWithPeer(p *peer) bool {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue