peer: add error chan to queueMsg

This commit is contained in:
Johan T. Halseth 2017-11-15 18:23:46 -08:00 committed by Olaoluwa Osuntokun
parent c36b1c3992
commit 25b77a0aee
1 changed files with 13 additions and 8 deletions

21
peer.go
View File

@ -48,8 +48,8 @@ const (
// a buffered channel which will be sent upon once the write is complete. This
// buffered channel acts as a semaphore to be used for synchronization purposes.
type outgoinMsg struct {
msg lnwire.Message
sentChan chan struct{} // MUST be buffered.
msg lnwire.Message
errChan chan error // MUST be buffered.
}
// newChannelMsg packages a lnwallet.LightningChannel with a channel that
@ -1027,8 +1027,8 @@ out:
// callers to optionally synchronize sends with the
// writeHandler.
err := p.writeMessage(outMsg.msg)
if outMsg.sentChan != nil {
close(outMsg.sentChan)
if outMsg.errChan != nil {
outMsg.errChan <- err
}
if err != nil {
@ -1122,12 +1122,17 @@ func (p *peer) PingTime() int64 {
}
// queueMsg queues a new lnwire.Message to be eventually sent out on the
// wire.
func (p *peer) queueMsg(msg lnwire.Message, doneChan chan struct{}) {
// wire. It returns an error if we failed to queue the message. An error
// is sent on errChan if the message fails being sent to the peer, or
// nil otherwise.
func (p *peer) queueMsg(msg lnwire.Message, errChan chan error) {
select {
case p.outgoingQueue <- outgoinMsg{msg, doneChan}:
case p.outgoingQueue <- outgoinMsg{msg, errChan}:
case <-p.quit:
return
peerLog.Debugf("Peer shutting down, could not enqueue msg.")
if errChan != nil {
errChan <- fmt.Errorf("peer shutting down")
}
}
}