optimize fast path for single topic validator
so that we don't do the goroutine dance for just a single validator, which ought to be the most common case.
This commit is contained in:
parent
7c508b4fba
commit
7c1012c247
22
pubsub.go
22
pubsub.go
|
@ -506,6 +506,10 @@ func (p *PubSub) validateSignature(msg *Message) bool {
|
|||
}
|
||||
|
||||
func (p *PubSub) validateTopic(vals []*topicVal, msg *Message) bool {
|
||||
if len(vals) == 1 {
|
||||
return p.validateSingleTopic(vals[0], msg)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(p.ctx)
|
||||
defer cancel()
|
||||
|
||||
|
@ -545,6 +549,24 @@ loop:
|
|||
return true
|
||||
}
|
||||
|
||||
// fast path for single topic validation that avoids the extra goroutine
|
||||
func (p *PubSub) validateSingleTopic(val *topicVal, msg *Message) bool {
|
||||
select {
|
||||
case val.validateThrottle <- struct{}{}:
|
||||
ctx, cancel := context.WithCancel(p.ctx)
|
||||
defer cancel()
|
||||
|
||||
res := val.validateMsg(ctx, msg)
|
||||
<-val.validateThrottle
|
||||
|
||||
return res
|
||||
|
||||
default:
|
||||
log.Debugf("validation throttled for topic %s", val.topic)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (p *PubSub) publishMessage(from peer.ID, pmsg *pb.Message) {
|
||||
p.notifySubs(pmsg)
|
||||
p.rt.Publish(from, pmsg)
|
||||
|
|
Loading…
Reference in New Issue