mirror of
https://github.com/libp2p/go-libp2p-peerstore.git
synced 2025-01-14 02:30:05 +08:00
reduce mutex contention in SetProtocols
This commit is contained in:
parent
a1f426f3d0
commit
3f028c937b
@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
peer "github.com/libp2p/go-libp2p-core/peer"
|
||||
"github.com/libp2p/go-libp2p-core/peer"
|
||||
|
||||
pstore "github.com/libp2p/go-libp2p-core/peerstore"
|
||||
)
|
||||
@ -43,15 +43,15 @@ func (pb *dsProtoBook) SetProtocols(p peer.ID, protos ...string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
s := pb.segments.get(p)
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
|
||||
protomap := make(map[string]struct{}, len(protos))
|
||||
for _, proto := range protos {
|
||||
protomap[proto] = struct{}{}
|
||||
}
|
||||
|
||||
s := pb.segments.get(p)
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
|
||||
return pb.meta.Put(p, "protocols", protomap)
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@ package pstoremem
|
||||
import (
|
||||
"sync"
|
||||
|
||||
peer "github.com/libp2p/go-libp2p-core/peer"
|
||||
"github.com/libp2p/go-libp2p-core/peer"
|
||||
|
||||
pstore "github.com/libp2p/go-libp2p-core/peerstore"
|
||||
)
|
||||
@ -71,16 +71,15 @@ func (pb *memoryProtoBook) SetProtocols(p peer.ID, protos ...string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
s := pb.segments.get(p)
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
|
||||
newprotos := make(map[string]struct{}, len(protos))
|
||||
for _, proto := range protos {
|
||||
newprotos[pb.internProtocol(proto)] = struct{}{}
|
||||
}
|
||||
|
||||
s := pb.segments.get(p)
|
||||
s.Lock()
|
||||
s.protocols[p] = newprotos
|
||||
s.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user