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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
118 lines
3.2 KiB
Go
118 lines
3.2 KiB
Go
// 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/<pid>/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 <multiaddr> [-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/<peer id> 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()
|
|
}
|
|
}
|