From 7577e59f6c2ea981a64e88fd4061214d4d9ccbd4 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 16 Feb 2026 11:31:21 +1030 Subject: [PATCH] connectd: refactor outgoing loop. Give us a single "next message" function to call. This will be useful when we want to write more than one at a time. Signed-off-by: Rusty Russell --- connectd/multiplex.c | 58 ++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/connectd/multiplex.c b/connectd/multiplex.c index a59b7ad33..4135ad5f3 100644 --- a/connectd/multiplex.c +++ b/connectd/multiplex.c @@ -1085,6 +1085,33 @@ static void maybe_update_channelid(struct subd *subd, const u8 *msg) } } +static const u8 *next_msg_for_peer(struct peer *peer) +{ + const u8 *msg; + + msg = msg_dequeue(peer->peer_outq); + if (!msg) { + /* Draining? Don't send gossip. */ + if (peer->draining_state == WRITING_TO_PEER) + return NULL; + + /* If they want us to send gossip, do so now. */ + msg = maybe_gossip_msg(NULL, peer); + if (!msg) + return NULL; + } + + /* dev_disconnect can disable writes (discard everything) */ + if (peer->dev_writes_enabled) { + if (*peer->dev_writes_enabled == 0) { + return tal_free(msg); + } + (*peer->dev_writes_enabled)--; + } + + return msg; +} + static struct io_plan *write_to_peer(struct io_conn *peer_conn, struct peer *peer) { @@ -1094,10 +1121,8 @@ static struct io_plan *write_to_peer(struct io_conn *peer_conn, /* Free last sent one (if any) */ peer->sent_to_peer = tal_free(peer->sent_to_peer); - /* Pop tail of send queue */ - msg = msg_dequeue(peer->peer_outq); - - /* Still nothing to send? */ + /* Pop tail of send queue (or gossip) */ + msg = next_msg_for_peer(peer); if (!msg) { /* Draining? Shutdown socket (to avoid losing msgs) */ if (peer->draining_state == WRITING_TO_PEER) { @@ -1106,33 +1131,18 @@ static struct io_plan *write_to_peer(struct io_conn *peer_conn, return io_sock_shutdown(peer_conn); } - /* If they want us to send gossip, do so now. */ - msg = maybe_gossip_msg(NULL, peer); - if (!msg) { - /* Tell them to read again, */ - io_wake(&peer->subds); - io_wake(&peer->peer_in); + /* Tell them to read again, */ + io_wake(&peer->subds); + io_wake(&peer->peer_in); - /* Wait for them to wake us */ - return msg_queue_wait(peer_conn, peer->peer_outq, - write_to_peer, peer); - } + /* Wait for them to wake us */ + return msg_queue_wait(peer_conn, peer->peer_outq, write_to_peer, peer); } if (peer->draining_state == WRITING_TO_PEER) status_peer_debug(&peer->id, "draining, but sending %s.", peer_wire_name(fromwire_peektype(msg))); - /* dev_disconnect can disable writes */ - if (peer->dev_writes_enabled) { - if (*peer->dev_writes_enabled == 0) { - tal_free(msg); - /* Continue, to drain queue */ - return write_to_peer(peer_conn, peer); - } - (*peer->dev_writes_enabled)--; - } - return encrypt_and_send(peer, take(msg)); }