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 committed 2017-01-24 17:06:32 -08:00
1 parent ddc3c3ab35
commit 73d5daa2c3
1 file changed
+13 -1
+13 -1
View File
@@ -578,6 +578,11 @@ out:
srvrLog.Debugf("Broadcasting %v messages", len(bMsg.msgs)) 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() s.peersMtx.RLock()
for _, peer := range s.peersByPub { for _, peer := range s.peersByPub {
if ignore != nil && if ignore != nil &&
@@ -596,6 +601,7 @@ out:
s.peersMtx.RUnlock() s.peersMtx.RUnlock()
bMsg.errChan <- nil bMsg.errChan <- nil
}()
case sMsg := <-s.sendRequests: case sMsg := <-s.sendRequests:
// TODO(roasbeef): use [33]byte everywhere instead // TODO(roasbeef): use [33]byte everywhere instead
// * eliminate usage of mutexes, funnel all peer // * eliminate usage of mutexes, funnel all peer
@@ -605,6 +611,10 @@ out:
srvrLog.Debugf("Attempting to send msgs %v to: %x", srvrLog.Debugf("Attempting to send msgs %v to: %x",
len(sMsg.msgs), target) 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() s.peersMtx.RLock()
targetPeer, ok := s.peersByPub[string(target)] targetPeer, ok := s.peersByPub[string(target)]
if !ok { if !ok {
@@ -612,7 +622,7 @@ out:
srvrLog.Errorf("unable to send message to %x, "+ srvrLog.Errorf("unable to send message to %x, "+
"peer not found", target) "peer not found", target)
sMsg.errChan <- errors.New("peer not found") sMsg.errChan <- errors.New("peer not found")
continue return
} }
for _, msg := range sMsg.msgs { for _, msg := range sMsg.msgs {
@@ -621,6 +631,7 @@ out:
s.peersMtx.RUnlock() s.peersMtx.RUnlock()
sMsg.errChan <- nil sMsg.errChan <- nil
}()
case query := <-s.queries: case query := <-s.queries:
switch msg := query.(type) { switch msg := query.(type) {
case *connectPeerMsg: case *connectPeerMsg:
@@ -695,6 +706,7 @@ func (s *server) handleConnectPeer(msg *connectPeerMsg) {
Addr: addr, Addr: addr,
Permanent: true, Permanent: true,
}) })
msg.err <- nil
} else { } else {
// If we're not making a persistent connection, then we'll // If we're not making a persistent connection, then we'll
// attempt to connect o the target peer, returning an error // attempt to connect o the target peer, returning an error