mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-21 05:43:09 -06:00
Fix database lock errors in User API in SQLite mode
This commit is contained in:
parent
72b3160776
commit
443301135b
|
|
@ -93,15 +93,15 @@ func (s *profilesStatements) selectProfileByLocalpart(
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *profilesStatements) setAvatarURL(
|
func (s *profilesStatements) setAvatarURL(
|
||||||
ctx context.Context, localpart string, avatarURL string,
|
ctx context.Context, txn *sql.Tx, localpart string, avatarURL string,
|
||||||
) (err error) {
|
) (err error) {
|
||||||
_, err = s.setAvatarURLStmt.ExecContext(ctx, avatarURL, localpart)
|
_, err = txn.Stmt(s.setAvatarURLStmt).ExecContext(ctx, avatarURL, localpart)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *profilesStatements) setDisplayName(
|
func (s *profilesStatements) setDisplayName(
|
||||||
ctx context.Context, localpart string, displayName string,
|
ctx context.Context, txn *sql.Tx, localpart string, displayName string,
|
||||||
) (err error) {
|
) (err error) {
|
||||||
_, err = s.setDisplayNameStmt.ExecContext(ctx, displayName, localpart)
|
_, err = txn.Stmt(s.setDisplayNameStmt).ExecContext(ctx, displayName, localpart)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
||||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||||
|
|
@ -40,7 +39,9 @@ type Database struct {
|
||||||
threepids threepidStatements
|
threepids threepidStatements
|
||||||
serverName gomatrixserverlib.ServerName
|
serverName gomatrixserverlib.ServerName
|
||||||
|
|
||||||
createAccountMu sync.Mutex
|
accountWriter *sqlutil.TransactionWriter
|
||||||
|
profileWriter *sqlutil.TransactionWriter
|
||||||
|
threepidWriter *sqlutil.TransactionWriter
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDatabase creates a new accounts and profiles database
|
// NewDatabase creates a new accounts and profiles database
|
||||||
|
|
@ -74,7 +75,18 @@ func NewDatabase(dataSourceName string, serverName gomatrixserverlib.ServerName)
|
||||||
if err = t.prepare(db); err != nil {
|
if err = t.prepare(db); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &Database{db, partitions, a, p, ac, t, serverName, sync.Mutex{}}, nil
|
return &Database{
|
||||||
|
db: db,
|
||||||
|
PartitionOffsetStatements: partitions,
|
||||||
|
accounts: a,
|
||||||
|
profiles: p,
|
||||||
|
accountDatas: ac,
|
||||||
|
threepids: t,
|
||||||
|
serverName: serverName,
|
||||||
|
accountWriter: sqlutil.NewTransactionWriter(),
|
||||||
|
profileWriter: sqlutil.NewTransactionWriter(),
|
||||||
|
threepidWriter: sqlutil.NewTransactionWriter(),
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAccountByPassword returns the account associated with the given localpart and password.
|
// GetAccountByPassword returns the account associated with the given localpart and password.
|
||||||
|
|
@ -105,7 +117,9 @@ func (d *Database) GetProfileByLocalpart(
|
||||||
func (d *Database) SetAvatarURL(
|
func (d *Database) SetAvatarURL(
|
||||||
ctx context.Context, localpart string, avatarURL string,
|
ctx context.Context, localpart string, avatarURL string,
|
||||||
) error {
|
) error {
|
||||||
return d.profiles.setAvatarURL(ctx, localpart, avatarURL)
|
return d.profileWriter.Do(d.db, func(txn *sql.Tx) error {
|
||||||
|
return d.profiles.setAvatarURL(ctx, txn, localpart, avatarURL)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetDisplayName updates the display name of the profile associated with the given
|
// SetDisplayName updates the display name of the profile associated with the given
|
||||||
|
|
@ -113,7 +127,9 @@ func (d *Database) SetAvatarURL(
|
||||||
func (d *Database) SetDisplayName(
|
func (d *Database) SetDisplayName(
|
||||||
ctx context.Context, localpart string, displayName string,
|
ctx context.Context, localpart string, displayName string,
|
||||||
) error {
|
) error {
|
||||||
return d.profiles.setDisplayName(ctx, localpart, displayName)
|
return d.profileWriter.Do(d.db, func(txn *sql.Tx) error {
|
||||||
|
return d.profiles.setDisplayName(ctx, txn, localpart, displayName)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateGuestAccount makes a new guest account and creates an empty profile
|
// CreateGuestAccount makes a new guest account and creates an empty profile
|
||||||
|
|
@ -124,9 +140,7 @@ func (d *Database) CreateGuestAccount(ctx context.Context) (acc *api.Account, er
|
||||||
// when the first txn upgrades to a write txn. We also need to lock the account creation else we can
|
// when the first txn upgrades to a write txn. We also need to lock the account creation else we can
|
||||||
// race with CreateAccount
|
// race with CreateAccount
|
||||||
// We know we'll be the only process since this is sqlite ;) so a lock here will be all that is needed.
|
// We know we'll be the only process since this is sqlite ;) so a lock here will be all that is needed.
|
||||||
d.createAccountMu.Lock()
|
err = d.accountWriter.Do(d.db, func(txn *sql.Tx) error {
|
||||||
defer d.createAccountMu.Unlock()
|
|
||||||
err = sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error {
|
|
||||||
var numLocalpart int64
|
var numLocalpart int64
|
||||||
numLocalpart, err = d.accounts.selectNewNumericLocalpart(ctx, txn)
|
numLocalpart, err = d.accounts.selectNewNumericLocalpart(ctx, txn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -146,9 +160,7 @@ func (d *Database) CreateAccount(
|
||||||
ctx context.Context, localpart, plaintextPassword, appserviceID string,
|
ctx context.Context, localpart, plaintextPassword, appserviceID string,
|
||||||
) (acc *api.Account, err error) {
|
) (acc *api.Account, err error) {
|
||||||
// Create one account at a time else we can get 'database is locked'.
|
// Create one account at a time else we can get 'database is locked'.
|
||||||
d.createAccountMu.Lock()
|
err = d.accountWriter.Do(d.db, func(txn *sql.Tx) error {
|
||||||
defer d.createAccountMu.Unlock()
|
|
||||||
err = sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error {
|
|
||||||
acc, err = d.createAccount(ctx, txn, localpart, plaintextPassword, appserviceID)
|
acc, err = d.createAccount(ctx, txn, localpart, plaintextPassword, appserviceID)
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
|
|
@ -196,7 +208,7 @@ func (d *Database) createAccount(
|
||||||
func (d *Database) SaveAccountData(
|
func (d *Database) SaveAccountData(
|
||||||
ctx context.Context, localpart, roomID, dataType string, content json.RawMessage,
|
ctx context.Context, localpart, roomID, dataType string, content json.RawMessage,
|
||||||
) error {
|
) error {
|
||||||
return sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error {
|
return d.accountWriter.Do(d.db, func(txn *sql.Tx) error {
|
||||||
return d.accountDatas.insertAccountData(ctx, txn, localpart, roomID, dataType, content)
|
return d.accountDatas.insertAccountData(ctx, txn, localpart, roomID, dataType, content)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -247,7 +259,7 @@ var Err3PIDInUse = errors.New("This third-party identifier is already in use")
|
||||||
func (d *Database) SaveThreePIDAssociation(
|
func (d *Database) SaveThreePIDAssociation(
|
||||||
ctx context.Context, threepid, localpart, medium string,
|
ctx context.Context, threepid, localpart, medium string,
|
||||||
) (err error) {
|
) (err error) {
|
||||||
return sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error {
|
return d.threepidWriter.Do(d.db, func(txn *sql.Tx) error {
|
||||||
user, err := d.threepids.selectLocalpartForThreePID(
|
user, err := d.threepids.selectLocalpartForThreePID(
|
||||||
ctx, txn, threepid, medium,
|
ctx, txn, threepid, medium,
|
||||||
)
|
)
|
||||||
|
|
@ -270,7 +282,9 @@ func (d *Database) SaveThreePIDAssociation(
|
||||||
func (d *Database) RemoveThreePIDAssociation(
|
func (d *Database) RemoveThreePIDAssociation(
|
||||||
ctx context.Context, threepid string, medium string,
|
ctx context.Context, threepid string, medium string,
|
||||||
) (err error) {
|
) (err error) {
|
||||||
return d.threepids.deleteThreePID(ctx, threepid, medium)
|
return d.threepidWriter.Do(d.db, func(txn *sql.Tx) error {
|
||||||
|
return d.threepids.deleteThreePID(ctx, txn, threepid, medium)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLocalpartForThreePID looks up the localpart associated with a given third-party
|
// GetLocalpartForThreePID looks up the localpart associated with a given third-party
|
||||||
|
|
|
||||||
|
|
@ -124,7 +124,8 @@ func (s *threepidStatements) insertThreePID(
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *threepidStatements) deleteThreePID(
|
func (s *threepidStatements) deleteThreePID(
|
||||||
ctx context.Context, threepid string, medium string) (err error) {
|
ctx context.Context, txn *sql.Tx, threepid string, medium string) (err error) {
|
||||||
_, err = s.deleteThreePIDStmt.ExecContext(ctx, threepid, medium)
|
stmt := sqlutil.TxStmt(txn, s.deleteThreePIDStmt)
|
||||||
|
_, err = stmt.ExecContext(ctx, threepid, medium)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue