mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-07-30 21:12:45 +00:00
Remove BaseDendrite
(#3023)
Removes `BaseDendrite` to, hopefully, make testing and composing of components easier in the future.
This commit is contained in:
parent
ec6879e5ae
commit
5e85a00cb3
68 changed files with 1186 additions and 1002 deletions
|
@ -19,15 +19,21 @@ import (
|
|||
"fmt"
|
||||
|
||||
"github.com/matrix-org/dendrite/setup/config"
|
||||
"github.com/matrix-org/dendrite/setup/process"
|
||||
)
|
||||
|
||||
type Connections struct {
|
||||
db *sql.DB
|
||||
writer Writer
|
||||
db *sql.DB
|
||||
writer Writer
|
||||
globalConfig config.DatabaseOptions
|
||||
processContext *process.ProcessContext
|
||||
}
|
||||
|
||||
func NewConnectionManager() Connections {
|
||||
return Connections{}
|
||||
func NewConnectionManager(processCtx *process.ProcessContext, globalConfig config.DatabaseOptions) Connections {
|
||||
return Connections{
|
||||
globalConfig: globalConfig,
|
||||
processContext: processCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Connections) Connection(dbProperties *config.DatabaseOptions) (*sql.DB, Writer, error) {
|
||||
|
@ -35,14 +41,29 @@ func (c *Connections) Connection(dbProperties *config.DatabaseOptions) (*sql.DB,
|
|||
if dbProperties.ConnectionString.IsSQLite() {
|
||||
writer = NewExclusiveWriter()
|
||||
}
|
||||
var err error
|
||||
if dbProperties.ConnectionString == "" {
|
||||
// if no connectionString was provided, try the global one
|
||||
dbProperties = &c.globalConfig
|
||||
}
|
||||
if dbProperties.ConnectionString != "" || c.db == nil {
|
||||
var err error
|
||||
// Open a new database connection using the supplied config.
|
||||
c.db, err = Open(dbProperties, writer)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
c.writer = writer
|
||||
go func() {
|
||||
if c.processContext == nil {
|
||||
return
|
||||
}
|
||||
// If we have a ProcessContext, start a component and wait for
|
||||
// Dendrite to shut down to cleanly close the database connection.
|
||||
c.processContext.ComponentStarted()
|
||||
<-c.processContext.WaitForShutdown()
|
||||
_ = c.db.Close()
|
||||
c.processContext.ComponentFinished()
|
||||
}()
|
||||
return c.db, c.writer, nil
|
||||
}
|
||||
if c.db != nil && c.writer != nil {
|
||||
|
|
|
@ -13,9 +13,9 @@ func TestConnectionManager(t *testing.T) {
|
|||
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
||||
conStr, close := test.PrepareDBConnectionString(t, dbType)
|
||||
t.Cleanup(close)
|
||||
cm := sqlutil.NewConnectionManager()
|
||||
cm := sqlutil.NewConnectionManager(nil, config.DatabaseOptions{})
|
||||
|
||||
dbProps := &config.DatabaseOptions{ConnectionString: config.DataSource(string(conStr))}
|
||||
dbProps := &config.DatabaseOptions{ConnectionString: config.DataSource(conStr)}
|
||||
db, writer, err := cm.Connection(dbProps)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
@ -47,8 +47,8 @@ func TestConnectionManager(t *testing.T) {
|
|||
}
|
||||
|
||||
// test invalid connection string configured
|
||||
cm = sqlutil.NewConnectionManager()
|
||||
_, _, err = cm.Connection(&config.DatabaseOptions{ConnectionString: "http://"})
|
||||
cm2 := sqlutil.NewConnectionManager(nil, config.DatabaseOptions{})
|
||||
_, _, err = cm2.Connection(&config.DatabaseOptions{ConnectionString: "http://"})
|
||||
if err == nil {
|
||||
t.Fatal("expected an error but got none")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue