fix race conditions around contact access

This commit is contained in:
Dan Ballard 2019-07-24 15:42:06 -07:00
parent 8fe6648100
commit ed904ed79d
2 changed files with 21 additions and 11 deletions

View File

@ -308,7 +308,9 @@ func (this *GrandCentralDispatcher) savePeerSettings(onion, nick string) {
event.Data: nick,
}))
this.UIState.contacts[onion].DisplayName = nick
cif, _ := this.UIState.contacts.Load(onion)
c := cif.(*gobjects.Contact)
c.DisplayName = nick
this.UIState.UpdateContact(onion)
}
@ -352,7 +354,9 @@ func (this *GrandCentralDispatcher) saveGroupSettings(groupID, nick string) {
event.Data: nick,
}))
this.UIState.contacts[groupID].DisplayName = nick
cif, _ := this.UIState.contacts.Load(groupID)
c := cif.(*gobjects.Contact)
c.DisplayName = nick
this.UIState.UpdateContact(groupID)
}

View File

@ -13,20 +13,20 @@ import (
type InterfaceState struct {
parentGcd *GrandCentralDispatcher
contacts map[string]*gobjects.Contact
contacts sync.Map // string : *gobjects.Contact
messages sync.Map
}
func NewUIState(gcd *GrandCentralDispatcher) (uis InterfaceState) {
uis = InterfaceState{gcd, make(map[string]*gobjects.Contact), sync.Map{}}
uis = InterfaceState{gcd, sync.Map{}, sync.Map{}}
return
}
func (this *InterfaceState) AddContact(c *gobjects.Contact) {
if len(c.Handle) == 32 { // ADD GROUP
if _, found := this.contacts[c.Handle]; !found {
if _, found := this.contacts.Load(c.Handle); !found {
this.parentGcd.AddContact(c.Handle, c.DisplayName, c.Image, c.Server, c.Badge, c.Status, c.Trusted, c.Loading)
this.contacts[c.Handle] = c
this.contacts.Store(c.Handle, c)
}
return
} else if len(c.Handle) != 56 {
@ -35,8 +35,8 @@ func (this *InterfaceState) AddContact(c *gobjects.Contact) {
return
}
if _, found := this.contacts[c.Handle]; !found {
this.contacts[c.Handle] = c
if _, found := this.contacts.Load(c.Handle); !found {
this.contacts.Store(c.Handle, c)
this.parentGcd.AddContact(c.Handle, c.DisplayName, c.Image, c.Server, c.Badge, c.Status, c.Trusted, false)
if the.Peer.GetContact(c.Handle) == nil {
the.Peer.AddContact(c.DisplayName, c.Handle, c.Trusted)
@ -46,7 +46,7 @@ func (this *InterfaceState) AddContact(c *gobjects.Contact) {
}
func (this *InterfaceState) GetContact(handle string) *gobjects.Contact {
if _, found := this.contacts[handle]; !found {
if _, found := this.contacts.Load(handle); !found {
if len(handle) == 32 {
group := the.Peer.GetGroup(handle)
if group != nil {
@ -84,7 +84,12 @@ func (this *InterfaceState) GetContact(handle string) *gobjects.Contact {
}
}
return this.contacts[handle]
contactIntf, _ := this.contacts.Load(handle)
if contactIntf == nil {
return nil
}
contact := contactIntf.(*gobjects.Contact)
return contact
}
func (this *InterfaceState) AddSendMessageError(peer string, signature string, err string) {
@ -166,8 +171,9 @@ func (this *InterfaceState) GetMessages(handle string) []*gobjects.Message {
}
func (this *InterfaceState) UpdateContact(handle string) {
c, found := this.contacts[handle]
cif, found := this.contacts.Load(handle)
if found {
c := cif.(*gobjects.Contact)
this.parentGcd.UpdateContact(c.Handle, c.DisplayName, c.Image, c.Server, c.Badge, c.Status, c.Trusted, c.Loading)
}
}