Skip to content

Commit

Permalink
set defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
vkuznet committed Dec 5, 2024
1 parent 9b29f1d commit e54f579
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions sqldb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sqldb
import (
"database/sql"
"log"
"time"

srvConfig "github.com/CHESSComputing/golib/config"
)
Expand All @@ -23,13 +24,22 @@ func InitDB(dbtype, dburi string) (*sql.DB, error) {
log.Println("DB ping error", dberr)
return nil, dberr
}
db.SetMaxOpenConns(srvConfig.Config.DataBookkeeping.MaxDBConnections)
db.SetMaxIdleConns(srvConfig.Config.DataBookkeeping.MaxIdleConnections)
if srvConfig.Config.DataBookkeeping.MaxDBConnections == 0 {
db.SetMaxOpenConns(100) // Allow up to 100 open connections
} else {
db.SetMaxOpenConns(srvConfig.Config.DataBookkeeping.MaxDBConnections)
}
if srvConfig.Config.DataBookkeeping.MaxIdleConnections == 0 {
db.SetMaxIdleConns(50) // Keep up to 50 idle connections ready
} else {
db.SetMaxIdleConns(srvConfig.Config.DataBookkeeping.MaxIdleConnections)
}
db.SetConnMaxLifetime(5 * time.Minute) // Recycle connections after 5 minutes
// Disables connection pool for sqlite3. This enables some concurrency with sqlite3 databases
// See https://stackoverflow.com/questions/57683132/turning-off-connection-pool-for-go-http-client
// and https://sqlite.org/wal.html
// This only will apply to sqlite3 databases
if dbtype == "sqlite3" {
if dbtype == "sqlite3" || dbtype == "sqlite" {
db.Exec("PRAGMA journal_mode=WAL;")
}
return db, nil
Expand Down

0 comments on commit e54f579

Please sign in to comment.