From f39edd8000d8a0353c87d0e325ca2939f559d093 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Tue, 5 Mar 2019 17:08:22 -0800 Subject: [PATCH] peer: add SendMessageLazy --- discovery/mock_test.go | 5 +++++ fundingmanager_test.go | 4 ++++ htlcswitch/link_test.go | 3 +++ htlcswitch/mock.go | 4 ++++ lnpeer/peer.go | 15 +++++++++++---- peer.go | 31 +++++++++++++++++++++++++++---- 6 files changed, 54 insertions(+), 8 deletions(-) diff --git a/discovery/mock_test.go b/discovery/mock_test.go index 85c6c4f3..9e945193 100644 --- a/discovery/mock_test.go +++ b/discovery/mock_test.go @@ -35,6 +35,11 @@ func (p *mockPeer) SendMessage(_ bool, msgs ...lnwire.Message) error { return nil } + +func (p *mockPeer) SendMessageLazy(sync bool, msgs ...lnwire.Message) error { + return p.SendMessage(sync, msgs...) +} + func (p *mockPeer) AddNewChannel(_ *channeldb.OpenChannel, _ <-chan struct{}) error { return nil } diff --git a/fundingmanager_test.go b/fundingmanager_test.go index 22d0d710..debd16eb 100644 --- a/fundingmanager_test.go +++ b/fundingmanager_test.go @@ -170,6 +170,10 @@ func (n *testNode) SendMessage(_ bool, msg ...lnwire.Message) error { return n.sendMessage(msg[0]) } +func (n *testNode) SendMessageLazy(sync bool, msgs ...lnwire.Message) error { + return n.SendMessage(sync, msgs...) +} + func (n *testNode) WipeChannel(_ *wire.OutPoint) error { return nil } diff --git a/htlcswitch/link_test.go b/htlcswitch/link_test.go index 6c5f10ba..8b466948 100644 --- a/htlcswitch/link_test.go +++ b/htlcswitch/link_test.go @@ -1549,6 +1549,9 @@ func (m *mockPeer) SendMessage(sync bool, msgs ...lnwire.Message) error { } return nil } +func (m *mockPeer) SendMessageLazy(sync bool, msgs ...lnwire.Message) error { + return m.SendMessage(sync, msgs...) +} func (m *mockPeer) AddNewChannel(_ *channeldb.OpenChannel, _ <-chan struct{}) error { return nil diff --git a/htlcswitch/mock.go b/htlcswitch/mock.go index 5e899415..cd6a5dcf 100644 --- a/htlcswitch/mock.go +++ b/htlcswitch/mock.go @@ -502,6 +502,10 @@ func (s *mockServer) SendMessage(sync bool, msgs ...lnwire.Message) error { return nil } +func (s *mockServer) SendMessageLazy(sync bool, msgs ...lnwire.Message) error { + panic("not implemented") +} + func (s *mockServer) readHandler(message lnwire.Message) error { var targetChan lnwire.ChannelID diff --git a/lnpeer/peer.go b/lnpeer/peer.go index 34675aa5..16cbff63 100644 --- a/lnpeer/peer.go +++ b/lnpeer/peer.go @@ -12,10 +12,17 @@ import ( // Peer is an interface which represents the remote lightning node inside our // system. type Peer interface { - // SendMessage sends a variadic number of message to remote peer. The - // first argument denotes if the method should block until the message - // has been sent to the remote peer. - SendMessage(sync bool, msg ...lnwire.Message) error + // SendMessage sends a variadic number of high-priority message to + // remote peer. The first argument denotes if the method should block + // until the messages have been sent to the remote peer or an error is + // returned, otherwise it returns immediately after queuing. + SendMessage(sync bool, msgs ...lnwire.Message) error + + // SendMessageLazy sends a variadic number of low-priority message to + // remote peer. The first argument denotes if the method should block + // until the messages have been sent to the remote peer or an error is + // returned, otherwise it returns immediately after queueing. + SendMessageLazy(sync bool, msgs ...lnwire.Message) error // AddNewChannel adds a new channel to the peer. The channel should fail // to be added if the cancel channel is closed. diff --git a/peer.go b/peer.go index 89c21601..f4e97c35 100644 --- a/peer.go +++ b/peer.go @@ -2351,12 +2351,31 @@ func (p *peer) resendChanSyncMsg(cid lnwire.ChannelID) error { return nil } -// SendMessage sends a variadic number of message to remote peer. The first -// argument denotes if the method should block until the message has been sent -// to the remote peer. +// SendMessage sends a variadic number of high-priority message to remote peer. +// The first argument denotes if the method should block until the messages have +// been sent to the remote peer or an error is returned, otherwise it returns +// immediately after queuing. // // NOTE: Part of the lnpeer.Peer interface. func (p *peer) SendMessage(sync bool, msgs ...lnwire.Message) error { + return p.sendMessage(sync, true, msgs...) +} + +// SendMessageLazy sends a variadic number of low-priority message to remote +// peer. The first argument denotes if the method should block until the +// messages have been sent to the remote peer or an error is returned, otherwise +// it returns immediately after queueing. +// +// NOTE: Part of the lnpeer.Peer interface. +func (p *peer) SendMessageLazy(sync bool, msgs ...lnwire.Message) error { + return p.sendMessage(sync, false, msgs...) +} + +// sendMessage queues a variadic number of messages using the passed priority +// to the remote peer. If sync is true, this method will block until the +// messages have been sent to the remote peer or an error is returned, otherwise +// it returns immediately after queueing. +func (p *peer) sendMessage(sync, priority bool, msgs ...lnwire.Message) error { // Add all incoming messages to the outgoing queue. A list of error // chans is populated for each message if the caller requested a sync // send. @@ -2370,7 +2389,11 @@ func (p *peer) SendMessage(sync bool, msgs ...lnwire.Message) error { errChans = append(errChans, errChan) } - p.queueMsg(msg, errChan) + if priority { + p.queueMsg(msg, errChan) + } else { + p.queueMsgLazy(msg, errChan) + } } // Wait for all replies from the writeHandler. For async sends, this