Skip to content

Commit 0fdb433

Browse files
committed
chore: thread context through to SendCustomMessage
The only way to unblock SendCustomMessage is if the peer activates, disconnects or the server shuts down. This means that if the context is cancelled, we will still wait until one of those other events happen. With this commit we thread the context through to SendCustomMessage, so that if the context is cancelled, we can return early. This improves the cancellation semantics.
1 parent 98a3594 commit 0fdb433

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

rpcserver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9242,7 +9242,7 @@ func (r *rpcServer) RegisterRPCMiddleware(
92429242
}
92439243

92449244
// SendCustomMessage sends a custom peer message.
9245-
func (r *rpcServer) SendCustomMessage(_ context.Context,
9245+
func (r *rpcServer) SendCustomMessage(ctx context.Context,
92469246
req *lnrpc.SendCustomMessageRequest) (*lnrpc.SendCustomMessageResponse,
92479247
error) {
92489248

@@ -9252,7 +9252,7 @@ func (r *rpcServer) SendCustomMessage(_ context.Context,
92529252
}
92539253

92549254
err = r.server.SendCustomMessage(
9255-
peer, lnwire.MessageType(req.Type), req.Data,
9255+
ctx, peer, lnwire.MessageType(req.Type), req.Data,
92569256
)
92579257
switch {
92589258
case errors.Is(err, ErrPeerNotConnected):

server.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5289,21 +5289,24 @@ func (s *server) applyChannelUpdate(update *lnwire.ChannelUpdate1,
52895289

52905290
// SendCustomMessage sends a custom message to the peer with the specified
52915291
// pubkey.
5292-
func (s *server) SendCustomMessage(peerPub [33]byte, msgType lnwire.MessageType,
5293-
data []byte) error {
5292+
func (s *server) SendCustomMessage(ctx context.Context, peerPub [33]byte,
5293+
msgType lnwire.MessageType, data []byte) error {
52945294

52955295
peer, err := s.FindPeerByPubStr(string(peerPub[:]))
52965296
if err != nil {
52975297
return err
52985298
}
52995299

5300-
// We'll wait until the peer is active.
5300+
// We'll wait until the peer is active, but also listen for
5301+
// cancellation.
53015302
select {
53025303
case <-peer.ActiveSignal():
53035304
case <-peer.QuitSignal():
53045305
return fmt.Errorf("peer %x disconnected", peerPub)
53055306
case <-s.quit:
53065307
return ErrServerShuttingDown
5308+
case <-ctx.Done():
5309+
return ctx.Err()
53075310
}
53085311

53095312
msg, err := lnwire.NewCustom(msgType, data)

0 commit comments

Comments
 (0)