server: asynchronously handle internal broadcast/send requests

This commit modifies the request handling within the sever’s
queryHandler goroutine to ensure that requests from the ChannelRouter
or other related sub-systems don’t block the main processing loop.

We do this simply by launching a goroutine to handle the dispatch of
the request.
This commit is contained in:
Olaoluwa Osuntokun 2017-01-24 17:06:23 -08:00
parent ddc3c3ab35
commit 73d5daa2c3
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
1 changed files with 41 additions and 29 deletions

View File

@ -578,6 +578,11 @@ out:
srvrLog.Debugf("Broadcasting %v messages", len(bMsg.msgs))
// Launch a new goroutine to handle the broadcast
// request, this allows us process this request
// asynchronously without blocking subsequent broadcast
// requests.
go func() {
s.peersMtx.RLock()
for _, peer := range s.peersByPub {
if ignore != nil &&
@ -596,6 +601,7 @@ out:
s.peersMtx.RUnlock()
bMsg.errChan <- nil
}()
case sMsg := <-s.sendRequests:
// TODO(roasbeef): use [33]byte everywhere instead
// * eliminate usage of mutexes, funnel all peer
@ -605,6 +611,10 @@ out:
srvrLog.Debugf("Attempting to send msgs %v to: %x",
len(sMsg.msgs), target)
// Launch a new goroutine to handle this send request,
// this allows us process this request asynchronously
// without blocking future send requests.
go func() {
s.peersMtx.RLock()
targetPeer, ok := s.peersByPub[string(target)]
if !ok {
@ -612,7 +622,7 @@ out:
srvrLog.Errorf("unable to send message to %x, "+
"peer not found", target)
sMsg.errChan <- errors.New("peer not found")
continue
return
}
for _, msg := range sMsg.msgs {
@ -621,6 +631,7 @@ out:
s.peersMtx.RUnlock()
sMsg.errChan <- nil
}()
case query := <-s.queries:
switch msg := query.(type) {
case *connectPeerMsg:
@ -695,6 +706,7 @@ func (s *server) handleConnectPeer(msg *connectPeerMsg) {
Addr: addr,
Permanent: true,
})
msg.err <- nil
} else {
// If we're not making a persistent connection, then we'll
// attempt to connect o the target peer, returning an error