Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove warning "maxopenconns is 0, default to 100" #3129

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/csconfig/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func TestLoadAPIServer(t *testing.T) {
DbConfig: &DatabaseCfg{
DbPath: "./testdata/test.db",
Type: "sqlite",
MaxOpenConns: ptr.Of(DEFAULT_MAX_OPEN_CONNS),
MaxOpenConns: DEFAULT_MAX_OPEN_CONNS,
UseWal: ptr.Of(true), // autodetected
DecisionBulkSize: defaultDecisionBulkSize,
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/csconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func NewDefaultConfig() *Config {
dbConfig := DatabaseCfg{
Type: "sqlite",
DbPath: DefaultDataPath("crowdsec.db"),
MaxOpenConns: ptr.Of(DEFAULT_MAX_OPEN_CONNS),
MaxOpenConns: DEFAULT_MAX_OPEN_CONNS,
}

globalCfg := Config{
Expand Down
7 changes: 4 additions & 3 deletions pkg/csconfig/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type DatabaseCfg struct {
Type string `yaml:"type"`
Flush *FlushDBCfg `yaml:"flush"`
LogLevel *log.Level `yaml:"log_level"`
MaxOpenConns *int `yaml:"max_open_conns,omitempty"`
MaxOpenConns int `yaml:"max_open_conns,omitempty"`
UseWal *bool `yaml:"use_wal,omitempty"`
DecisionBulkSize int `yaml:"decision_bulk_size,omitempty"`
}
Expand Down Expand Up @@ -68,8 +68,8 @@ func (c *Config) LoadDBConfig(inCli bool) error {
c.API.Server.DbConfig = c.DbConfig
}

if c.DbConfig.MaxOpenConns == nil {
c.DbConfig.MaxOpenConns = ptr.Of(DEFAULT_MAX_OPEN_CONNS)
if c.DbConfig.MaxOpenConns == 0 {
c.DbConfig.MaxOpenConns = DEFAULT_MAX_OPEN_CONNS
}

if !inCli && c.DbConfig.Type == "sqlite" {
Expand Down Expand Up @@ -134,6 +134,7 @@ func (d *DatabaseCfg) ConnectionString() string {
} else {
connString = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?parseTime=True", d.User, d.Password, d.Host, d.Port, d.DbName)
}

if d.Sslmode != "" {
connString = fmt.Sprintf("%s&tls=%s", connString, d.Sslmode)
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/csconfig/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ func TestLoadDBConfig(t *testing.T) {
DbConfig: &DatabaseCfg{
Type: "sqlite",
DbPath: "./testdata/test.db",
MaxOpenConns: ptr.Of(10),
MaxOpenConns: 10,
},
Cscli: &CscliCfg{},
API: &APICfg{
Server: &LocalApiServerCfg{},
},
},
expected: &DatabaseCfg{
Type: "sqlite",
DbPath: "./testdata/test.db",
MaxOpenConns: ptr.Of(10),
UseWal: ptr.Of(true),
Type: "sqlite",
DbPath: "./testdata/test.db",
MaxOpenConns: 10,
UseWal: ptr.Of(true),
DecisionBulkSize: defaultDecisionBulkSize,
},
},
Expand All @@ -49,6 +49,7 @@ func TestLoadDBConfig(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
err := tc.input.LoadDBConfig(false)
cstest.RequireErrorContains(t, err, tc.expectedErr)

if tc.expectedErr != "" {
return
}
Expand Down
19 changes: 8 additions & 11 deletions pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
_ "github.com/mattn/go-sqlite3"
log "github.com/sirupsen/logrus"

"github.com/crowdsecurity/go-cs-lib/ptr"

"github.com/crowdsecurity/crowdsec/pkg/csconfig"
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
"github.com/crowdsecurity/crowdsec/pkg/types"
Expand All @@ -37,12 +35,11 @@
return nil, err
}

if config.MaxOpenConns == nil {
log.Warningf("MaxOpenConns is 0, defaulting to %d", csconfig.DEFAULT_MAX_OPEN_CONNS)
config.MaxOpenConns = ptr.Of(csconfig.DEFAULT_MAX_OPEN_CONNS)
if config.MaxOpenConns == 0 {
config.MaxOpenConns = csconfig.DEFAULT_MAX_OPEN_CONNS
}

db.SetMaxOpenConns(*config.MaxOpenConns)
db.SetMaxOpenConns(config.MaxOpenConns)
drv := entsql.OpenDB(dbdialect, db)

return drv, nil
Expand Down Expand Up @@ -74,7 +71,7 @@

if config.Type == "sqlite" {
/*if it's the first startup, we want to touch and chmod file*/
if _, err := os.Stat(config.DbPath); os.IsNotExist(err) {
if _, err = os.Stat(config.DbPath); os.IsNotExist(err) {
f, err := os.OpenFile(config.DbPath, os.O_CREATE|os.O_RDWR, 0o600)
if err != nil {
return nil, fmt.Errorf("failed to create SQLite database file %q: %w", config.DbPath, err)
Expand All @@ -85,14 +82,14 @@
}
}
// Always try to set permissions to simplify a bit the code for windows (as the permissions set by OpenFile will be garbage)
if err := setFilePerm(config.DbPath, 0o640); err != nil {
return nil, fmt.Errorf("unable to set perms on %s: %v", config.DbPath, err)
if err = setFilePerm(config.DbPath, 0o640); err != nil {
return nil, fmt.Errorf("unable to set perms on %s: %w", config.DbPath, err)

Check warning on line 86 in pkg/database/database.go

View check run for this annotation

Codecov / codecov/patch

pkg/database/database.go#L86

Added line #L86 was not covered by tests
}
}

drv, err := getEntDriver(typ, dia, config.ConnectionString(), config)
if err != nil {
return nil, fmt.Errorf("failed opening connection to %s: %v", config.Type, err)
return nil, fmt.Errorf("failed opening connection to %s: %w", config.Type, err)

Check warning on line 92 in pkg/database/database.go

View check run for this annotation

Codecov / codecov/patch

pkg/database/database.go#L92

Added line #L92 was not covered by tests
}

client = ent.NewClient(ent.Driver(drv), entOpt)
Expand All @@ -104,7 +101,7 @@
}

if err = client.Schema.Create(ctx); err != nil {
return nil, fmt.Errorf("failed creating schema resources: %v", err)
return nil, fmt.Errorf("failed creating schema resources: %w", err)

Check warning on line 104 in pkg/database/database.go

View check run for this annotation

Codecov / codecov/patch

pkg/database/database.go#L104

Added line #L104 was not covered by tests
}

return &Client{
Expand Down
Loading