Hi guys ! The bandwidth of the relay server is very small. I need to limit the connection speed and count the traffic consumed by each node. What should I do? Give me some adivice !
p2p/protocol/circuitv2/relay/relay_test.go:117
`func TestBasicRelay(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
hosts, upgraders := getNetHosts(t, ctx, 3)
addTransport(t, hosts[0], upgraders[0])
addTransport(t, hosts[2], upgraders[2])
rch := make(chan []byte, 1)
relayServer := hosts[1]
boxServer := hosts[0]
mobileClient := hosts[2]
relayServer.SetStreamHandler("/libp2p/circuit/relay/0.1.0", func(stream network.Stream) {
fmt.Println(stream)
})
boxServer.SetStreamHandler("test", func(s network.Stream) {
defer func() {
s.Close()
fmt.Println("close")
}()
defer func() {
fmt.Println("close")
close(rch)
}()
// box 收到消息
buf := make([]byte, 1024)
nread := 0
for nread < len(buf) {
n, err := s.Read(buf[nread:])
nread += n
if err != nil {
if err == io.EOF {
break
}
t.Fatal(err)
}
}
rch <- buf[:nread]
})
r, err := relay.New(relayServer,
relay.WithResources(relay.DefaultResources()),
relay.WithLimit(relay.DefaultLimit()),
relay.WithACL(&MyFiflter{}))
if err != nil {
t.Fatal(err)
}
defer r.Close()
connect(t, boxServer, relayServer)
connect(t, mobileClient, relayServer)
rinfo := relayServer.Peerstore().PeerInfo(relayServer.ID())
rsvp, err := client.Reserve(ctx, boxServer, rinfo)
if err != nil {
t.Fatal(err)
}
if rsvp.Voucher == nil {
t.Fatal("no reservation voucher")
}
raddr, err := ma.NewMultiaddr(fmt.Sprintf("/p2p/%s/p2p-circuit/p2p/%s", relayServer.ID(), boxServer.ID()))
if err != nil {
t.Fatal(err)
}
err = mobileClient.Connect(ctx, peer.AddrInfo{ID: boxServer.ID(), Addrs: []ma.Multiaddr{raddr}})
if err != nil {
t.Fatal(err)
}
conns := mobileClient.Network().ConnsToPeer(boxServer.ID())
if len(conns) != 1 {
t.Fatalf("expected 1 connection, but got %d", len(conns))
}
if !conns[0].Stat().Transient {
t.Fatal("expected transient connection")
}
s, err := mobileClient.NewStream(network.WithUseTransient(ctx, "test"), boxServer.ID(), "test")
if err != nil {
t.Fatal(err)
}
msg := []byte("relay works!")
nwritten, err := s.Write(msg)
if err != nil {
t.Fatal(err)
}
if nwritten != len(msg) {
t.Fatalf("expected to write %d bytes, but wrote %d instead", len(msg), nwritten)
}
s.CloseWrite()