From cc2ec22e1bb4a4c37f7661b452e71f5d6cfe340d Mon Sep 17 00:00:00 2001 From: Nate Maninger Date: Thu, 28 Dec 2023 17:29:35 -0800 Subject: [PATCH] sqlite: check text instead of error code --- persist/sqlite/store.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/persist/sqlite/store.go b/persist/sqlite/store.go index bc9cfea2..0ee81000 100644 --- a/persist/sqlite/store.go +++ b/persist/sqlite/store.go @@ -9,7 +9,6 @@ import ( "strings" "time" - "github.com/mattn/go-sqlite3" "go.uber.org/zap" "lukechampine.com/frand" ) @@ -86,24 +85,24 @@ func (s *Store) transaction(fn func(txn) error) error { txnID := hex.EncodeToString(frand.Bytes(4)) log := s.log.Named("transaction").With(zap.String("id", txnID)) start := time.Now() - for i := 1; i <= retryAttempts; i++ { + attempt := 1 + for ; attempt <= retryAttempts; attempt++ { attemptStart := time.Now() - log := log.With(zap.Int("attempt", i)) + log := log.With(zap.Int("attempt", attempt)) err = doTransaction(s.db, log, fn) if err == nil { // no error, break out of the loop return nil } - // check if the error is not a busy error - var sqliteErr sqlite3.Error - if !errors.As(err, &sqliteErr) || sqliteErr.Code != sqlite3.ErrBusy { - return err + // return immediately if the error is not a busy error + if !strings.Contains(err.Error(), "database is locked") { + break } log.Debug("database locked", zap.Duration("elapsed", time.Since(attemptStart)), zap.Duration("totalElapsed", time.Since(start)), zap.Stack("stack")) - jitterSleep(time.Duration(math.Pow(factor, float64(i))) * time.Millisecond) // exponential backoff + jitterSleep(time.Duration(math.Pow(factor, float64(attempt))) * time.Millisecond) // exponential backoff } - return fmt.Errorf("transaction failed: %w", err) + return fmt.Errorf("transaction failed (%d): %w", attempt, err) } // Close closes the underlying database.