mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 04:14:39 +03:00
[Files Refactor] Performance tuning (#2813)
* Do database txn in same thread. Retry on locked db * Remove captions from slimscenedata * Fix tracing * Use where in instead of individual selects * Remove scenes_query view * Remove image query view * Remove gallery query view * Use where in for FindMany * Don't interrupt scanning zip files * Fix image filesize sort
This commit is contained in:
@@ -1,12 +1,17 @@
|
||||
package txn
|
||||
|
||||
import "context"
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Manager interface {
|
||||
Begin(ctx context.Context) (context.Context, error)
|
||||
Commit(ctx context.Context) error
|
||||
Rollback(ctx context.Context) error
|
||||
|
||||
IsLocked(err error) bool
|
||||
|
||||
AddPostCommitHook(ctx context.Context, hook TxnFunc)
|
||||
AddPostRollbackHook(ctx context.Context, hook TxnFunc)
|
||||
}
|
||||
@@ -58,3 +63,33 @@ func WithDatabase(ctx context.Context, p DatabaseProvider, fn TxnFunc) error {
|
||||
|
||||
return fn(ctx)
|
||||
}
|
||||
|
||||
type Retryer struct {
|
||||
Manager Manager
|
||||
Retries int
|
||||
OnFail func(ctx context.Context, err error, attempt int) error
|
||||
}
|
||||
|
||||
func (r Retryer) WithTxn(ctx context.Context, fn TxnFunc) error {
|
||||
var attempt int
|
||||
var err error
|
||||
for attempt = 1; attempt <= r.Retries; attempt++ {
|
||||
err = WithTxn(ctx, r.Manager, fn)
|
||||
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !r.Manager.IsLocked(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
if r.OnFail != nil {
|
||||
if err := r.OnFail(ctx, err, attempt); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("failed after %d attempts: %w", attempt, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user