Limit max db connections

While load testing mysql compained about too many connections.
This commit is contained in:
Jannis Mattheis 2018-03-20 19:35:05 +01:00
parent 46f1bc17c1
commit d5d19b55bb
1 changed files with 10 additions and 4 deletions

View File

@ -15,13 +15,19 @@ func New(dialect, connection, defaultUser, defaultPass string, strength int) (*G
if err != nil { if err != nil {
return nil, err return nil, err
} }
// we use the database connection inside the handlers from the http
// We normally don't need that much connections, so we limit them. F.ex. mysql complains about
// "too many connections", while load testing Gotify.
db.DB().SetMaxOpenConns(10)
if dialect == "sqlite3" {
// We use the database connection inside the handlers from the http
// framework, therefore concurrent access occurs. Sqlite cannot handle // framework, therefore concurrent access occurs. Sqlite cannot handle
// concurrent writes, so we limit sqlite to one connection. // concurrent writes, so we limit sqlite to one connection.
// see https://github.com/mattn/go-sqlite3/issues/274 // see https://github.com/mattn/go-sqlite3/issues/274
if dialect == "sqlite3" {
db.DB().SetMaxOpenConns(1) db.DB().SetMaxOpenConns(1)
} }
if !db.HasTable(new(model.User)) && !db.HasTable(new(model.Message)) && if !db.HasTable(new(model.User)) && !db.HasTable(new(model.Message)) &&
!db.HasTable(new(model.Client)) && !db.HasTable(new(model.Application)) { !db.HasTable(new(model.Client)) && !db.HasTable(new(model.Application)) {
db.AutoMigrate(new(model.User), new(model.Application), new(model.Message), new(model.Client)) db.AutoMigrate(new(model.User), new(model.Application), new(model.Message), new(model.Client))