Skip to content

Agent Mesh

node: dial TCP first

Antonio Ojea071bc4cada4c
diff --git a/internal/node/dial_ranker.go b/internal/node/dial_ranker.go
new file mode 100644
index 00000000..f03e7ed6
--- /dev/null
+++ b/internal/node/dial_ranker.go
@@ -0,0 +1,94 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package node
+
+import (
+ "time"
+
+ "github.com/libp2p/go-libp2p/core/network"
+ "github.com/libp2p/go-libp2p/p2p/net/swarm"
+ "github.com/multiformats/go-multiaddr"
+)
+
+// tcpHeadStart is how long the other transports wait when a peer offers
+// TCP: the head start libp2p's default ranker gives QUIC over TCP, reversed.
+// Once a TCP socket is connected the swarm holds further dials for twice
+// this while it upgrades, so the others are dialled only when TCP's SYN
+// goes unanswered.
+const tcpHeadStart = swarm.PublicTCPDelay
+
+// tcpFirstDialRanker orders a peer's addresses so the node ends up on a TCP
+// connection when the peer offers one. libp2p's default dials QUIC first,
+// and a QUIC connection admits 256 streams from the far end; the node's
+// connection to a router is a hub link that carries one stream per peer
+// relayed over it, so a service called through a router over QUIC had 256
+// callers at most through it, and a caller reaching many peers through one
+// router had the same wall. yamux over TCP has no per-connection stream
+// limit; the resource manager's budgets are the only ones. The transport
+// makes no difference to a connection that carries one session, so the
+// rule is applied to every dial rather than to routers alone, which would
+// have to be enforced at each place the node learns a router's address.
+// Relay addresses wait swarm.RelayDelay after direct ones, as in the
+// default. Hole punching does not use the ranker: a simultaneous-connect
+// dial tries every address at once.
+func tcpFirstDialRanker(addrs []multiaddr.Multiaddr) []network.AddrDelay {
+ var direct, relay []multiaddr.Multiaddr
+ for _, a := range addrs {
+ if hasCircuit(a) {
+ relay = append(relay, a)
+ } else {
+ direct = append(direct, a)
+ }
+ }
+ var relayOffset time.Duration
+ if len(direct) > 0 {
+ relayOffset = swarm.RelayDelay
+ }
+ res := make([]network.AddrDelay, 0, len(addrs))
+ res = append(res, rankTCPFirst(direct, 0)...)
+ return append(res, rankTCPFirst(relay, relayOffset)...)
+}
+
+// rankTCPFirst gives the TCP addresses of one group delay offset and the
+// rest offset plus tcpHeadStart, or offset alone when there is no TCP to
+// wait for.
+func rankTCPFirst(addrs []multiaddr.Multiaddr, offset time.Duration) []network.AddrDelay {
+ var otherDelay time.Duration
+ for _, a := range addrs {
+ if isTCPAddr(a) {
+ otherDelay = tcpHeadStart
+ break
+ }
+ }
+ res := make([]network.AddrDelay, 0, len(addrs))
+ for _, a := range addrs {
+ if isTCPAddr(a) {
+ res = append(res, network.AddrDelay{Addr: a, Delay: offset})
+ }
+ }
+ for _, a := range addrs {
+ if !isTCPAddr(a) {
+ res = append(res, network.AddrDelay{Addr: a, Delay: offset + otherDelay})
+ }
+ }
+ return res
+}
+
+// isTCPAddr is true for an address dialled over TCP: plain, WebSocket or
+// through a relay reached over TCP.
+func isTCPAddr(a multiaddr.Multiaddr) bool {
+ _, err := a.ValueForProtocol(multiaddr.P_TCP)
+ return err == nil
+}
diff --git a/internal/node/dial_ranker_test.go b/internal/node/dial_ranker_test.go
new file mode 100644
index 00000000..9aef5106
--- /dev/null
+++ b/internal/node/dial_ranker_test.go
@@ -0,0 +1,82 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package node
+
+import (
+ "testing"
+ "time"
+
+ "github.com/libp2p/go-libp2p/core/test"
+ "github.com/libp2p/go-libp2p/p2p/net/swarm"
+ "github.com/multiformats/go-multiaddr"
+)
+
+// A router offers TCP and QUIC on one port: TCP is dialled at once and
+// QUIC only after the head start, so the node holds its hub link over the
+// transport without a per-connection stream limit; a peer that offers no
+// TCP is dialled with no delay; a WebSocket address counts as TCP; relay
+// addresses wait for the direct ones, TCP relays first among them.
+func TestTCPFirstDialRanker(t *testing.T) {
+ relay := test.RandPeerIDFatal(t).String()
+ delays := func(addrs ...string) map[string]time.Duration {
+ mas := make([]multiaddr.Multiaddr, 0, len(addrs))
+ for _, a := range addrs {
+ mas = append(mas, multiaddr.StringCast(a))
+ }
+ out := map[string]time.Duration{}
+ for _, ad := range tcpFirstDialRanker(mas) {
+ out[ad.Addr.String()] = ad.Delay
+ }
+ if len(out) != len(addrs) {
+ t.Fatalf("ranked %d of %d addresses", len(out), len(addrs))
+ }
+ return out
+ }
+ want := func(got map[string]time.Duration, addr string, d time.Duration) {
+ t.Helper()
+ if got[addr] != d {
+ t.Errorf("%s dialled at %v, want %v", addr, got[addr], d)
+ }
+ }
+
+ both := delays("/ip4/203.0.113.1/udp/4501/quic-v1", "/ip4/203.0.113.1/tcp/4501", "/ip6/2001:db8::1/tcp/4501", "/ip4/203.0.113.1/udp/4501/quic-v1/webtransport")
+ want(both, "/ip4/203.0.113.1/tcp/4501", 0)
+ want(both, "/ip6/2001:db8::1/tcp/4501", 0)
+ want(both, "/ip4/203.0.113.1/udp/4501/quic-v1", tcpHeadStart)
+ want(both, "/ip4/203.0.113.1/udp/4501/quic-v1/webtransport", tcpHeadStart)
+
+ quicOnly := delays("/ip4/203.0.113.1/udp/4501/quic-v1", "/ip6/2001:db8::1/udp/4501/quic-v1")
+ want(quicOnly, "/ip4/203.0.113.1/udp/4501/quic-v1", 0)
+ want(quicOnly, "/ip6/2001:db8::1/udp/4501/quic-v1", 0)
+
+ ws := delays("/dns4/router.example/tcp/443/wss", "/ip4/203.0.113.1/udp/4501/quic-v1")
+ want(ws, "/dns4/router.example/tcp/443/wss", 0)
+ want(ws, "/ip4/203.0.113.1/udp/4501/quic-v1", tcpHeadStart)
+
+ relayed := delays(
+ "/ip4/198.51.100.7/tcp/5002",
+ "/ip4/203.0.113.1/tcp/4501/p2p/"+relay+"/p2p-circuit",
+ "/ip4/203.0.113.1/udp/4501/quic-v1/p2p/"+relay+"/p2p-circuit",
+ "/p2p/"+relay+"/p2p-circuit",
+ )
+ want(relayed, "/ip4/198.51.100.7/tcp/5002", 0)
+ want(relayed, "/ip4/203.0.113.1/tcp/4501/p2p/"+relay+"/p2p-circuit", swarm.RelayDelay)
+ want(relayed, "/ip4/203.0.113.1/udp/4501/quic-v1/p2p/"+relay+"/p2p-circuit", swarm.RelayDelay+tcpHeadStart)
+ want(relayed, "/p2p/"+relay+"/p2p-circuit", swarm.RelayDelay+tcpHeadStart)
+
+ // Nothing direct to wait for: the relays are dialled at once.
+ relayOnly := delays("/p2p/" + relay + "/p2p-circuit")
+ want(relayOnly, "/p2p/"+relay+"/p2p-circuit", 0)
+}
diff --git a/internal/node/node.go b/internal/node/node.go
index 946f01cd..93a99902 100644
--- a/internal/node/node.go
+++ b/internal/node/node.go
@@ -576,7 +576,7 @@ func (n *AgentMeshNode) Start(ctx context.Context) error {
libp2p.EnableHolePunching(),
libp2p.ConnectionManager(cm),
libp2p.ResourceManager(rm),
- libp2p.SwarmOpts(swarm.WithDialTimeout(15 * time.Second)),
+ libp2p.SwarmOpts(swarm.WithDialTimeout(15*time.Second), swarm.WithDialRanker(tcpFirstDialRanker)),
libp2p.AddrsFactory(n.announceFilter),
}
// Private: the node holds a relay reservation whatever AutoNAT finds, so