aboutsummaryrefslogtreecommitdiff
path: root/appservice
diff options
context:
space:
mode:
authorNeil Alexander <neilalexander@users.noreply.github.com>2020-08-21 10:42:08 +0100
committerGitHub <noreply@github.com>2020-08-21 10:42:08 +0100
commit9d53351dc20283103bf2eec6b92831033d06c5a8 (patch)
tree653cf0ddca3f777bcdba188187fb78fe39ae2b02 /appservice
parent5aaf32bbed4d704d5a22ad7dff79f7a68002a213 (diff)
Component-wide TransactionWriters (#1290)
* Offset updates take place using TransactionWriter * Refactor TransactionWriter in current state server * Refactor TransactionWriter in federation sender * Refactor TransactionWriter in key server * Refactor TransactionWriter in media API * Refactor TransactionWriter in server key API * Refactor TransactionWriter in sync API * Refactor TransactionWriter in user API * Fix deadlocking Sync API tests * Un-deadlock device database * Fix appservice API * Rename TransactionWriters to Writers * Move writers up a layer in sync API * Document sqlutil.Writer interface * Add note to Writer documentation
Diffstat (limited to 'appservice')
-rw-r--r--appservice/storage/postgres/storage.go4
-rw-r--r--appservice/storage/sqlite3/appservice_events_table.go6
-rw-r--r--appservice/storage/sqlite3/storage.go8
-rw-r--r--appservice/storage/sqlite3/txn_id_counter_table.go6
4 files changed, 14 insertions, 10 deletions
diff --git a/appservice/storage/postgres/storage.go b/appservice/storage/postgres/storage.go
index 9fda87ae..95215816 100644
--- a/appservice/storage/postgres/storage.go
+++ b/appservice/storage/postgres/storage.go
@@ -32,6 +32,7 @@ type Database struct {
events eventsStatements
txnID txnStatements
db *sql.DB
+ writer sqlutil.Writer
}
// NewDatabase opens a new database
@@ -41,10 +42,11 @@ func NewDatabase(dbProperties *config.DatabaseOptions) (*Database, error) {
if result.db, err = sqlutil.Open(dbProperties); err != nil {
return nil, err
}
+ result.writer = sqlutil.NewDummyWriter()
if err = result.prepare(); err != nil {
return nil, err
}
- if err = result.PartitionOffsetStatements.Prepare(result.db, "appservice"); err != nil {
+ if err = result.PartitionOffsetStatements.Prepare(result.db, result.writer, "appservice"); err != nil {
return nil, err
}
return &result, nil
diff --git a/appservice/storage/sqlite3/appservice_events_table.go b/appservice/storage/sqlite3/appservice_events_table.go
index 5cc07ed3..5dfb72f6 100644
--- a/appservice/storage/sqlite3/appservice_events_table.go
+++ b/appservice/storage/sqlite3/appservice_events_table.go
@@ -67,7 +67,7 @@ const (
type eventsStatements struct {
db *sql.DB
- writer sqlutil.TransactionWriter
+ writer sqlutil.Writer
selectEventsByApplicationServiceIDStmt *sql.Stmt
countEventsByApplicationServiceIDStmt *sql.Stmt
insertEventStmt *sql.Stmt
@@ -75,9 +75,9 @@ type eventsStatements struct {
deleteEventsBeforeAndIncludingIDStmt *sql.Stmt
}
-func (s *eventsStatements) prepare(db *sql.DB) (err error) {
+func (s *eventsStatements) prepare(db *sql.DB, writer sqlutil.Writer) (err error) {
s.db = db
- s.writer = sqlutil.NewTransactionWriter()
+ s.writer = writer
_, err = db.Exec(appserviceEventsSchema)
if err != nil {
return
diff --git a/appservice/storage/sqlite3/storage.go b/appservice/storage/sqlite3/storage.go
index 59af9016..916845ab 100644
--- a/appservice/storage/sqlite3/storage.go
+++ b/appservice/storage/sqlite3/storage.go
@@ -32,6 +32,7 @@ type Database struct {
events eventsStatements
txnID txnStatements
db *sql.DB
+ writer sqlutil.Writer
}
// NewDatabase opens a new database
@@ -41,21 +42,22 @@ func NewDatabase(dbProperties *config.DatabaseOptions) (*Database, error) {
if result.db, err = sqlutil.Open(dbProperties); err != nil {
return nil, err
}
+ result.writer = sqlutil.NewExclusiveWriter()
if err = result.prepare(); err != nil {
return nil, err
}
- if err = result.PartitionOffsetStatements.Prepare(result.db, "appservice"); err != nil {
+ if err = result.PartitionOffsetStatements.Prepare(result.db, result.writer, "appservice"); err != nil {
return nil, err
}
return &result, nil
}
func (d *Database) prepare() error {
- if err := d.events.prepare(d.db); err != nil {
+ if err := d.events.prepare(d.db, d.writer); err != nil {
return err
}
- return d.txnID.prepare(d.db)
+ return d.txnID.prepare(d.db, d.writer)
}
// StoreEvent takes in a gomatrixserverlib.HeaderedEvent and stores it in the database
diff --git a/appservice/storage/sqlite3/txn_id_counter_table.go b/appservice/storage/sqlite3/txn_id_counter_table.go
index 0ae0feee..b2940e35 100644
--- a/appservice/storage/sqlite3/txn_id_counter_table.go
+++ b/appservice/storage/sqlite3/txn_id_counter_table.go
@@ -38,13 +38,13 @@ const selectTxnIDSQL = `
type txnStatements struct {
db *sql.DB
- writer sqlutil.TransactionWriter
+ writer sqlutil.Writer
selectTxnIDStmt *sql.Stmt
}
-func (s *txnStatements) prepare(db *sql.DB) (err error) {
+func (s *txnStatements) prepare(db *sql.DB, writer sqlutil.Writer) (err error) {
s.db = db
- s.writer = sqlutil.NewTransactionWriter()
+ s.writer = writer
_, err = db.Exec(txnIDSchema)
if err != nil {
return