aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwmin0 <wmin0@hotmail.com>2019-01-04 16:28:47 +0800
committerWei-Ning Huang <w@dexon.org>2019-01-04 16:28:47 +0800
commit17b96ca95ffd4cbf8af762a974fc8b18f2c1b993 (patch)
tree10df60f30c05b731e81755cfd3e4e57625b1630b
parentbeaef47dd5dd4b342cef3c8b26508ce3c334be06 (diff)
downloadtangerine-consensus-17b96ca95ffd4cbf8af762a974fc8b18f2c1b993.tar
tangerine-consensus-17b96ca95ffd4cbf8af762a974fc8b18f2c1b993.tar.gz
tangerine-consensus-17b96ca95ffd4cbf8af762a974fc8b18f2c1b993.tar.bz2
tangerine-consensus-17b96ca95ffd4cbf8af762a974fc8b18f2c1b993.tar.lz
tangerine-consensus-17b96ca95ffd4cbf8af762a974fc8b18f2c1b993.tar.xz
tangerine-consensus-17b96ca95ffd4cbf8af762a974fc8b18f2c1b993.tar.zst
tangerine-consensus-17b96ca95ffd4cbf8af762a974fc8b18f2c1b993.zip
core: syncer: safe spawn go routine (#399)
-rw-r--r--core/syncer/agreement.go7
-rw-r--r--core/syncer/consensus.go9
2 files changed, 6 insertions, 10 deletions
diff --git a/core/syncer/agreement.go b/core/syncer/agreement.go
index 32ea654..08be77a 100644
--- a/core/syncer/agreement.go
+++ b/core/syncer/agreement.go
@@ -18,8 +18,6 @@
package syncer
import (
- "sync"
-
"github.com/dexon-foundation/dexon-consensus/common"
"github.com/dexon-foundation/dexon-consensus/core"
"github.com/dexon-foundation/dexon-consensus/core/types"
@@ -29,7 +27,6 @@ import (
// Struct agreement implements struct of BA (Byzantine Agreement) protocol
// needed in syncer, which only receives agreement results.
type agreement struct {
- wg *sync.WaitGroup
cache *utils.NodeSetCache
inputChan chan interface{}
outputChan chan<- *types.Block
@@ -47,12 +44,10 @@ func newAgreement(
ch chan<- *types.Block,
pullChan chan<- common.Hash,
cache *utils.NodeSetCache,
- wg *sync.WaitGroup,
logger common.Logger) *agreement {
return &agreement{
cache: cache,
- wg: wg,
inputChan: make(chan interface{}, 1000),
outputChan: ch,
pullChan: pullChan,
@@ -68,8 +63,6 @@ func newAgreement(
// run starts the agreement, this does not start a new routine, go a new
// routine explicitly in the caller.
func (a *agreement) run() {
- a.wg.Add(1)
- defer a.wg.Done()
for {
select {
case val, ok := <-a.inputChan:
diff --git a/core/syncer/consensus.go b/core/syncer/consensus.go
index d334bbd..8852a47 100644
--- a/core/syncer/consensus.go
+++ b/core/syncer/consensus.go
@@ -623,10 +623,13 @@ func (con *Consensus) resizeByNumChains(numChains uint32) {
// Resize the pool of blocks.
con.blocks = append(con.blocks, types.ByPosition{})
// Resize agreement modules.
- a := newAgreement(con.receiveChan, con.pullChan, con.nodeSetCache,
- &con.agreementWaitGroup, con.logger)
+ a := newAgreement(con.receiveChan, con.pullChan, con.nodeSetCache, con.logger)
con.agreements = append(con.agreements, a)
- go a.run()
+ con.agreementWaitGroup.Add(1)
+ go func() {
+ defer con.agreementWaitGroup.Done()
+ a.run()
+ }()
}
}
}