@@ -138,10 +138,11 @@ type Pss struct {
138138 symKeyDecryptCacheCapacity int // max amount of symkeys to keep.
139139
140140 // message handling
141- handlers map [Topic ]map [* handler ]bool // topic and version based pss payload handlers. See pss.Handle()
142- handlersMu sync.RWMutex
143- hashPool sync.Pool
144- topicHandlerCaps map [Topic ]* handlerCaps // caches capabilities of each topic's handlers (see handlerCap* consts in types.go)
141+ handlers map [Topic ]map [* handler ]bool // topic and version based pss payload handlers. See pss.Handle()
142+ handlersMu sync.RWMutex
143+ hashPool sync.Pool
144+ topicHandlerCaps map [Topic ]* handlerCaps // caches capabilities of each topic's handlers
145+ topicHandlerCapsMu sync.RWMutex
145146
146147 // process
147148 quitC chan struct {}
@@ -307,6 +308,19 @@ func (p *Pss) PublicKey() *ecdsa.PublicKey {
307308// SECTION: Message handling
308309/////////////////////////////////////////////////////////////////////
309310
311+ func (p * Pss ) getTopicHandlerCaps (topic Topic ) (hc * handlerCaps , found bool ) {
312+ p .topicHandlerCapsMu .RLock ()
313+ defer p .topicHandlerCapsMu .RUnlock ()
314+ hc , found = p .topicHandlerCaps [topic ]
315+ return
316+ }
317+
318+ func (p * Pss ) setTopicHandlerCaps (topic Topic , hc * handlerCaps ) {
319+ p .topicHandlerCapsMu .Lock ()
320+ defer p .topicHandlerCapsMu .Unlock ()
321+ p .topicHandlerCaps [topic ] = hc
322+ }
323+
310324// Links a handler function to a Topic
311325//
312326// All incoming messages with an envelope Topic matching the
@@ -323,20 +337,24 @@ func (p *Pss) Register(topic *Topic, hndlr *handler) func() {
323337 if handlers == nil {
324338 handlers = make (map [* handler ]bool )
325339 p .handlers [* topic ] = handlers
326- log .Debug ("registered handler" , "caps " , hndlr .caps )
340+ log .Debug ("registered handler" , "capabilities " , hndlr .caps )
327341 }
328342 if hndlr .caps == nil {
329343 hndlr .caps = & handlerCaps {}
330344 }
331345 handlers [hndlr ] = true
332- if _ , ok := p .topicHandlerCaps [* topic ]; ! ok {
333- p .topicHandlerCaps [* topic ] = & handlerCaps {}
346+
347+ capabilities , ok := p .getTopicHandlerCaps (* topic )
348+ if ! ok {
349+ capabilities = & handlerCaps {}
350+ p .setTopicHandlerCaps (* topic , capabilities )
334351 }
352+
335353 if hndlr .caps .raw {
336- p . topicHandlerCaps [ * topic ] .raw = true
354+ capabilities .raw = true
337355 }
338356 if hndlr .caps .prox {
339- p . topicHandlerCaps [ * topic ] .prox = true
357+ capabilities .prox = true
340358 }
341359 return func () { p .deregister (topic , hndlr ) }
342360}
@@ -357,7 +375,7 @@ func (p *Pss) deregister(topic *Topic, hndlr *handler) {
357375 caps .prox = true
358376 }
359377 }
360- p .topicHandlerCaps [ * topic ] = caps
378+ p .setTopicHandlerCaps ( * topic , caps )
361379 return
362380 }
363381 delete (handlers , hndlr )
@@ -390,8 +408,8 @@ func (p *Pss) handlePssMsg(ctx context.Context, msg interface{}) error {
390408 // raw is simplest handler contingency to check, so check that first
391409 var isRaw bool
392410 if pssmsg .isRaw () {
393- if _ , ok := p .topicHandlerCaps [ psstopic ] ; ok {
394- if ! p . topicHandlerCaps [ psstopic ] .raw {
411+ if capabilities , ok := p .getTopicHandlerCaps ( psstopic ) ; ok {
412+ if ! capabilities .raw {
395413 log .Debug ("No handler for raw message" , "topic" , psstopic )
396414 return nil
397415 }
@@ -404,8 +422,8 @@ func (p *Pss) handlePssMsg(ctx context.Context, msg interface{}) error {
404422 // - prox handler on message and we are in prox regardless of partial address match
405423 // store this result so we don't calculate again on every handler
406424 var isProx bool
407- if _ , ok := p .topicHandlerCaps [ psstopic ] ; ok {
408- isProx = p . topicHandlerCaps [ psstopic ] .prox
425+ if capabilities , ok := p .getTopicHandlerCaps ( psstopic ) ; ok {
426+ isProx = capabilities .prox
409427 }
410428 isRecipient := p .isSelfPossibleRecipient (pssmsg , isProx )
411429 if ! isRecipient {
@@ -783,8 +801,8 @@ func (p *Pss) SendRaw(address PssAddress, topic Topic, msg []byte) error {
783801
784802 // if we have a proxhandler on this topic
785803 // also deliver message to ourselves
786- if _ , ok := p .topicHandlerCaps [ topic ] ; ok {
787- if p .isSelfPossibleRecipient (pssMsg , true ) && p . topicHandlerCaps [ topic ] .prox {
804+ if capabilities , ok := p .getTopicHandlerCaps ( topic ) ; ok {
805+ if p .isSelfPossibleRecipient (pssMsg , true ) && capabilities .prox {
788806 return p .process (pssMsg , true , true )
789807 }
790808 }
@@ -885,8 +903,8 @@ func (p *Pss) send(to []byte, topic Topic, msg []byte, asymmetric bool, key []by
885903 if err != nil {
886904 return err
887905 }
888- if _ , ok := p .topicHandlerCaps [ topic ] ; ok {
889- if p .isSelfPossibleRecipient (pssMsg , true ) && p . topicHandlerCaps [ topic ] .prox {
906+ if capabilities , ok := p .getTopicHandlerCaps ( topic ) ; ok {
907+ if p .isSelfPossibleRecipient (pssMsg , true ) && capabilities .prox {
890908 return p .process (pssMsg , true , true )
891909 }
892910 }
0 commit comments