From 802a13cffdac406468f03ee703ea9c02f23974c2 Mon Sep 17 00:00:00 2001 From: konrad Date: Sat, 25 May 2019 05:49:52 +0000 Subject: [PATCH] Added settings for max open/idle connections and max connection lifetime (#74) --- Featurecreep.md | 1 + config.yml.sample | 6 +++++- docs/content/doc/setup/config.md | 6 +++++- pkg/config/config.go | 5 ++++- pkg/db/db.go | 13 ++++++++++++- 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/Featurecreep.md b/Featurecreep.md index 872950dbc8e..95e952e1253 100644 --- a/Featurecreep.md +++ b/Featurecreep.md @@ -213,6 +213,7 @@ Sorry for some of them being in German, I'll tranlate them at some point. * [x] Re-check all `{List|Namespace}{User|Team}` if really all parameters need to be exposed via json or are overwritten via param anyway. * [x] Things like list/task order should use queries and not url params * [x] Fix lint errors +* [x] Add settings for max open/idle connections and max connection lifetime * [ ] Reminders should use an extra table so we can make reverse lookups aka "give me all tasks with reminders in this period" which we'll need for things like email reminders notifications * [ ] Teams and users should also have uuids (for users these can be the username) * [ ] When giving a team or user access to a list/namespace, they should be reffered to by uuid, not numeric id diff --git a/config.yml.sample b/config.yml.sample index a796bce8ac3..b768881d25c 100644 --- a/config.yml.sample +++ b/config.yml.sample @@ -33,7 +33,11 @@ database: # When using sqlite, this is the path where to store the data Path: "./vikunja.db" # Sets the max open connections to the database. Only used when using mysql. - openconnections: 100 + maxopenconnections: 100 + # Sets the maximum number of idle connections to the db. + maxidleconnections: 50 + # The maximum lifetime of a single db connection in miliseconds. + maxconnectionlifetime: 10000 cache: # If cache is enabled or not diff --git a/docs/content/doc/setup/config.md b/docs/content/doc/setup/config.md index 03a9a5c1986..64eb74426c6 100644 --- a/docs/content/doc/setup/config.md +++ b/docs/content/doc/setup/config.md @@ -76,7 +76,11 @@ database: # When using sqlite, this is the path where to store the data Path: "./vikunja.db" # Sets the max open connections to the database. Only used when using mysql. - openconnections: 100 + maxopenconnections: 100 + # Sets the maximum number of idle connections to the db. + maxidleconnections: 50 + # The maximum lifetime of a single db connection in miliseconds. + maxconnectionlifetime: 10000 cache: # If cache is enabled or not diff --git a/pkg/config/config.go b/pkg/config/config.go index 30902384804..f17ca1326b0 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -57,7 +57,10 @@ func InitConfig() { viper.SetDefault("database.password", "") viper.SetDefault("database.database", "vikunja") viper.SetDefault("database.path", "./vikunja.db") - viper.SetDefault("database.openconnections", 100) + viper.SetDefault("database.maxopenconnections", 100) + viper.SetDefault("database.maxidleconnections", 50) + viper.SetDefault("database.maxconnectionlifetime", 10000) + // Cacher viper.SetDefault("cache.enabled", false) viper.SetDefault("cache.type", "memory") diff --git a/pkg/db/db.go b/pkg/db/db.go index 69acda4ae96..fd8d124f3cb 100644 --- a/pkg/db/db.go +++ b/pkg/db/db.go @@ -23,6 +23,8 @@ import ( "github.com/go-xorm/core" "github.com/go-xorm/xorm" "github.com/spf13/viper" + "strconv" + "time" _ "github.com/go-sql-driver/mysql" // Because. _ "github.com/mattn/go-sqlite3" // Because. @@ -64,7 +66,16 @@ func initMysqlEngine() (engine *xorm.Engine, err error) { viper.GetString("database.host"), viper.GetString("database.database")) engine, err = xorm.NewEngine("mysql", connStr) - engine.SetMaxOpenConns(viper.GetInt("database.openconnections")) + if err != nil { + return + } + engine.SetMaxOpenConns(viper.GetInt("database.maxopenconnections")) + engine.SetMaxIdleConns(viper.GetInt("database.maxidleconnections")) + max, err := time.ParseDuration(strconv.Itoa(viper.GetInt("database.maxconnectionlifetime")) + `ms`) + if err != nil { + return + } + engine.SetConnMaxLifetime(max) return }