server: use nil errChan for broadcasts

This skips creating errChans when sending messages to
peer during broadcast. This should be a minor memory
optimization, as well as not requiring channel sends
on those which will never be read.
This commit is contained in:
Conner Fromknecht 2018-05-07 18:36:08 -07:00
parent 0691b21a30
commit d20adc8e0e
No known key found for this signature in database
GPG Key ID: 39DE78FBE6ACB0EF

@ -1091,7 +1091,7 @@ func (s *server) SendToPeer(target *btcec.PublicKey,
case err := <-errChan: case err := <-errChan:
return err return err
case <-targetPeer.quit: case <-targetPeer.quit:
return fmt.Errorf("peer shutting down") return ErrPeerExiting
case <-s.quit: case <-s.quit:
return ErrServerShuttingDown return ErrServerShuttingDown
} }
@ -1183,7 +1183,8 @@ func (s *server) sendPeerMessages(
// event, we defer a call to Done on both WaitGroups to 1) ensure that // event, we defer a call to Done on both WaitGroups to 1) ensure that
// server will be able to shutdown after its go routines exit, and 2) // server will be able to shutdown after its go routines exit, and 2)
// so the server can return to the caller of BroadcastMessage. // so the server can return to the caller of BroadcastMessage.
if wg != nil { isBroadcast := wg != nil
if isBroadcast {
defer s.wg.Done() defer s.wg.Done()
defer wg.Done() defer wg.Done()
} }
@ -1193,9 +1194,16 @@ func (s *server) sendPeerMessages(
// the queue. // the queue.
var errChans []chan error var errChans []chan error
for _, msg := range msgs { for _, msg := range msgs {
errChan := make(chan error, 1) // If this is not broadcast, create error channels to provide
// synchronous feedback regarding the delivery of the message to
// a specific peer.
var errChan chan error
if !isBroadcast {
errChan = make(chan error, 1)
errChans = append(errChans, errChan)
}
targetPeer.queueMsg(msg, errChan) targetPeer.queueMsg(msg, errChan)
errChans = append(errChans, errChan)
} }
return errChans return errChans