Merge pull request #59 from sgotti/db_retry_on_sqlite_locked_err

db: retry on sqlite locked error
This commit is contained in:
Simone Gotti 2019-07-24 17:00:14 +02:00 committed by GitHub
commit 5a16b72adb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 1 deletions

View File

@ -20,7 +20,7 @@ import (
"regexp"
"time"
_ "github.com/mattn/go-sqlite3"
"github.com/mattn/go-sqlite3"
errors "golang.org/x/xerrors"
)
@ -29,6 +29,8 @@ type Type string
const (
Sqlite3 Type = "sqlite3"
Postgres Type = "postgres"
maxTxRetries = 20
)
type dbData struct {
@ -171,6 +173,25 @@ func (db *DB) NewTx() (*Tx, error) {
}
func (db *DB) Do(f func(tx *Tx) error) error {
retries := 0
for {
err := db.do(f)
if err != nil {
var sqerr sqlite3.Error
if errors.As(err, &sqerr) {
if sqerr.Code == sqlite3.ErrLocked {
retries++
if retries <= maxTxRetries {
continue
}
}
}
}
return err
}
}
func (db *DB) do(f func(tx *Tx) error) error {
tx, err := db.NewTx()
if err != nil {
return err