Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

peerstore: don't intern protocols #2860

Merged
merged 3 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion p2p/host/peerstore/pstoreds/protobook.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewProtoBook(meta pstore.PeerMetadata, opts ...ProtoBookOption) (*dsProtoBo
}
return ret
}(),
maxProtos: 1024,
maxProtos: 128,
}

for _, opt := range opts {
Expand Down
39 changes: 7 additions & 32 deletions p2p/host/peerstore/pstoremem/protobook.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ type memoryProtoBook struct {
segments protoSegments

maxProtos int

lk sync.RWMutex
interned map[protocol.ID]protocol.ID
}

var _ pstore.ProtoBook = (*memoryProtoBook)(nil)
Expand All @@ -44,7 +41,6 @@ func WithMaxProtocols(num int) ProtoBookOption {

func NewProtoBook(opts ...ProtoBookOption) (*memoryProtoBook, error) {
pb := &memoryProtoBook{
interned: make(map[protocol.ID]protocol.ID, 256),
segments: func() (ret protoSegments) {
for i := range ret {
ret[i] = &protoSegment{
Expand All @@ -53,7 +49,7 @@ func NewProtoBook(opts ...ProtoBookOption) (*memoryProtoBook, error) {
}
return ret
}(),
maxProtos: 1024,
maxProtos: 128,
}

for _, opt := range opts {
Expand All @@ -64,38 +60,14 @@ func NewProtoBook(opts ...ProtoBookOption) (*memoryProtoBook, error) {
return pb, nil
}

func (pb *memoryProtoBook) internProtocol(proto protocol.ID) protocol.ID {
// check if it is interned with the read lock
pb.lk.RLock()
interned, ok := pb.interned[proto]
pb.lk.RUnlock()

if ok {
return interned
}

// intern with the write lock
pb.lk.Lock()
defer pb.lk.Unlock()

// check again in case it got interned in between locks
interned, ok = pb.interned[proto]
if ok {
return interned
}

pb.interned[proto] = proto
return proto
}

func (pb *memoryProtoBook) SetProtocols(p peer.ID, protos ...protocol.ID) error {
if len(protos) > pb.maxProtos {
return errTooManyProtocols
}

newprotos := make(map[protocol.ID]struct{}, len(protos))
for _, proto := range protos {
newprotos[pb.internProtocol(proto)] = struct{}{}
newprotos[proto] = struct{}{}
}

s := pb.segments.get(p)
Expand All @@ -121,7 +93,7 @@ func (pb *memoryProtoBook) AddProtocols(p peer.ID, protos ...protocol.ID) error
}

for _, proto := range protos {
protomap[pb.internProtocol(proto)] = struct{}{}
protomap[proto] = struct{}{}
}
return nil
}
Expand Down Expand Up @@ -151,7 +123,10 @@ func (pb *memoryProtoBook) RemoveProtocols(p peer.ID, protos ...protocol.ID) err
}

for _, proto := range protos {
delete(protomap, pb.internProtocol(proto))
delete(protomap, proto)
}
if len(protomap) == 0 {
delete(s.protocols, p)
}
return nil
}
Expand Down
16 changes: 13 additions & 3 deletions p2p/protocol/identify/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"math/rand"
"slices"
"sync"
"testing"
Expand Down Expand Up @@ -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()
Expand All @@ -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) {})
}
Expand Down
Loading