peer: extend SendMessage to allow callers to block until msg is sent

This commit is contained in:
Olaoluwa Osuntokun 2018-04-04 17:43:51 -07:00
parent b3bc374ba1
commit ca9174e166
No known key found for this signature in database
GPG Key ID: 964EA263DD637C21
2 changed files with 20 additions and 8 deletions

@ -111,10 +111,9 @@ type ChannelLink interface {
// Peer is an interface which represents the remote lightning node inside our // Peer is an interface which represents the remote lightning node inside our
// system. // system.
type Peer interface { type Peer interface {
// SendMessage sends message to remote peer. The second arguments // SendMessage sends message to remote peer. The second argument
// denote if the method should block until the message has been sent to // denotes if the method should block until the message has been sent
// the remote peer. If set, this allows the caller to more strongly // to the remote peer.
// synchronize.
SendMessage(msg lnwire.Message, sync bool) error SendMessage(msg lnwire.Message, sync bool) error
// WipeChannel removes the channel uniquely identified by its channel // WipeChannel removes the channel uniquely identified by its channel

21
peer.go

@ -1874,10 +1874,23 @@ func (p *peer) sendInitMsg() error {
return p.writeMessage(msg) return p.writeMessage(msg)
} }
// SendMessage queues a message for sending to the target peer. // SendMessage sends message to remote peer. The second argument denotes if the
func (p *peer) SendMessage(msg lnwire.Message) error { // method should block until the message has been sent to the remote peer.
p.queueMsg(msg, nil) func (p *peer) SendMessage(msg lnwire.Message, sync bool) error {
return nil if !sync {
p.queueMsg(msg, nil)
return nil
}
errChan := make(chan error, 1)
p.queueMsg(msg, errChan)
select {
case err := <-errChan:
return err
case <-p.quit:
return fmt.Errorf("peer shutting down")
}
} }
// PubKey returns the pubkey of the peer in compressed serialized format. // PubKey returns the pubkey of the peer in compressed serialized format.