Skip to content

Commit

Permalink
sqlite: add backoff consts
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Aug 1, 2023
1 parent ccba68a commit 9f46cbb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
6 changes: 5 additions & 1 deletion persist/sqlite/consts_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

package sqlite

const busyTimeout = 5000 // 5 seconds
const (
busyTimeout = 5000 // 5 seconds
retryAttempts = 15 // 15 attempts
factor = 2.0 // factor ^ retryAttempts = backoff time in milliseconds
)
6 changes: 5 additions & 1 deletion persist/sqlite/consts_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

package sqlite

const busyTimeout = 100 // 100ms
const (
busyTimeout = 5 // 5ms
retryAttempts = 10 // 10 attempts
factor = 2.0 // factor ^ retryAttempts = backoff time in milliseconds
)
6 changes: 3 additions & 3 deletions persist/sqlite/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (s *Store) queryRow(query string, args ...any) *loggedRow {
func (s *Store) transaction(fn func(txn) error) error {
var err error
log := s.log.Named("transaction")
for i := 1; i <= 15; i++ {
for i := 1; i <= retryAttempts; i++ {
start := time.Now()
log := log.With(zap.Int("attempt", i))
err = doTransaction(s.db, log, fn)
Expand All @@ -98,8 +98,8 @@ func (s *Store) transaction(fn func(txn) error) error {
return err
}
log.Debug("database locked", zap.Duration("elapsed", time.Since(start)), zap.Stack("stack"))
sleep := time.Duration(math.Pow(2, float64(i))) * time.Millisecond // exponential backoff for a total of ~30s
time.Sleep(sleep + time.Duration(rand.Int63n(int64(sleep)/10)))
sleep := time.Duration(math.Pow(factor, float64(i))) * time.Millisecond // exponential backoff
time.Sleep(sleep + time.Duration(rand.Int63n(int64(sleep)/10))) // add random jitter
}
return fmt.Errorf("transaction failed: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion persist/sqlite/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestTransactionRetry(t *testing.T) {
return err
}
ch <- struct{}{}
time.Sleep(10 * time.Second)
time.Sleep(2 * time.Second)
return nil
})
if err != nil {
Expand Down

0 comments on commit 9f46cbb

Please sign in to comment.