mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-12-30 00:21:16 +00:00
[core] Add connection tuning for MySQL databases
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
41582eabd0
commit
0dc648d02a
1 changed files with 26 additions and 0 deletions
|
@ -4,6 +4,7 @@ import (
|
|||
"database/sql"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
@ -16,6 +17,11 @@ import (
|
|||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
const (
|
||||
mysqlMaxIdleConnections = 2 // Default as of Go 1.20
|
||||
mysqlMaxOpenConnections = 10
|
||||
)
|
||||
|
||||
type (
|
||||
connector struct {
|
||||
db *gorm.DB
|
||||
|
@ -38,6 +44,7 @@ func New(driverName, connString, encryptionSecret string) (Connector, error) {
|
|||
case "mysql":
|
||||
mysqlDriver.SetLogger(newLogrusLogWriterWithLevel(logrus.ErrorLevel, driverName))
|
||||
innerDB = mysql.Open(connString)
|
||||
dbTuner = tuneMySQLDatabase
|
||||
|
||||
case "postgres":
|
||||
innerDB = postgres.Open(connString)
|
||||
|
@ -106,6 +113,25 @@ func patchSQLiteConnString(connString string) (string, error) {
|
|||
return u.String(), nil
|
||||
}
|
||||
|
||||
func tuneMySQLDatabase(db *sql.DB, err error) error {
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "getting database")
|
||||
}
|
||||
|
||||
// By default the package allows unlimited connections and the
|
||||
// default value of a MySQL / MariaDB server is to allow 151
|
||||
// connections at most. Therefore we tune the connection pool to
|
||||
// sane values in order not to flood the database with connections
|
||||
// in case a lot of events occur at the same time.
|
||||
|
||||
db.SetConnMaxIdleTime(time.Hour)
|
||||
db.SetConnMaxLifetime(time.Hour)
|
||||
db.SetMaxIdleConns(mysqlMaxIdleConnections)
|
||||
db.SetMaxOpenConns(mysqlMaxOpenConnections)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func tuneSQLiteDatabase(db *sql.DB, err error) error {
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "getting database")
|
||||
|
|
Loading…
Reference in a new issue