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:
vyzo 2018-08-26 13:19:05 +03:00
parent 7c508b4fba
commit 7c1012c247
1 changed files with 22 additions and 0 deletions

View File

@ -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)