load grok patterns concurrently to speed up start process with re2

This commit is contained in:
Sebastien Blot 2023-05-15 09:30:45 +02:00
parent 8aca0ea860
commit d0aba7dd9a
No known key found for this signature in database
GPG key ID: DFC2902F40449F6A

View file

@ -10,6 +10,7 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
"github.com/crowdsecurity/crowdsec/pkg/fflag"
"golang.org/x/sync/errgroup"
"github.com/crowdsecurity/grokky"
log "github.com/sirupsen/logrus"
@ -41,14 +42,32 @@ func Init(c map[string]interface{}) (*UnixParserCtx, error) {
return nil, err
}
r.DataFolder = c["data"].(string)
filesList := make([]string, 0)
for _, f := range files {
if strings.Contains(f.Name(), ".") {
continue
}
if err := r.Grok.AddFromFile(path.Join(c["patterns"].(string), f.Name())); err != nil {
log.Errorf("failed to load pattern %s : %v", f.Name(), err)
return nil, err
filesList = append(filesList, path.Join(c["patterns"].(string), f.Name()))
}
g := new(errgroup.Group)
for i := 0; i < len(filesList); i += 10 {
start := i
end := start + 10
if end > len(filesList) {
end = len(filesList)
}
g.Go(func() error {
for _, f := range filesList[start:end] {
if err := r.Grok.AddFromFile(f); err != nil {
log.Errorf("failed to load pattern %s : %v", f, err)
return err
}
}
return nil
})
}
if err := g.Wait(); err != nil {
return nil, err
}
log.Debugf("Loaded %d pattern files", len(files))
return &r, nil