don't spawn an extra goroutine for the validator context

This commit is contained in:
vyzo 2018-01-13 22:14:01 +02:00
parent 145a84a33b
commit f1be0f1296

View File

@ -38,22 +38,13 @@ func (sub *Subscription) Cancel() {
} }
func (sub *Subscription) validateMsg(ctx context.Context, msg *Message) bool { func (sub *Subscription) validateMsg(ctx context.Context, msg *Message) bool {
result := make(chan bool, 1)
vctx, cancel := context.WithTimeout(ctx, sub.validateTimeout) vctx, cancel := context.WithTimeout(ctx, sub.validateTimeout)
defer cancel() defer cancel()
go func() { valid := sub.validate(vctx, msg)
result <- sub.validate(vctx, msg) if !valid {
}() log.Debugf("validation failed for topic %s", sub.topic)
select {
case valid := <-result:
if !valid {
log.Debugf("validation failed for topic %s", sub.topic)
}
return valid
case <-vctx.Done():
log.Debugf("validation timeout for topic %s", sub.topic)
return false
} }
return valid
} }