Use a nicer pattern for wrapping transactions

This commit is contained in:
Mark Haines 2017-08-21 14:02:35 +01:00
parent 901fcec288
commit 6d69180fc9
3 changed files with 77 additions and 72 deletions

View file

@ -18,6 +18,24 @@ import (
"database/sql"
)
// A Transaction is something that can be committed or rolledback.
type Transaction interface {
// Commit the transaction
Commit() error
// Rollback the transaction.
Rollback() error
}
// EndTransaction ends a transaction.
// If the transaction succeeded then it is committed, otherwise it is rolledback.
func EndTransaction(txn Transaction, succeeded *bool) {
if *succeeded {
txn.Commit()
} else {
txn.Rollback()
}
}
// WithTransaction runs a block of code passing in an SQL transaction
// If the code returns an error or panics then the transactions is rolledback
// Otherwise the transaction is committed.
@ -26,16 +44,14 @@ func WithTransaction(db *sql.DB, fn func(txn *sql.Tx) error) (err error) {
if err != nil {
return
}
defer func() {
if r := recover(); r != nil {
txn.Rollback()
panic(r)
} else if err != nil {
txn.Rollback()
} else {
err = txn.Commit()
}
}()
succeeded := false
defer EndTransaction(txn, &succeeded)
err = fn(txn)
if err != nil {
return
}
succeeded = true
return
}

View file

@ -17,6 +17,7 @@ package input
import (
"fmt"
"github.com/matrix-org/dendrite/common"
"github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/dendrite/roomserver/state"
"github.com/matrix-org/dendrite/roomserver/types"
@ -122,8 +123,8 @@ func processInviteEvent(db RoomEventDatabase, ow OutputRoomEventWriter, input ap
if err != nil {
return err
}
return withTransaction(updater, func() error {
succeeded := false
defer common.EndTransaction(updater, &succeeded)
if updater.IsJoin() {
// If the user is joined to the room then that takes precedence over this
@ -161,30 +162,10 @@ func processInviteEvent(db RoomEventDatabase, ow OutputRoomEventWriter, input ap
return err
}
return ow.WriteOutputEvents(roomID, outputUpdates)
})
if err = ow.WriteOutputEvents(roomID, outputUpdates); err != nil {
return err
}
// withTransaction runs a function inside a transaction.
// Commits the transaction if the function succeeds.
// Rollsback the transaction if the function fails or panics.
func withTransaction(t types.Transaction, f func() error) (err error) {
defer func() {
if r := recover(); r != nil {
t.Rollback()
panic(r)
}
if err != nil {
// Ignore any error we get rolling back since we don't want to
// clobber the current error
// TODO: log the error here.
t.Rollback()
} else {
// Commit if there wasn't an error.
// Set the returned err value if we encounter an error committing.
// This only works because err is a named return.
err = t.Commit()
}
}()
return f()
succeeded = true
return nil
}

View file

@ -17,6 +17,7 @@ package input
import (
"bytes"
"github.com/matrix-org/dendrite/common"
"github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/dendrite/roomserver/state"
"github.com/matrix-org/dendrite/roomserver/types"
@ -52,12 +53,19 @@ func updateLatestEvents(
if err != nil {
return
}
succeeded := false
defer common.EndTransaction(updater, &succeeded)
u := latestEventsUpdater{
db: db, updater: updater, ow: ow, roomNID: roomNID,
stateAtEvent: stateAtEvent, event: event, sendAsServer: sendAsServer,
}
return withTransaction(updater, u.doUpdateLatestEvents)
if err = u.doUpdateLatestEvents(); err != nil {
return err
}
succeeded = true
return
}
// latestEventsUpdater tracks the state used to update the latest events in the