mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-12-05 12:48:07 -06:00
[performance] retry db queries on busy errors (#2025)
* catch SQLITE_BUSY errors, wrap bun.DB to use our own busy retrier, remove unnecessary db.Error type Signed-off-by: kim <grufwub@gmail.com> * remove dead code Signed-off-by: kim <grufwub@gmail.com> * remove more dead code, add missing error arguments Signed-off-by: kim <grufwub@gmail.com> * update sqlite to use maxOpenConns() Signed-off-by: kim <grufwub@gmail.com> * add uncommitted changes Signed-off-by: kim <grufwub@gmail.com> * use direct calls-through for the ConnIface to make sure we don't double query hook Signed-off-by: kim <grufwub@gmail.com> * expose underlying bun.DB better Signed-off-by: kim <grufwub@gmail.com> * retry on the correct busy error Signed-off-by: kim <grufwub@gmail.com> * use longer possible maxRetries for db retry-backoff Signed-off-by: kim <grufwub@gmail.com> * remove the note regarding max-open-conns only applying to postgres Signed-off-by: kim <grufwub@gmail.com> * improved code commenting Signed-off-by: kim <grufwub@gmail.com> * remove unnecessary infof call (just use info) Signed-off-by: kim <grufwub@gmail.com> * rename DBConn to WrappedDB to better follow sql package name conventions Signed-off-by: kim <grufwub@gmail.com> * update test error string checks Signed-off-by: kim <grufwub@gmail.com> * shush linter Signed-off-by: kim <grufwub@gmail.com> * update backoff logic to be more transparent Signed-off-by: kim <grufwub@gmail.com> --------- Signed-off-by: kim <grufwub@gmail.com>
This commit is contained in:
parent
9eff0d46e4
commit
5f3e095717
53 changed files with 1050 additions and 898 deletions
|
|
@ -32,16 +32,16 @@ import (
|
|||
)
|
||||
|
||||
type statusFaveDB struct {
|
||||
conn *DBConn
|
||||
db *WrappedDB
|
||||
state *state.State
|
||||
}
|
||||
|
||||
func (s *statusFaveDB) GetStatusFave(ctx context.Context, accountID string, statusID string) (*gtsmodel.StatusFave, db.Error) {
|
||||
func (s *statusFaveDB) GetStatusFave(ctx context.Context, accountID string, statusID string) (*gtsmodel.StatusFave, error) {
|
||||
return s.getStatusFave(
|
||||
ctx,
|
||||
"AccountID.StatusID",
|
||||
func(fave *gtsmodel.StatusFave) error {
|
||||
return s.conn.
|
||||
return s.db.
|
||||
NewSelect().
|
||||
Model(fave).
|
||||
Where("? = ?", bun.Ident("account_id"), accountID).
|
||||
|
|
@ -53,12 +53,12 @@ func (s *statusFaveDB) GetStatusFave(ctx context.Context, accountID string, stat
|
|||
)
|
||||
}
|
||||
|
||||
func (s *statusFaveDB) GetStatusFaveByID(ctx context.Context, id string) (*gtsmodel.StatusFave, db.Error) {
|
||||
func (s *statusFaveDB) GetStatusFaveByID(ctx context.Context, id string) (*gtsmodel.StatusFave, error) {
|
||||
return s.getStatusFave(
|
||||
ctx,
|
||||
"ID",
|
||||
func(fave *gtsmodel.StatusFave) error {
|
||||
return s.conn.
|
||||
return s.db.
|
||||
NewSelect().
|
||||
Model(fave).
|
||||
Where("? = ?", bun.Ident("id"), id).
|
||||
|
|
@ -75,7 +75,7 @@ func (s *statusFaveDB) getStatusFave(ctx context.Context, lookup string, dbQuery
|
|||
|
||||
// Not cached! Perform database query.
|
||||
if err := dbQuery(&fave); err != nil {
|
||||
return nil, s.conn.ProcessError(err)
|
||||
return nil, s.db.ProcessError(err)
|
||||
}
|
||||
|
||||
return &fave, nil
|
||||
|
|
@ -119,16 +119,16 @@ func (s *statusFaveDB) getStatusFave(ctx context.Context, lookup string, dbQuery
|
|||
return fave, nil
|
||||
}
|
||||
|
||||
func (s *statusFaveDB) GetStatusFavesForStatus(ctx context.Context, statusID string) ([]*gtsmodel.StatusFave, db.Error) {
|
||||
func (s *statusFaveDB) GetStatusFavesForStatus(ctx context.Context, statusID string) ([]*gtsmodel.StatusFave, error) {
|
||||
ids := []string{}
|
||||
|
||||
if err := s.conn.
|
||||
if err := s.db.
|
||||
NewSelect().
|
||||
Table("status_faves").
|
||||
Column("id").
|
||||
Where("? = ?", bun.Ident("status_id"), statusID).
|
||||
Scan(ctx, &ids); err != nil {
|
||||
return nil, s.conn.ProcessError(err)
|
||||
return nil, s.db.ProcessError(err)
|
||||
}
|
||||
|
||||
faves := make([]*gtsmodel.StatusFave, 0, len(ids))
|
||||
|
|
@ -188,17 +188,17 @@ func (s *statusFaveDB) PopulateStatusFave(ctx context.Context, statusFave *gtsmo
|
|||
return errs.Combine()
|
||||
}
|
||||
|
||||
func (s *statusFaveDB) PutStatusFave(ctx context.Context, fave *gtsmodel.StatusFave) db.Error {
|
||||
func (s *statusFaveDB) PutStatusFave(ctx context.Context, fave *gtsmodel.StatusFave) error {
|
||||
return s.state.Caches.GTS.StatusFave().Store(fave, func() error {
|
||||
_, err := s.conn.
|
||||
_, err := s.db.
|
||||
NewInsert().
|
||||
Model(fave).
|
||||
Exec(ctx)
|
||||
return s.conn.ProcessError(err)
|
||||
return s.db.ProcessError(err)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *statusFaveDB) DeleteStatusFaveByID(ctx context.Context, id string) db.Error {
|
||||
func (s *statusFaveDB) DeleteStatusFaveByID(ctx context.Context, id string) error {
|
||||
defer s.state.Caches.GTS.StatusFave().Invalidate("ID", id)
|
||||
|
||||
// Load fave into cache before attempting a delete,
|
||||
|
|
@ -214,21 +214,21 @@ func (s *statusFaveDB) DeleteStatusFaveByID(ctx context.Context, id string) db.E
|
|||
}
|
||||
|
||||
// Finally delete fave from DB.
|
||||
_, err = s.conn.NewDelete().
|
||||
_, err = s.db.NewDelete().
|
||||
Table("status_faves").
|
||||
Where("? = ?", bun.Ident("id"), id).
|
||||
Exec(ctx)
|
||||
return s.conn.ProcessError(err)
|
||||
return s.db.ProcessError(err)
|
||||
}
|
||||
|
||||
func (s *statusFaveDB) DeleteStatusFaves(ctx context.Context, targetAccountID string, originAccountID string) db.Error {
|
||||
func (s *statusFaveDB) DeleteStatusFaves(ctx context.Context, targetAccountID string, originAccountID string) error {
|
||||
if targetAccountID == "" && originAccountID == "" {
|
||||
return errors.New("DeleteStatusFaves: one of targetAccountID or originAccountID must be set")
|
||||
}
|
||||
|
||||
var faveIDs []string
|
||||
|
||||
q := s.conn.
|
||||
q := s.db.
|
||||
NewSelect().
|
||||
Column("id").
|
||||
Table("status_faves")
|
||||
|
|
@ -242,7 +242,7 @@ func (s *statusFaveDB) DeleteStatusFaves(ctx context.Context, targetAccountID st
|
|||
}
|
||||
|
||||
if _, err := q.Exec(ctx, &faveIDs); err != nil {
|
||||
return s.conn.ProcessError(err)
|
||||
return s.db.ProcessError(err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
|
|
@ -263,24 +263,24 @@ func (s *statusFaveDB) DeleteStatusFaves(ctx context.Context, targetAccountID st
|
|||
}
|
||||
|
||||
// Finally delete all from DB.
|
||||
_, err := s.conn.NewDelete().
|
||||
_, err := s.db.NewDelete().
|
||||
Table("status_faves").
|
||||
Where("? IN (?)", bun.Ident("id"), bun.In(faveIDs)).
|
||||
Exec(ctx)
|
||||
return s.conn.ProcessError(err)
|
||||
return s.db.ProcessError(err)
|
||||
}
|
||||
|
||||
func (s *statusFaveDB) DeleteStatusFavesForStatus(ctx context.Context, statusID string) db.Error {
|
||||
func (s *statusFaveDB) DeleteStatusFavesForStatus(ctx context.Context, statusID string) error {
|
||||
// Capture fave IDs in a RETURNING statement.
|
||||
var faveIDs []string
|
||||
|
||||
q := s.conn.
|
||||
q := s.db.
|
||||
NewSelect().
|
||||
Column("id").
|
||||
Table("status_faves").
|
||||
Where("? = ?", bun.Ident("status_id"), statusID)
|
||||
if _, err := q.Exec(ctx, &faveIDs); err != nil {
|
||||
return s.conn.ProcessError(err)
|
||||
return s.db.ProcessError(err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
|
|
@ -301,9 +301,9 @@ func (s *statusFaveDB) DeleteStatusFavesForStatus(ctx context.Context, statusID
|
|||
}
|
||||
|
||||
// Finally delete all from DB.
|
||||
_, err := s.conn.NewDelete().
|
||||
_, err := s.db.NewDelete().
|
||||
Table("status_faves").
|
||||
Where("? IN (?)", bun.Ident("id"), bun.In(faveIDs)).
|
||||
Exec(ctx)
|
||||
return s.conn.ProcessError(err)
|
||||
return s.db.ProcessError(err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue