Fix database locked errors (#3153)

* Make read-only operations use WithReadTxn
* Allow one database write thread
* Add unit test for concurrent transactions
* Perform some actions after commit to release txn
* Suppress some errors from cancelled context
This commit is contained in:
WithoutPants
2022-11-21 06:49:10 +11:00
committed by GitHub
parent 420c6fa9d7
commit f39fa416a9
54 changed files with 626 additions and 311 deletions

View File

@@ -47,7 +47,7 @@ func (c Cache) postScrape(ctx context.Context, content ScrapedContent) (ScrapedC
}
func (c Cache) postScrapePerformer(ctx context.Context, p models.ScrapedPerformer) (ScrapedContent, error) {
if err := txn.WithTxn(ctx, c.txnManager, func(ctx context.Context) error {
if err := txn.WithReadTxn(ctx, c.txnManager, func(ctx context.Context) error {
tqb := c.repository.TagFinder
tags, err := postProcessTags(ctx, tqb, p.Tags)
@@ -73,7 +73,7 @@ func (c Cache) postScrapePerformer(ctx context.Context, p models.ScrapedPerforme
func (c Cache) postScrapeMovie(ctx context.Context, m models.ScrapedMovie) (ScrapedContent, error) {
if m.Studio != nil {
if err := txn.WithTxn(ctx, c.txnManager, func(ctx context.Context) error {
if err := txn.WithReadTxn(ctx, c.txnManager, func(ctx context.Context) error {
return match.ScrapedStudio(ctx, c.repository.StudioFinder, m.Studio, nil)
}); err != nil {
return nil, err
@@ -106,7 +106,7 @@ func (c Cache) postScrapeScenePerformer(ctx context.Context, p models.ScrapedPer
}
func (c Cache) postScrapeScene(ctx context.Context, scene ScrapedScene) (ScrapedContent, error) {
if err := txn.WithTxn(ctx, c.txnManager, func(ctx context.Context) error {
if err := txn.WithReadTxn(ctx, c.txnManager, func(ctx context.Context) error {
pqb := c.repository.PerformerFinder
mqb := c.repository.MovieFinder
tqb := c.repository.TagFinder
@@ -160,7 +160,7 @@ func (c Cache) postScrapeScene(ctx context.Context, scene ScrapedScene) (Scraped
}
func (c Cache) postScrapeGallery(ctx context.Context, g ScrapedGallery) (ScrapedContent, error) {
if err := txn.WithTxn(ctx, c.txnManager, func(ctx context.Context) error {
if err := txn.WithReadTxn(ctx, c.txnManager, func(ctx context.Context) error {
pqb := c.repository.PerformerFinder
tqb := c.repository.TagFinder
sqb := c.repository.StudioFinder