[NOD-744] Wrap go routines with spawn (#626)

* [NOD-744] Wrap go routines with spawn

* [NOD-747] Wrap some more go routines with spawn

* [NOD-744] Some more missing go routines

* [NOD-744] Break lines so make code more readable

* [NOD-744] Declare a local scope variable so the func would use it.

* [NOD-744] Fix type and update comment.

* [NOD-744] Declare local var so go routine would use it

* [NOD-744] Rename variable, use normal assignment;

* [NOD-744] Rename variable.
This commit is contained in:
Dan Aharoni 2020-02-13 13:10:07 +02:00 committed by GitHub
parent 51ff9e2562
commit 7567cd4cb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 48 additions and 22 deletions

View File

@ -60,8 +60,12 @@ func mineLoop(client *minerClient, numberOfBlocks uint64, blockDelay uint64) err
func mineNextBlock(client *minerClient, foundBlock chan *util.Block, templateStopChan chan struct{}, errChan chan error) {
newTemplateChan := make(chan *rpcmodel.GetBlockTemplateResult)
go templatesLoop(client, newTemplateChan, errChan, templateStopChan)
go solveLoop(newTemplateChan, foundBlock, errChan)
spawn(func() {
templatesLoop(client, newTemplateChan, errChan, templateStopChan)
})
spawn(func() {
solveLoop(newTemplateChan, foundBlock, errChan)
})
}
func handleFoundBlock(client *minerClient, block *util.Block) error {
@ -193,7 +197,9 @@ func solveLoop(newTemplateChan chan *rpcmodel.GetBlockTemplateResult, foundBlock
return
}
go solveBlock(block, stopOldTemplateSolving, foundBlock)
spawn(func() {
solveBlock(block, stopOldTemplateSolving, foundBlock)
})
}
if stopOldTemplateSolving != nil {
close(stopOldTemplateSolving)

View File

@ -375,7 +375,9 @@ out:
}
if cm.cfg.OnDisconnection != nil {
go cm.cfg.OnDisconnection(connReq)
spawn(func() {
cm.cfg.OnDisconnection(connReq)
}, cm.handlePanic)
}
// All internal state has been cleaned up, if
@ -572,7 +574,9 @@ func (cm *ConnManager) listenHandler(listener net.Listener) {
}
continue
}
go cm.cfg.OnAccept(conn)
spawn(func() {
cm.cfg.OnAccept(conn)
}, cm.handlePanic)
}
cm.wg.Done()
@ -593,9 +597,14 @@ func (cm *ConnManager) Start() {
// Start all the listeners so long as the caller requested them and
// provided a callback to be invoked when connections are accepted.
if cm.cfg.OnAccept != nil {
for _, listner := range cm.cfg.Listeners {
for _, listener := range cm.cfg.Listeners {
// Declaring this variable is necessary as it needs be declared in the same
// scope of the anonymous function below it.
listenerCopy := listener
cm.wg.Add(1)
go cm.listenHandler(listner)
spawn(func() {
cm.listenHandler(listenerCopy)
}, cm.handlePanic)
}
}

View File

@ -66,7 +66,7 @@ func SeedFromDNS(dagParams *dagconfig.Params, reqServices wire.ServiceFlag, incl
}
}
go func(host string) {
spawn(func() {
randSource := mrand.New(mrand.NewSource(time.Now().UnixNano()))
seedpeers, err := lookupFn(host)
@ -94,6 +94,6 @@ func SeedFromDNS(dagParams *dagconfig.Params, reqServices wire.ServiceFlag, incl
}
seedFn(addresses)
}(host)
}, nil)
}
}

View File

@ -301,7 +301,9 @@ func (bi *blockImporter) Import() chan *importResults {
// Start the status handler and return the result channel that it will
// send the results on when the import is done.
resultChan := make(chan *importResults)
go bi.statusHandler(resultChan)
spawn(func() {
bi.statusHandler(resultChan)
})
return resultChan
}

View File

@ -426,7 +426,9 @@ out:
}
break out
}
go c.handleMessage(msg)
spawn(func() {
c.handleMessage(msg)
})
}
// Ensure the connection is closed.

View File

@ -923,9 +923,11 @@ func (s *Server) handleQuery(state *peerState, querymsg interface{}) {
}
// TODO: if too many, nuke a non-perm peer.
go s.connManager.Connect(&connmgr.ConnReq{
Addr: netAddr,
Permanent: msg.Permanent,
spawn(func() {
s.connManager.Connect(&connmgr.ConnReq{
Addr: netAddr,
Permanent: msg.Permanent,
})
})
msg.Reply <- nil
case RemoveNodeMsg:
@ -1751,9 +1753,11 @@ func NewServer(listenAddrs []string, db database.DB, dagParams *dagconfig.Params
return nil, err
}
go s.connManager.Connect(&connmgr.ConnReq{
Addr: netAddr,
Permanent: true,
spawn(func() {
s.connManager.Connect(&connmgr.ConnReq{
Addr: netAddr,
Permanent: true,
})
})
}

View File

@ -623,12 +623,15 @@ func (s *Server) Start() {
for _, listener := range s.cfg.Listeners {
s.wg.Add(1)
go func(listener net.Listener) {
log.Infof("RPC server listening on %s", listener.Addr())
httpServer.Serve(listener)
log.Tracef("RPC listener done for %s", listener.Addr())
// Declaring this variable is necessary as it needs be declared in the same
// scope of the anonymous function below it.
listenerCopy := listener
spawn(func() {
log.Infof("RPC server listening on %s", listenerCopy.Addr())
httpServer.Serve(listenerCopy)
log.Tracef("RPC listener done for %s", listenerCopy.Addr())
s.wg.Done()
}(listener)
})
}
s.ntfnMgr.Start()