Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Commit

Permalink
make RemoveProtocols take the write lock. (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
raulk authored Jul 10, 2019
1 parent 06edc32 commit ad0faef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
24 changes: 14 additions & 10 deletions pstoreds/protobook.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ func NewProtoBook(meta pstore.PeerMetadata) pstore.ProtoBook {
}

func (pb *dsProtoBook) SetProtocols(p peer.ID, protos ...string) error {
pb.segments.get(p).Lock()
defer pb.segments.get(p).Unlock()
s := pb.segments.get(p)
s.Lock()
defer s.Unlock()

protomap := make(map[string]struct{}, len(protos))
for _, proto := range protos {
Expand All @@ -51,8 +52,9 @@ func (pb *dsProtoBook) SetProtocols(p peer.ID, protos ...string) error {
}

func (pb *dsProtoBook) AddProtocols(p peer.ID, protos ...string) error {
pb.segments.get(p).Lock()
defer pb.segments.get(p).Unlock()
s := pb.segments.get(p)
s.Lock()
defer s.Unlock()

pmap, err := pb.getProtocolMap(p)
if err != nil {
Expand All @@ -67,8 +69,9 @@ func (pb *dsProtoBook) AddProtocols(p peer.ID, protos ...string) error {
}

func (pb *dsProtoBook) GetProtocols(p peer.ID) ([]string, error) {
pb.segments.get(p).RLock()
defer pb.segments.get(p).RUnlock()
s := pb.segments.get(p)
s.RLock()
defer s.RUnlock()

pmap, err := pb.getProtocolMap(p)
if err != nil {
Expand All @@ -84,8 +87,9 @@ func (pb *dsProtoBook) GetProtocols(p peer.ID) ([]string, error) {
}

func (pb *dsProtoBook) SupportsProtocols(p peer.ID, protos ...string) ([]string, error) {
pb.segments.get(p).RLock()
defer pb.segments.get(p).RUnlock()
s := pb.segments.get(p)
s.RLock()
defer s.RUnlock()

pmap, err := pb.getProtocolMap(p)
if err != nil {
Expand All @@ -104,8 +108,8 @@ func (pb *dsProtoBook) SupportsProtocols(p peer.ID, protos ...string) ([]string,

func (pb *dsProtoBook) RemoveProtocols(p peer.ID, protos ...string) error {
s := pb.segments.get(p)
s.RLock()
defer s.RUnlock()
s.Lock()
defer s.Unlock()

pmap, err := pb.getProtocolMap(p)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pstoremem/protobook.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ func (pb *memoryProtoBook) GetProtocols(p peer.ID) ([]string, error) {

func (pb *memoryProtoBook) RemoveProtocols(p peer.ID, protos ...string) error {
s := pb.segments.get(p)
s.RLock()
defer s.RUnlock()
s.Lock()
defer s.Unlock()

protomap, ok := s.protocols[p]
if !ok {
Expand Down

0 comments on commit ad0faef

Please sign in to comment.