server: return error from sendPeerMessages
This commit adds a return error to sendPeerMessages, making it possible to observe if a message sent to a peer fails or succeeds.
This commit is contained in:
parent
25b77a0aee
commit
8a79bbf383
30
server.go
30
server.go
@ -950,15 +950,27 @@ func (s *server) sendToPeer(target *btcec.PublicKey,
|
||||
return err
|
||||
}
|
||||
|
||||
s.sendPeerMessages(targetPeer, msgs, nil)
|
||||
|
||||
// Send messages to the peer and return any error from
|
||||
// sending a message.
|
||||
errChans := s.sendPeerMessages(targetPeer, msgs, nil)
|
||||
for _, errChan := range errChans {
|
||||
select {
|
||||
case err := <-errChan:
|
||||
return err
|
||||
case <-s.quit:
|
||||
return ErrServerShuttingDown
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// sendPeerMessages enqueues a list of messages into the outgoingQueue of the
|
||||
// `targetPeer`. This method supports additional broadcast-level
|
||||
// synchronization by using the additional `wg` to coordinate a particular
|
||||
// broadcast.
|
||||
// broadcast. Since this method will wait for the return error from sending
|
||||
// each message, it should be run as a goroutine (see comment below) and
|
||||
// the error ignored if used for broadcasting messages, where the result
|
||||
// from sending the messages is not of importance.
|
||||
//
|
||||
// NOTE: This method must be invoked with a non-nil `wg` if it is spawned as a
|
||||
// go routine--both `wg` and the server's WaitGroup should be incremented
|
||||
@ -968,7 +980,7 @@ func (s *server) sendToPeer(target *btcec.PublicKey,
|
||||
func (s *server) sendPeerMessages(
|
||||
targetPeer *peer,
|
||||
msgs []lnwire.Message,
|
||||
wg *sync.WaitGroup) {
|
||||
wg *sync.WaitGroup) []chan error {
|
||||
|
||||
// If a WaitGroup is provided, we assume that this method was spawned
|
||||
// as a go routine, and that it is being tracked by both the server's
|
||||
@ -981,9 +993,17 @@ func (s *server) sendPeerMessages(
|
||||
defer wg.Done()
|
||||
}
|
||||
|
||||
// We queue each message, creating a slice of error channels that
|
||||
// can be inspected after every message is successfully added to
|
||||
// the queue.
|
||||
var errChans []chan error
|
||||
for _, msg := range msgs {
|
||||
targetPeer.queueMsg(msg, nil)
|
||||
errChan := make(chan error, 1)
|
||||
targetPeer.queueMsg(msg, errChan)
|
||||
errChans = append(errChans, errChan)
|
||||
}
|
||||
|
||||
return errChans
|
||||
}
|
||||
|
||||
// FindPeer will return the peer that corresponds to the passed in public key.
|
||||
|
Loading…
Reference in New Issue
Block a user