From 05ae6ad9fea0e1484dab7546c3a7cdc12db32676 Mon Sep 17 00:00:00 2001 From: sukun Date: Wed, 3 Jul 2024 14:09:09 +0530 Subject: [PATCH] peerstore: reduce default protocol limit to 128 --- p2p/host/peerstore/pstoreds/protobook.go | 2 +- p2p/host/peerstore/pstoremem/protobook.go | 2 +- p2p/protocol/identify/id_test.go | 16 +++++++++++++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/p2p/host/peerstore/pstoreds/protobook.go b/p2p/host/peerstore/pstoreds/protobook.go index 40fa7d951b..9ef7d1c9fa 100644 --- a/p2p/host/peerstore/pstoreds/protobook.go +++ b/p2p/host/peerstore/pstoreds/protobook.go @@ -48,7 +48,7 @@ func NewProtoBook(meta pstore.PeerMetadata, opts ...ProtoBookOption) (*dsProtoBo } return ret }(), - maxProtos: 1024, + maxProtos: 128, } for _, opt := range opts { diff --git a/p2p/host/peerstore/pstoremem/protobook.go b/p2p/host/peerstore/pstoremem/protobook.go index e5bfae14f2..283dd105f9 100644 --- a/p2p/host/peerstore/pstoremem/protobook.go +++ b/p2p/host/peerstore/pstoremem/protobook.go @@ -51,7 +51,7 @@ func NewProtoBook(opts ...ProtoBookOption) (*memoryProtoBook, error) { } return ret }(), - maxProtos: 1024, + maxProtos: 128, } for _, opt := range opts { diff --git a/p2p/protocol/identify/id_test.go b/p2p/protocol/identify/id_test.go index a65d64f24e..904e47cece 100644 --- a/p2p/protocol/identify/id_test.go +++ b/p2p/protocol/identify/id_test.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "math/rand" "slices" "sync" "testing" @@ -730,6 +731,15 @@ func TestLargeIdentifyMessage(t *testing.T) { } } +func randString(n int) string { + chars := "abcdefghijklmnopqrstuvwxyz" + buf := make([]byte, n) + for i := 0; i < n; i++ { + buf[i] = chars[rand.Intn(len(chars))] + } + return string(buf) +} + func TestLargePushMessage(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -738,9 +748,9 @@ func TestLargePushMessage(t *testing.T) { h2 := blhost.NewBlankHost(swarmt.GenSwarm(t)) // add protocol strings to make the message larger - // about 2K of protocol strings - for i := 0; i < 500; i++ { - r := protocol.ID(fmt.Sprintf("rand%d", i)) + // about 3K of protocol strings + for i := 0; i < 100; i++ { + r := protocol.ID(fmt.Sprintf("%s-%d", randString(30), i)) h1.SetStreamHandler(r, func(network.Stream) {}) h2.SetStreamHandler(r, func(network.Stream) {}) }