mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-11 16:13:10 -06:00
SQLite has rather unfortunate defaults for concurrent accesses, leading to database locked errors. https://github.com/matrix-org/dendrite/pull/1290 appears to try to fix some of this, but doesn't appear to cover a concurrent read+write and the errors still happened in large quantities when I joined the dendrite room. This PR enables WAL for concurrent read+write and much better performance. It also sets busy_timeout to 10 seconds to not immediately fail operations while the database is locked, providing a DB level solution to PR 1290's objective with additional coverage. I chose this particular location to add the two statements as they should apply to every SQLite DB connection. From a quick test it looks as if the db locked errors are gone with this change.
55 lines
1.7 KiB
Go
55 lines
1.7 KiB
Go
package sqlutil
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"regexp"
|
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// Open opens a database specified by its database driver name and a driver-specific data source name,
|
|
// usually consisting of at least a database name and connection information. Includes tracing driver
|
|
// if DENDRITE_TRACE_SQL=1
|
|
func Open(dbProperties *config.DatabaseOptions, writer Writer) (*sql.DB, error) {
|
|
var err error
|
|
var driverName, dsn string
|
|
switch {
|
|
case dbProperties.ConnectionString.IsSQLite():
|
|
driverName = "sqlite3"
|
|
dsn, err = ParseFileURI(dbProperties.ConnectionString)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ParseFileURI: %w", err)
|
|
}
|
|
case dbProperties.ConnectionString.IsPostgres():
|
|
driverName = "postgres"
|
|
dsn = string(dbProperties.ConnectionString)
|
|
default:
|
|
return nil, fmt.Errorf("invalid database connection string %q", dbProperties.ConnectionString)
|
|
}
|
|
if tracingEnabled {
|
|
// install the wrapped driver
|
|
driverName += "-trace"
|
|
}
|
|
db, err := sql.Open(driverName, dsn)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if driverName != "sqlite3" {
|
|
logrus.WithFields(logrus.Fields{
|
|
"MaxOpenConns": dbProperties.MaxOpenConns(),
|
|
"MaxIdleConns": dbProperties.MaxIdleConns(),
|
|
"ConnMaxLifetime": dbProperties.ConnMaxLifetime(),
|
|
"dataSourceName": regexp.MustCompile(`://[^@]*@`).ReplaceAllLiteralString(dsn, "://"),
|
|
}).Debug("Setting DB connection limits")
|
|
db.SetMaxOpenConns(dbProperties.MaxOpenConns())
|
|
db.SetMaxIdleConns(dbProperties.MaxIdleConns())
|
|
db.SetConnMaxLifetime(dbProperties.ConnMaxLifetime())
|
|
} else {
|
|
db.Exec("PRAGMA busy_timeout = 10000;")
|
|
db.Exec("PRAGMA journal_mode=WAL;")
|
|
}
|
|
return db, nil
|
|
}
|