Преглед изворни кода

avoid create a new name generator at each bucket instanciation, it's not that cheap (#1591)

Thibault "bui" Koechlin пре 3 година
родитељ
комит
ff72a3c1c7
2 измењених фајлова са 4 додато и 4 уклоњено
  1. 1 2
      pkg/leakybucket/bucket.go
  2. 3 2
      pkg/leakybucket/manager_load.go

+ 1 - 2
pkg/leakybucket/bucket.go

@@ -9,7 +9,6 @@ import (
 	//"log"
 	"github.com/crowdsecurity/crowdsec/pkg/time/rate"
 	"github.com/crowdsecurity/crowdsec/pkg/types"
-	"github.com/goombaio/namegenerator"
 	"gopkg.in/tomb.v2"
 
 	//rate "time/rate"
@@ -158,7 +157,7 @@ func FromFactory(bucketFactory BucketFactory) *Leaky {
 	l := &Leaky{
 		Name:            bucketFactory.Name,
 		Limiter:         limiter,
-		Uuid:            namegenerator.NewNameGenerator(time.Now().UTC().UnixNano()).Generate(),
+		Uuid:            seed.Generate(),
 		Queue:           NewQueue(Qsize),
 		CacheSize:       bucketFactory.CacheSize,
 		Out:             make(chan *Queue, 1),

+ 3 - 2
pkg/leakybucket/manager_load.go

@@ -74,6 +74,9 @@ type BucketFactory struct {
 	wgDumpState     *sync.WaitGroup           `yaml:"-"`
 }
 
+//we use one NameGenerator for all the future buckets
+var seed namegenerator.Generator = namegenerator.NewNameGenerator(time.Now().UTC().UnixNano())
+
 func ValidateFactory(bucketFactory *BucketFactory) error {
 	if bucketFactory.Name == "" {
 		return fmt.Errorf("bucket must have name")
@@ -147,8 +150,6 @@ func LoadBuckets(cscfg *csconfig.CrowdsecServiceCfg, files []string, tomb *tomb.
 		response chan types.Event
 	)
 
-	var seed namegenerator.Generator = namegenerator.NewNameGenerator(time.Now().UTC().UnixNano())
-
 	response = make(chan types.Event, 1)
 	for _, f := range files {
 		log.Debugf("Loading '%s'", f)