allmende-conns -relay: the relay holds 128 reservations of 200 asked, 67 MB; measured and documented
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
@@ -160,7 +160,11 @@ starting a design-heavy item; see "Working with the owner".
|
||||
`GOMEMLIMIT=400MiB` as a safety net. With a 269 MB database (50,000
|
||||
listings) the same node idled at 53 MB, held 137 MB with 1000 connections and
|
||||
peaked at 174 MB with 1000 connections plus 30 real nodes catching up.
|
||||
Not measured: relayed streams (need a node behind a NAT). The run found that
|
||||
As a relay (`allmende-conns -relay`): 128 reservations held of 200 asked
|
||||
(libp2p's default cap; 8 per address), 67 MB with 128 open circuits; a
|
||||
relayed connection ends after 2 min or 128 KB, so it only brings nodes into
|
||||
contact. With one public node at most 128 NAT'd nodes can have a relay at
|
||||
once; not tested with a real NAT or hole punching. The run found that
|
||||
a newcomer stored only ~137 listings/s with SQLite's `synchronous=FULL` and
|
||||
its 2-minute sync cut-off left 18% of a 20k region for the next round; the
|
||||
store now uses `synchronous=NORMAL`, stores a sync page in one transaction
|
||||
|
||||
@@ -32,6 +32,7 @@ func main() {
|
||||
region := flag.String("region", "u1x", "region whose listing topic every client subscribes to")
|
||||
hold := flag.Duration("hold", time.Minute, "how long to keep them open once connected")
|
||||
parallel := flag.Int("parallel", 25, "connections made at the same time")
|
||||
relay := flag.Bool("relay", false, "measure the node as a relay instead: -n clients take a reservation on it, then pairs talk through it")
|
||||
flag.Parse()
|
||||
if *nodeAddr == "" {
|
||||
fmt.Fprintln(os.Stderr, "usage: allmende-conns -node <multiaddr> [-n 1000] [-region u1x] [-hold 1m]")
|
||||
@@ -51,6 +52,11 @@ func main() {
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
|
||||
defer stop()
|
||||
|
||||
if *relay {
|
||||
runRelay(ctx, target, *n, *parallel, *hold)
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
hosts []host.Host
|
||||
mu sync.Mutex
|
||||
|
||||
@@ -0,0 +1,155 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/libp2p/go-libp2p"
|
||||
"github.com/libp2p/go-libp2p/core/host"
|
||||
"github.com/libp2p/go-libp2p/core/network"
|
||||
"github.com/libp2p/go-libp2p/core/peer"
|
||||
"github.com/libp2p/go-libp2p/p2p/host/autorelay"
|
||||
)
|
||||
|
||||
const pingProtocol = "/allmende-conns/ping/1"
|
||||
|
||||
// runRelay measures what a public node pays as a relay: n clients that treat
|
||||
// themselves as unreachable each take a reservation on the node, then pairs of
|
||||
// them talk to each other through it. Each client listens on its own loopback
|
||||
// address (127.0.0.2, 127.0.0.3, …), because the relay limits reservations per
|
||||
// source address and one address would stop it after 8.
|
||||
func runRelay(ctx context.Context, target *peer.AddrInfo, n int, parallel int, hold time.Duration) {
|
||||
var (
|
||||
mu sync.Mutex
|
||||
hosts []host.Host
|
||||
reserved atomic.Int64
|
||||
failed atomic.Int64
|
||||
wg sync.WaitGroup
|
||||
sem = make(chan struct{}, parallel)
|
||||
)
|
||||
start := time.Now()
|
||||
for i := 0; i < n && ctx.Err() == nil; i++ {
|
||||
sem <- struct{}{}
|
||||
wg.Add(1)
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
defer func() { <-sem }()
|
||||
ip := fmt.Sprintf("127.0.%d.%d", 1+i/250, 2+i%250)
|
||||
h, err := libp2p.New(
|
||||
libp2p.ListenAddrStrings("/ip4/"+ip+"/tcp/0"),
|
||||
libp2p.ForceReachabilityPrivate(),
|
||||
libp2p.EnableAutoRelayWithStaticRelays([]peer.AddrInfo{*target},
|
||||
autorelay.WithBootDelay(0), autorelay.WithMinCandidates(1), autorelay.WithNumRelays(1)),
|
||||
libp2p.ResourceManager(&network.NullResourceManager{}),
|
||||
)
|
||||
if err != nil {
|
||||
failed.Add(1)
|
||||
return
|
||||
}
|
||||
h.SetStreamHandler(pingProtocol, func(s network.Stream) {
|
||||
defer s.Close()
|
||||
buf := make([]byte, 4)
|
||||
if _, err := io.ReadFull(s, buf); err == nil {
|
||||
s.Write(buf)
|
||||
}
|
||||
})
|
||||
cctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
if err := h.Connect(cctx, *target); err != nil {
|
||||
failed.Add(1)
|
||||
h.Close()
|
||||
return
|
||||
}
|
||||
mu.Lock()
|
||||
hosts = append(hosts, h)
|
||||
mu.Unlock()
|
||||
reserved.Add(1)
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
fmt.Printf("%d clients connected to the relay, %d could not, in %s\n", reserved.Load(), failed.Load(), time.Since(start).Round(time.Millisecond))
|
||||
time.Sleep(3 * time.Second) // let the reservations settle
|
||||
|
||||
// libp2p does not advertise a relay address built on a loopback IP, so a
|
||||
// reservation cannot be seen from the client. A prober that dials each
|
||||
// client through the relay and exchanges a few bytes shows which ones the
|
||||
// relay really holds a reservation for.
|
||||
// Each client gets a prober of its own (the relay allows 16 open circuits
|
||||
// per peer), and the circuits stay open until the end, as they would for
|
||||
// people who keep talking.
|
||||
var ok, bad atomic.Int64
|
||||
var pw sync.WaitGroup
|
||||
var probers []host.Host
|
||||
var pmu sync.Mutex
|
||||
psem := make(chan struct{}, 16)
|
||||
for _, b := range hosts {
|
||||
psem <- struct{}{}
|
||||
pw.Add(1)
|
||||
go func() {
|
||||
defer pw.Done()
|
||||
defer func() { <-psem }()
|
||||
prober, err := libp2p.New(libp2p.NoListenAddrs, libp2p.EnableRelay(), libp2p.ResourceManager(&network.NullResourceManager{}))
|
||||
if err != nil {
|
||||
bad.Add(1)
|
||||
return
|
||||
}
|
||||
pmu.Lock()
|
||||
probers = append(probers, prober)
|
||||
pmu.Unlock()
|
||||
if err := pingThrough(ctx, prober, b, target); err != nil {
|
||||
if bad.Add(1) <= 3 {
|
||||
fmt.Fprintln(os.Stderr, "e.g.:", err)
|
||||
}
|
||||
} else {
|
||||
ok.Add(1)
|
||||
}
|
||||
}()
|
||||
}
|
||||
pw.Wait()
|
||||
fmt.Printf("%d of %d clients could be reached through the relay (a reservation held), %d could not\n",
|
||||
ok.Load(), ok.Load()+bad.Load(), bad.Load())
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case <-time.After(hold):
|
||||
}
|
||||
for _, h := range append(hosts, probers...) {
|
||||
h.Close()
|
||||
}
|
||||
}
|
||||
|
||||
// pingThrough has a dial b by way of the relay and send four bytes.
|
||||
func pingThrough(ctx context.Context, a, b host.Host, relay *peer.AddrInfo) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, 20*time.Second)
|
||||
defer cancel()
|
||||
ctx = network.WithAllowLimitedConn(ctx, "relay measurement")
|
||||
via := relay.Addrs[0].String() + "/p2p/" + relay.ID.String() + "/p2p-circuit"
|
||||
ai, err := peer.AddrInfoFromString(via + "/p2p/" + b.ID().String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := a.Connect(ctx, *ai); err != nil {
|
||||
return fmt.Errorf("dial through the relay: %w", err)
|
||||
}
|
||||
s, err := a.NewStream(ctx, b.ID(), pingProtocol)
|
||||
if err != nil {
|
||||
return fmt.Errorf("stream: %w", err)
|
||||
}
|
||||
defer s.Close()
|
||||
if _, err := s.Write([]byte("ping")); err != nil {
|
||||
return err
|
||||
}
|
||||
buf := make([]byte, 4)
|
||||
if _, err := io.ReadFull(s, buf); err != nil {
|
||||
return err
|
||||
}
|
||||
if string(buf) != "ping" {
|
||||
return fmt.Errorf("answer %q", buf)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
+17
-2
@@ -749,8 +749,23 @@ Findings (see "What is left" in SPECS.md for the open ones):
|
||||
peaked at 174 MB, a third of the pod limit. About a minute after the
|
||||
connections were opened the node's connection manager closed most of the
|
||||
idle ones (970 connected, 154 left after 2.5 minutes) once real nodes had
|
||||
joined, which is its job at the high-water mark. Relayed streams were not
|
||||
tried (they need a node behind a NAT).
|
||||
joined, which is its job at the high-water mark.
|
||||
|
||||
**As a relay** (`allmende-conns -relay`, 200 clients that count themselves
|
||||
unreachable, each on its own loopback address, take a reservation on one
|
||||
`--public` node; a prober then dials each through the relay and exchanges
|
||||
four bytes, all circuits kept open): the node held **128** reservations,
|
||||
exactly libp2p's default `MaxReservations`, and refused the other 72
|
||||
(`NO_RESERVATION`). Memory went from 39 MB idle to 67 MB with the 200
|
||||
connections and 128 open circuits (about 140 KB each), so the relay is
|
||||
bounded by that cap, not by memory. Other defaults that matter: 8
|
||||
reservations per source address, 16 open circuits per peer, and a relayed
|
||||
connection is cut after 2 minutes or 128 KB each way, so a relay is for
|
||||
getting in touch and for hole punching, not for carrying listings. Nodes
|
||||
behind a NAT still gossip and sync over their own outbound connections;
|
||||
what the 129th loses is being reachable from other NAT'd nodes through
|
||||
this relay. With one public node, at most 128 NAT'd nodes at a time can
|
||||
have a relay. (A real NAT, with hole punching, was not tested.)
|
||||
|
||||
The run also found a slow spot, not in the serving node but in the
|
||||
newcomer: a catch-up is cut off after 2 minutes (`syncTimeout`), and with
|
||||
|
||||
Reference in New Issue
Block a user