aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorNeil Alexander <neilalexander@users.noreply.github.com>2022-05-03 16:35:06 +0100
committerGitHub <noreply@github.com>2022-05-03 16:35:06 +0100
commit4ad5f9c982fe5dc9e306a9269621ead8c31248cf (patch)
tree9eac975c1d7232b35ce4d0c7f658db3c2289f0ab /internal
parent979a551f1e2aeb9f3417df5e52a7279230b7a3ba (diff)
Global database connection pool (for monolith mode) (#2411)
* Allow monolith components to share a single database pool * Don't yell about missing connection strings * Rename field * Setup tweaks * Fix panic * Improve configuration checks * Update config * Fix lint errors * Update comments
Diffstat (limited to 'internal')
-rw-r--r--internal/sqlutil/sqlutil.go51
-rw-r--r--internal/sqlutil/trace.go44
2 files changed, 51 insertions, 44 deletions
diff --git a/internal/sqlutil/sqlutil.go b/internal/sqlutil/sqlutil.go
new file mode 100644
index 00000000..0cdae6d3
--- /dev/null
+++ b/internal/sqlutil/sqlutil.go
@@ -0,0 +1,51 @@
+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())
+ }
+ return db, nil
+}
diff --git a/internal/sqlutil/trace.go b/internal/sqlutil/trace.go
index 51eaa1b4..c1673861 100644
--- a/internal/sqlutil/trace.go
+++ b/internal/sqlutil/trace.go
@@ -16,19 +16,16 @@ package sqlutil
import (
"context"
- "database/sql"
"database/sql/driver"
"fmt"
"io"
"os"
- "regexp"
"runtime"
"strconv"
"strings"
"sync"
"time"
- "github.com/matrix-org/dendrite/setup/config"
"github.com/ngrok/sqlmw"
"github.com/sirupsen/logrus"
)
@@ -96,47 +93,6 @@ func trackGoID(query string) {
logrus.Warnf("unsafe goid %d: SQL executed not on an ExclusiveWriter: %s", thisGoID, q)
}
-// 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) (*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())
- }
- return db, nil
-}
-
func init() {
registerDrivers()
}