Skip to content

Commit

Permalink
fix(profile.MemStore): address race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed May 10, 2018
1 parent ab7e035 commit 58ece89
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions repo/profile/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func (m MemStore) PutProfile(profile *Profile) error {

// PeernameID gives the ID for a given peername
func (m MemStore) PeernameID(peername string) (ID, error) {
m.RLock()
defer m.RUnlock()
m.Lock()
defer m.Unlock()

for id, profile := range m.store {
if profile.Peername == peername {
Expand All @@ -63,8 +63,8 @@ func (m MemStore) PeernameID(peername string) (ID, error) {
// TODO - this func implies that peer.ID's are only ever connected to the same
// profile. That could cause trouble.
func (m MemStore) PeerProfile(id peer.ID) (*Profile, error) {
m.RLock()
defer m.RUnlock()
m.Lock()
defer m.Unlock()

// str := fmt.Sprintf("/ipfs/%s", id.Pretty())

Expand All @@ -81,8 +81,8 @@ func (m MemStore) PeerProfile(id peer.ID) (*Profile, error) {

// PeerIDs gives the peer.IDs list for a given peername
func (m MemStore) PeerIDs(id ID) ([]peer.ID, error) {
m.RLock()
defer m.RUnlock()
m.Lock()
defer m.Unlock()

for proid, profile := range m.store {
if id == proid {
Expand All @@ -95,8 +95,8 @@ func (m MemStore) PeerIDs(id ID) ([]peer.ID, error) {

// List hands the full list of peers back
func (m MemStore) List() (map[ID]*Profile, error) {
m.RLock()
defer m.RUnlock()
m.Lock()
defer m.Unlock()

res := map[ID]*Profile{}
for id, p := range m.store {
Expand All @@ -107,8 +107,8 @@ func (m MemStore) List() (map[ID]*Profile, error) {

// GetProfile give's peer info from the store for a given peer.ID
func (m MemStore) GetProfile(id ID) (*Profile, error) {
m.RLock()
defer m.RUnlock()
m.Lock()
defer m.Unlock()

if m.store[id] == nil {
return nil, ErrNotFound
Expand Down

0 comments on commit 58ece89

Please sign in to comment.