From 6e37a2c654b1395eca0caf49dfeee4b2862642fa Mon Sep 17 00:00:00 2001 From: Nils Stinnesbeck Date: Thu, 19 Nov 2026 00:58:45 +0100 Subject: [PATCH] memory of a public node with 1000 connections: measured, GOMEMLIMIT in the manifest cmd/allmende-conns opens many small libp2p clients to one node. A real --public node: 35 MB idle, 131 MB with 1000 connections, 156 MB at the peak with 100 listings flooded to all of them; the limit refused the 1001st. The StatefulSet sets GOMEMLIMIT=400MiB under its 512Mi limit as a safety net. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw --- SPECS.md | 10 ++-- cmd/allmende-conns/main.go | 117 +++++++++++++++++++++++++++++++++++++ docs/PROTOCOL.md | 12 +++- k8s/base/statefulset.yaml | 5 ++ 4 files changed, 138 insertions(+), 6 deletions(-) create mode 100644 cmd/allmende-conns/main.go diff --git a/SPECS.md b/SPECS.md index 94a11fb..5457166 100644 --- a/SPECS.md +++ b/SPECS.md @@ -150,9 +150,11 @@ starting a design-heavy item; see "Working with the owner". access exists. Listings already have an `images` field of hashes. - **Recovering listings onto an empty database** (after losing the data directory but not the phrase). -- **Check a public node's memory under load.** `--public` nodes now accept - 1000 inbound connections (`--max-conns`); nobody has measured a node holding - that many against the 512 MiB pod limit. +- **Public node memory** was measured (`cmd/allmende-conns`): 1000 inbound + connections cost about 130 MB, 156 MB at the peak with 100 listings flooded + to all of them, against the 512 MiB pod limit; the manifest sets + `GOMEMLIMIT=400MiB` as a safety net. Not measured: real peers (DHT queries, + relayed streams) and a node with a large database. - **What the abuse limits do not cover** (`internal/p2p/guard.go`, `ipgate.go`, `internal/api/ratelimit.go`): attackers with many real addresses, valid but unwanted records under the limits, and bans are per node and not shared @@ -244,7 +246,7 @@ make image # build and push the container image (clean tree only) | `internal/inbox` | the owner's conversations, replies, block | | `internal/api` | HTTP API (public reads, relay, admin); `ratelimit.go` (per-client limit, trusted proxies) | | `internal/places`, `internal/jcs`, `internal/pow` | place names, canonical JSON, hashcash (`pow.Mine`, the fast miner) | -| `cmd/allmende-sim`, `cmd/allmende-load` | network simulator; load and mining-time tool | +| `cmd/allmende-sim`, `cmd/allmende-load`, `cmd/allmende-conns` | network simulator; load and mining-time tool; holds many connections to one node to measure its memory | | `web/` | TypeScript UI (embedded), tests, build; `web/static/places.tsv` is generated | | `k8s/`, `nix/`, `Dockerfile` | deployment | | `scripts/e2e.sh`, `scripts/e2e/` | end-to-end with real binaries and jsdom | diff --git a/cmd/allmende-conns/main.go b/cmd/allmende-conns/main.go new file mode 100644 index 0000000..b1f9087 --- /dev/null +++ b/cmd/allmende-conns/main.go @@ -0,0 +1,117 @@ +// Command allmende-conns holds many connections to one node, to measure what +// that costs it (memory above all; a --public node accepts 1000 inbound). +// +// Every client is a small libp2p host that connects over TCP and subscribes to +// the listing topic of a region, like a real peer would. It does nothing else. +// Watch the node's memory from outside (/proc//status: VmRSS, VmHWM). +// +// allmende-conns -node /ip4/127.0.0.1/tcp/4001/p2p/12D3Koo... -n 1000 -region u1x -hold 2m +package main + +import ( + "context" + "flag" + "fmt" + "os" + "os/signal" + "sync" + "sync/atomic" + "time" + + "github.com/libp2p/go-libp2p" + pubsub "github.com/libp2p/go-libp2p-pubsub" + "github.com/libp2p/go-libp2p/core/host" + "github.com/libp2p/go-libp2p/core/network" + "github.com/libp2p/go-libp2p/core/peer" + ma "github.com/multiformats/go-multiaddr" +) + +func main() { + nodeAddr := flag.String("node", "", "multiaddr of the node, with its /p2p/ peer ID") + n := flag.Int("n", 1000, "connections to open") + 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") + flag.Parse() + if *nodeAddr == "" { + fmt.Fprintln(os.Stderr, "usage: allmende-conns -node [-n 1000] [-region u1x] [-hold 1m]") + os.Exit(2) + } + maddr, err := ma.NewMultiaddr(*nodeAddr) + if err != nil { + fmt.Fprintln(os.Stderr, "bad -node:", err) + os.Exit(2) + } + target, err := peer.AddrInfoFromP2pAddr(maddr) + if err != nil { + fmt.Fprintln(os.Stderr, "-node needs a /p2p/ part:", err) + os.Exit(2) + } + + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) + defer stop() + + var ( + hosts []host.Host + mu sync.Mutex + ok 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() { + defer wg.Done() + defer func() { <-sem }() + h, err := libp2p.New(libp2p.NoListenAddrs, libp2p.DisableRelay(), + libp2p.ResourceManager(&network.NullResourceManager{})) + if err != nil { + failed.Add(1) + return + } + cctx, cancel := context.WithTimeout(ctx, 30*time.Second) + defer cancel() + if err := h.Connect(cctx, *target); err != nil { + failed.Add(1) + h.Close() + return + } + ps, err := pubsub.NewGossipSub(ctx, h) + if err == nil { + var t *pubsub.Topic + if t, err = ps.Join("/allmende/1/listings/" + *region); err == nil { + _, err = t.Subscribe() + } + } + if err != nil { + failed.Add(1) + h.Close() + return + } + mu.Lock() + hosts = append(hosts, h) + mu.Unlock() + ok.Add(1) + }() + } + wg.Wait() + fmt.Printf("%d connected, %d failed, in %s\n", ok.Load(), failed.Load(), time.Since(start).Round(time.Millisecond)) + + select { + case <-ctx.Done(): + case <-time.After(*hold): + } + still := 0 + for _, h := range hosts { + if h.Network().Connectedness(target.ID) == network.Connected { + still++ + } + } + fmt.Printf("%d of %d still connected after %s\n", still, len(hosts), (*hold).Round(time.Second)) + for _, h := range hosts { + h.Close() + } +} diff --git a/docs/PROTOCOL.md b/docs/PROTOCOL.md index 7d06756..056cb28 100644 --- a/docs/PROTOCOL.md +++ b/docs/PROTOCOL.md @@ -706,8 +706,16 @@ Findings (see "What is left" in SPECS.md for the open ones): GB, fewer in a small container): with 200 nodes, 12 never got a single peer. A `--public` node now accepts 1000 inbound connections (`--max-conns` changes it; `-1` restores libp2p's default), and the table above shows the - result. Not measured: the memory of a seed holding 1000 connections against - the 512 MiB limit in `k8s/base/statefulset.yaml`. + result. **Memory with 1000 connections** (`cmd/allmende-conns`, one real + `--public` node, 1000 small libp2p clients on the same machine, each + subscribed to the region's topic): 35 MB idle, 131 MB with the 1000 + connections (about 100 KB each), 156 MB at the peak after 100 listings were + published and flooded to all 1000 (the Go heap on Linux, `VmHWM`). A + further 100 connection attempts were refused as intended (1000 connected, + 100 failed). That is about 30% of the 512 MiB pod limit. Real peers do more + than these clients (identify, DHT queries, relayed streams), so the + manifest also sets `GOMEMLIMIT=400MiB`, which makes the Go runtime collect + harder as the limit nears instead of being killed by it. 2. **Peer limits are crude.** The first run had no peer scoring: the attacker stayed connected to all 10 nodes it dialed after 500 bad messages. Now every node counts, per relaying peer and minute, the messages it gets (over 1200 diff --git a/k8s/base/statefulset.yaml b/k8s/base/statefulset.yaml index ba6ab44..d2b4806 100644 --- a/k8s/base/statefulset.yaml +++ b/k8s/base/statefulset.yaml @@ -73,6 +73,11 @@ spec: env: - name: ALLMENDE_DATA value: /data + # A soft limit below the container's 512Mi: the Go runtime then + # collects harder as it nears this, instead of growing until the + # kernel kills the pod. 1000 connections measured about 160 MB. + - name: GOMEMLIMIT + value: 400MiB ports: - name: p2p-tcp containerPort: 4001