Skip to content

Commit

Permalink
les: protocol manager wait group filled in main goroutine
Browse files Browse the repository at this point in the history
  • Loading branch information
JekaMas committed Nov 7, 2017
1 parent 853d5d3 commit a231fdb
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 17 deletions.
11 changes: 3 additions & 8 deletions les/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package les
import (
"math/big"
"sync"
"sync/atomic"
"time"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -118,21 +117,17 @@ func newLightFetcher(pm *ProtocolManager) *lightFetcher {
maxConfirmedTd: big.NewInt(0),
}
pm.peers.notify(f)

f.pm.wg.Add(1)
go f.syncLoop()
return f
}

// syncLoop is the main event loop of the light fetcher
func (f *lightFetcher) syncLoop() {
once := true
requesting := false
defer f.pm.wg.Done()
for {
if once && atomic.LoadInt32(f.pm.isClosed) != closed {
once = false
f.pm.wg.Add(1)
defer f.pm.wg.Done()
}

select {
case <-f.pm.quitSync:
return
Expand Down
9 changes: 0 additions & 9 deletions les/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"math/big"
"net"
"sync"
"sync/atomic"
"time"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -125,16 +124,10 @@ type ProtocolManager struct {
wg *sync.WaitGroup
}

const (
started int32 = iota
closed
)

// NewProtocolManager returns a new ethereum sub protocol manager. The Ethereum sub protocol manages peers capable
// with the ethereum network.
func NewProtocolManager(chainConfig *params.ChainConfig, lightSync bool, networkId uint64, mux *event.TypeMux, engine consensus.Engine, peers *peerSet, blockchain BlockChain, txpool txPool, chainDb ethdb.Database, odr *LesOdr, txrelay *LesTxRelay, quitSync chan struct{}, wg *sync.WaitGroup) (*ProtocolManager, error) {
// Create the protocol manager with the base fields
isClosed := started
manager := &ProtocolManager{
lightSync: lightSync,
eventMux: mux,
Expand All @@ -147,7 +140,6 @@ func NewProtocolManager(chainConfig *params.ChainConfig, lightSync bool, network
txrelay: txrelay,
peers: peers,
newPeerCh: make(chan *peer),
isClosed: &isClosed,
quitSync: quitSync,
wg: wg,
noMorePeers: make(chan struct{}),
Expand Down Expand Up @@ -252,7 +244,6 @@ func (pm *ProtocolManager) Stop() {
pm.peers.Close()

// Wait for any process action. Wait should be executed after all pm.wg.Add()
atomic.StoreInt32(pm.isClosed, closed)
pm.wg.Wait()

log.Info("Light Ethereum protocol stopped")
Expand Down

0 comments on commit a231fdb

Please sign in to comment.