diff --git a/event/eventmanager.go b/event/eventmanager.go index c62d837..2d36ea9 100644 --- a/event/eventmanager.go +++ b/event/eventmanager.go @@ -33,9 +33,8 @@ func NewEventList(eventType Type, args ...interface{}) Event { // Manager is an Event Bus which allows subsystems to subscribe to certain EventTypes and publish others. type manager struct { - subscribers map[Type][]chan Event + subscribers sync.Map //[Type][]chan Event events chan Event - mapMutex sync.Mutex internal chan bool closed bool } @@ -57,7 +56,7 @@ func NewEventManager() Manager { // Initialize sets up the Manager. func (em *manager) initialize() { - em.subscribers = make(map[Type][]chan Event) + em.subscribers = sync.Map{} em.events = make(chan Event) em.internal = make(chan bool) em.closed = false @@ -67,9 +66,12 @@ func (em *manager) initialize() { // Subscribe takes an eventType and an Channel and associates them in the eventBus. All future events of that type // will be sent to the eventChannel. func (em *manager) Subscribe(eventType Type, eventChannel chan Event) { - em.mapMutex.Lock() - defer em.mapMutex.Unlock() - em.subscribers[eventType] = append(em.subscribers[eventType], eventChannel) + result, ok := em.subscribers.Load(eventType) + if !ok { + em.subscribers.Store(eventType, []chan Event{eventChannel}) + } else { + em.subscribers.Store(eventType, append(result.([]chan Event), eventChannel)) + } } // Publish takes an Event and sends it to the internal eventBus where it is distributed to all Subscribers @@ -94,10 +96,11 @@ func (em *manager) eventBus() { break } - // maps aren't thread safe - em.mapMutex.Lock() - subscribers := em.subscribers[event.EventType] - em.mapMutex.Unlock() + result, exists := em.subscribers.Load(event.EventType) + if !exists { + continue + } + subscribers := result.([]chan Event) // Send the event to any subscribers to that event type for _, subscriber := range subscribers { diff --git a/model/profile.go b/model/profile.go index b3109a0..9168c86 100644 --- a/model/profile.go +++ b/model/profile.go @@ -17,6 +17,8 @@ import ( "time" ) +// ****** Since we have a "live" 0.1 in the wild, we have to be careful of type changes breaking saving and loading ***** + // PublicProfile is a local copy of a CwtchIdentity type PublicProfile struct { Name string diff --git a/peer/cwtch_peer.go b/peer/cwtch_peer.go index 8a83dc3..00e3f1e 100644 --- a/peer/cwtch_peer.go +++ b/peer/cwtch_peer.go @@ -13,14 +13,12 @@ import ( "git.openprivacy.ca/openprivacy/libricochet-go/log" "github.com/golang/protobuf/proto" "strings" - "sync" "time" ) // cwtchPeer manages incoming and outgoing connections and all processing for a Cwtch cwtchPeer type cwtchPeer struct { Profile *model.Profile - mutex sync.Mutex shutdown bool started bool