(feat) maximum open DB connections is configurable

This commit is contained in:
TP Honey 2021-06-08 11:00:20 +01:00
parent ca3ac612c5
commit 9159661212
5 changed files with 16 additions and 7 deletions

1
.gitignore vendored
View file

@ -1,4 +1,5 @@
.vscode
__debug_bin
*.sqlite
*.txt
*.out

View file

@ -111,9 +111,10 @@ type (
// Database provides the database configuration.
Database struct {
Driver string `envconfig:"DRONE_DATABASE_DRIVER" default:"sqlite3"`
Datasource string `envconfig:"DRONE_DATABASE_DATASOURCE" default:"core.sqlite"`
Secret string `envconfig:"DRONE_DATABASE_SECRET"`
Driver string `envconfig:"DRONE_DATABASE_DRIVER" default:"sqlite3"`
Datasource string `envconfig:"DRONE_DATABASE_DATASOURCE" default:"core.sqlite"`
Secret string `envconfig:"DRONE_DATABASE_SECRET"`
MaxConnections int `envconfig:"DRONE_DATABASE_MAX_CONNECTIONS" default:"0"`
// Feature flag
LegacyBatch bool `envconfig:"DRONE_DATABASE_LEGACY_BATCH"`

View file

@ -63,6 +63,7 @@ func provideDatabase(config config.Config) (*db.DB, error) {
return db.Connect(
config.Database.Driver,
config.Database.Datasource,
config.Database.MaxConnections,
)
}

View file

@ -19,7 +19,7 @@ import (
)
// Connect to a database and verify with a ping.
func Connect(driver, datasource string) (*DB, error) {
func Connect(driver, datasource string, maxOpenConnections int) (*DB, error) {
db, err := sql.Open(driver, datasource)
if err != nil {
return nil, err
@ -34,6 +34,8 @@ func Connect(driver, datasource string) (*DB, error) {
if err := setupDatabase(db, driver); err != nil {
return nil, err
}
// generally set to 0, user configured for larger installs
db.SetMaxOpenConns(maxOpenConnections)
var engine Driver
var locker Locker

View file

@ -16,6 +16,7 @@ package dbtest
import (
"os"
"strconv"
"github.com/drone/drone/store/shared/db"
@ -30,14 +31,17 @@ import (
// Connect opens a new test database connection.
func Connect() (*db.DB, error) {
var (
driver = "sqlite3"
config = ":memory:?_foreign_keys=1"
driver = "sqlite3"
config = ":memory:?_foreign_keys=1"
maxConnections = 0
)
if os.Getenv("DRONE_DATABASE_DRIVER") != "" {
driver = os.Getenv("DRONE_DATABASE_DRIVER")
config = os.Getenv("DRONE_DATABASE_DATASOURCE")
maxConnectionsString := os.Getenv("DRONE_DATABASE_MAX_CONNECTIONS")
maxConnections, _ = strconv.Atoi(maxConnectionsString)
}
return db.Connect(driver, config)
return db.Connect(driver, config, maxConnections)
}
// Reset resets the database state.