Skip to content

Commit

Permalink
remove warning "maxopenconns is 0, default to 100" (#3129)
Browse files Browse the repository at this point in the history
* remove warning "maxopenconns is 0, default to 100"

also don't store as pointer since value 0 is not useful

* lint
  • Loading branch information
mmetc authored Jul 15, 2024
1 parent 9146383 commit 5390b8e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 21 deletions.
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 @@ import (
_ "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 @@ func getEntDriver(dbtype string, dbdialect string, dsn string, config *csconfig.
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 @@ func NewClient(ctx context.Context, config *csconfig.DatabaseCfg) (*Client, erro

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 @@ func NewClient(ctx context.Context, config *csconfig.DatabaseCfg) (*Client, erro
}
}
// 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)
}
}

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)
}

client = ent.NewClient(ent.Driver(drv), entOpt)
Expand All @@ -104,7 +101,7 @@ func NewClient(ctx context.Context, config *csconfig.DatabaseCfg) (*Client, erro
}

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)
}

return &Client{
Expand Down

0 comments on commit 5390b8e

Please sign in to comment.