Skip to content

Commit 83eab42

Browse files
committed
refact parser, leakybucket: internal concurrency safe name generator
1 parent ee8fd08 commit 83eab42

File tree

7 files changed

+1566
-15
lines changed

7 files changed

+1566
-15
lines changed

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require (
1313
github.com/aws/aws-lambda-go v1.47.0
1414
github.com/aws/aws-sdk-go v1.52.0
1515
github.com/beevik/etree v1.4.1
16-
github.com/blackfireio/osinfo v1.1.0
16+
github.com/blackfireio/osinfo v1.1.0 // indirect
1717
github.com/bluele/gcache v0.0.2
1818
github.com/buger/jsonparser v1.1.1
1919
github.com/cenkalti/backoff/v5 v5.0.2
@@ -45,7 +45,6 @@ require (
4545
github.com/google/go-querystring v1.1.0
4646
github.com/google/uuid v1.6.0
4747
github.com/google/winops v0.0.0-20230712152054-af9b550d0601
48-
github.com/goombaio/namegenerator v0.0.0-20181006234301-989e774b106e
4948
github.com/gorilla/websocket v1.5.0
5049
github.com/hashicorp/go-hclog v1.5.0
5150
github.com/hashicorp/go-plugin v1.6.3

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,6 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
233233
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
234234
github.com/google/winops v0.0.0-20230712152054-af9b550d0601 h1:XvlrmqZIuwxuRE88S9mkxX+FkV+YakqbiAC5Z4OzDnM=
235235
github.com/google/winops v0.0.0-20230712152054-af9b550d0601/go.mod h1:rT1mcjzuvcDDbRmUTsoH6kV0DG91AkFe9UCjASraK5I=
236-
github.com/goombaio/namegenerator v0.0.0-20181006234301-989e774b106e h1:XmA6L9IPRdUr28a+SK/oMchGgQy159wvzXA5tJ7l+40=
237-
github.com/goombaio/namegenerator v0.0.0-20181006234301-989e774b106e/go.mod h1:AFIo+02s+12CEg8Gzz9kzhCbmbq6JcKNrhHffCGA9z4=
238236
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
239237
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
240238
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=

pkg/leakybucket/bucket.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/crowdsecurity/go-cs-lib/trace"
1616

1717
"github.com/crowdsecurity/crowdsec/pkg/metrics"
18+
"github.com/crowdsecurity/crowdsec/pkg/namegenerator"
1819
"github.com/crowdsecurity/crowdsec/pkg/time/rate"
1920
"github.com/crowdsecurity/crowdsec/pkg/types"
2021
)
@@ -112,7 +113,7 @@ func FromFactory(bucketFactory BucketFactory) *Leaky {
112113
l := &Leaky{
113114
Name: bucketFactory.Name,
114115
Limiter: limiter,
115-
Uuid: seed.Generate(),
116+
Uuid: namegenerator.GetRandomName(),
116117
Queue: types.NewQueue(Qsize),
117118
CacheSize: bucketFactory.CacheSize,
118119
Out: make(chan *types.Queue, 1),

pkg/leakybucket/manager_load.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/expr-lang/expr"
1313
"github.com/expr-lang/expr/vm"
14-
"github.com/goombaio/namegenerator"
1514
log "github.com/sirupsen/logrus"
1615
"gopkg.in/tomb.v2"
1716
yaml "gopkg.in/yaml.v2"
@@ -23,6 +22,7 @@ import (
2322
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
2423
"github.com/crowdsecurity/crowdsec/pkg/cwversion/constraint"
2524
"github.com/crowdsecurity/crowdsec/pkg/exprhelpers"
25+
"github.com/crowdsecurity/crowdsec/pkg/namegenerator"
2626
"github.com/crowdsecurity/crowdsec/pkg/types"
2727
)
2828

@@ -75,9 +75,6 @@ type BucketFactory struct {
7575
orderEvent bool
7676
}
7777

78-
// we use one NameGenerator for all the future buckets
79-
var seed = namegenerator.NewNameGenerator(time.Now().UTC().UnixNano())
80-
8178
func validateLeakyType(bucketFactory *BucketFactory) error {
8279
if bucketFactory.Capacity <= 0 { // capacity must be a positive int
8380
return fmt.Errorf("bad capacity for leaky '%d'", bucketFactory.Capacity)
@@ -288,7 +285,7 @@ func loadBucketFactoriesFromFile(item *cwhub.Item, hub *cwhub.Hub, buckets *Buck
288285
}
289286

290287
bucketFactory.Filename = filepath.Clean(itemPath)
291-
bucketFactory.BucketName = seed.Generate()
288+
bucketFactory.BucketName = namegenerator.GetRandomName()
292289
bucketFactory.ret = response
293290

294291
bucketFactory.Simulated = simulationConfig.IsSimulated(bucketFactory.Name)

0 commit comments

Comments
 (0)