Skip to content

Add WithMessagePriority router option - #741

Open
domiwei wants to merge 1 commit into
libp2p:masterfrom
domiwei:kewei/message-priority
Open

domiwei wants to merge 1 commit into
libp2p:masterfrom
domiwei:kewei/message-priority

Conversation

@domiwei

@domiwei domiwei commented Nov 18, 2026

Copy link
Copy Markdown

This adds a GossipSub router option that lets an application send some messages ahead of the normal traffic already queued for each peer. It addresses #210 ("Allow prioritization of traffic per topic") with a callback, so the application decides which messages are prioritised, by topic or by anything else in the message.

pubsub.WithMessagePriority(func(m *pubsub.Message) bool {
	return !strings.Contains(m.GetTopic(), "data_column_sidecar")
})

Motivation

Ethereum consensus clients publish bursts of large messages: when a block arrives, a node can publish up to 128 data column sidecars to every mesh peer at once. Small, time-critical messages published right after the burst (attestations, payload attestations) wait in the same per-peer FIFO queue behind several megabytes of columns.

We measured this in Erigon's consensus client (Caplin) on an 8-node local devnet (Kurtosis, 40 ms one-way delay per link, every node responsible for all 128 data columns), comparing two Caplin nodes side by side over 240 slots. With a local build of an earlier version of this option (applied to v0.17.0; it also sent the piggybacked control urgently), and Caplin prioritising every topic except data columns and blobs, the median arrival of our payload attestation messages at the other nodes improved by 144 ms (paired per-slot difference, 95% interval [-176, -112] ms). A simpler rule that marked every message below the IDONTWANT size threshold as urgent gave 160 ms. For comparison, each from its own paired run:

  • cancelling queued RPCs on IDONTWANT (a port of cancel enqueued rpcs on receiving IDontWant #648 to v0.17.0) improved it by 8 ms;
  • publishing the columns in batches 300 ms apart improved it by 24 ms;
  • switching the transport from QUIC to TCP improved it by 14 ms, while the time from block publication to the new head rose by 27 ms and the time from the validator client's envelope POST to a FULL head rose by 33 ms in that run.

Other libp2p implementations already order outgoing traffic: nim-libp2p sends locally published messages before relayed ones, and rust-libp2p uses separate queues for subscription RPCs, control RPCs, and the remaining traffic (publish, forward, IHAVE, IWANT). This option keeps the policy in the application and leaves the default behaviour unchanged.

Changes

  • WithMessagePriority(func(*Message) bool) Option, a gossipsub router option like WithFloodPublish.
  • GossipSubRouter.Publish passes the callback's result as the existing urgent flag of sendRPC, so a prioritised message goes through UrgentPush and is queued ahead of the normal traffic for each peer. Publish handles both locally originated and forwarded messages.
  • PublishBatch is unchanged and keeps the normal queue. Batch publishing is meant for bulk data, and extending priority through RPCScheduler would require stable RPC or message identity, which the interface does not promise.
  • For a prioritised publish, sendRPC queues the message before the control and gossip it piggybacks (for example a pending GRAFT or IHAVE), and keeps that control in the normal queue. Both lanes share the queue capacity, so the prioritised message is not the one dropped when a single slot is left; a dropped control RPC goes through the existing drop path. Control-only urgent RPCs, such as IDONTWANT, and all non-urgent sends keep their current order and lanes.

Behaviour to note:

  • With the option unset, nothing changes.
  • Priority changes the order, not admission: prioritised messages still count towards the outbound queue size and are dropped like any other message when the queue is full.
  • Prioritised messages share the priority lane with IDONTWANT, so the callback should select small, time-critical messages; prioritising large or frequent messages delays IDONTWANT and other traffic.
  • The callback runs on the router's event loop, so it must be fast and must not block.
  • IWANT responses keep the normal queue.

Tests

gossipsub_priority_test.go checks the queue order directly, without timing, in a synctest bubble:

  • GossipSubRouter.Publish, called on the event loop with ReceivedFrom set to the local host or to another peer, queues a prioritised message ahead of an earlier normal one (the tests call the router directly; they do not go through inbound validation);
  • PublishBatch keeps a selected message in the normal queue;
  • pending control stays in the normal queue for a prioritised publish, while the IDONTWANT path still sends it urgently;
  • with one free slot and a pending GRAFT, the prioritised publish takes the slot;
  • without the option, the order is FIFO and the priority lane stays empty;
  • a full queue still drops a prioritised message;
  • the option returns an error on a non-gossipsub router.

go test ./..., go vet ./... and gofmt pass. Staticcheck reports only the pre-existing SA4006 finding.

WithMessagePriority lets an application mark messages sent through
Publish, locally originated or forwarded, to be queued ahead of the
normal traffic already waiting for each peer, through the existing
urgent path of the per-peer RPC queue. A prioritised publish is queued
before the control and gossip it piggybacks, which stay in the normal
queue. PublishBatch and all other sends are unchanged, and with the
option unset nothing changes.
joxerx pushed a commit to joxerx/erigon that referenced this pull request Nov 20, 2026
## Summary

Caplin publishes up to 128 data column sidecars per block, each to the
peers of its column subnet. Small, time-critical messages published or
forwarded right after them (attestations, payload attestations, blocks,
execution payload envelopes) wait behind several megabytes of columns in
go-libp2p-pubsub's per-peer outbound queue.

This PR uses a new GossipSub router option, `WithMessagePriority`, to
put every message Caplin publishes or forwards, except data column and
blob sidecars, in the queue's existing priority lane.

- `cl/p2p/libp2p_setting.go`: `gossipMessageHasPriority` returns false
for `data_column_sidecar_*` and `blob_sidecar_*` topics and true for
every other topic. `pubsubOptions` passes it to
`pubsub.WithMessagePriority`.
- `go.mod`: the option is not released upstream yet
(libp2p/go-libp2p-pubsub#741), so `github.com/libp2p/go-libp2p-pubsub`
is replaced by `github.com/domiwei/go-libp2p-pubsub
v0.0.0-20261119062000-9d96f54458a3`. That commit is upstream v0.17.0
plus a cherry-pick of the commit from that PR, nothing else. Its go.mod
is the same as in v0.17.0 and the require stays at v0.17.0, so no other
module version changes; go-libp2p is not bumped. Once upstream releases
the option, the require can move to that release and the replace can go.
- `.golangci.yml`: allows the new replace in `gomoddirectives`.

Behaviour to note:
- Priority changes the order, not the queue limit: prioritised messages
still count towards the outbound queue limit (600 RPCs per peer), do not
evict queued messages, and are dropped like any other message when the
queue is full.
- Messages in the priority lane keep FIFO order among themselves, so a
large prioritised message (a block or an envelope) can still delay
smaller ones queued after it. IDONTWANT already used this lane. Before
this change it went out ahead of every queued gossip message; now it
also waits behind prioritised messages queued before it.
- `PublishBatch`, IWANT responses and heartbeat control traffic keep the
queue they used before, as defined by the upstream option. The normal
queue is sent only when the priority lane is empty, so on a link that
cannot keep up with the prioritised traffic, sidecars, IWANT responses
and heartbeat control traffic wait longer than before.

## Measurement

E3 local devnet: Kurtosis, 8 beacon nodes (Lighthouse, Teku, Prysm,
Nimbus, Lodestar, Grandine and two embedded Caplin nodes), 40 ms one-way
delay per link, every node responsible for all 128 data columns. The two
Caplin nodes ran side by side for 240 slots: A = main d273dec, B =
the same build with an equivalent version of this change (the fork code
vendored locally). Both had a local timing-log overlay.

B − A, median of the paired per-slot differences with a 95% interval;
the own-block rows compare different slots (unpaired, difference of the
medians):

| | A | B | B − A |
|---|---|---|---|
| payload attestation arrival at the other nodes, from slot start, per
message | 1143 ms | 917 ms | −207 ms [−236, −151] |
| payload attestation POST → arrival, per message | 415 ms | 201 ms |
−185 ms [−231, −148] |
| own block: VC block publication → first envelope at a calibrated
reference node (unpaired, n 25 / 26) | 321 ms | 110 ms | −211 ms [−257,
−184] |
| own block: VC block publication → 128th column at Lighthouse
(unpaired, n 23 / 24) | 714 ms | 713 ms | −1 ms [−45, +24] |

Block → head, envelope → FULL head and attestation_data latency moved by
less than 1 ms. CPU moved by −0.004 cores and peak memory by +0.02 GiB
(medians of the paired 12 s samples).

## Tests

- `TestGossipMessageHasPriority` checks the rule on full topic strings
for the sidecar topics and for several other topic families.
- `TestPubsubOptionsSendAttestationAheadOfQueuedColumns` builds a
gossipsub router from `pubsubOptions` and connects it to a mocknet peer
that does not read yet. It publishes four 1 KiB messages on a data
column topic and then one on an attestation topic, and checks that the
attestation-topic message is not sent last. It fails when the
`WithMessagePriority` line is removed from `pubsubOptions`.
- The queue behaviour of the option is tested in the fork
(`gossipsub_priority_test.go`, the same tests as in
libp2p/go-libp2p-pubsub#741).
- `make lint`, `go test ./cl/p2p/...` and `go build ./cmd/erigon
./cmd/caplin` pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant