Add busy timeout

This commit is contained in:
Ben Johnson
2021-01-16 07:33:32 -07:00
parent 1b8cfc8a41
commit cbc2dce6dc

8
db.go
View File

@@ -33,6 +33,9 @@ const (
DefaultMaxCheckpointPageN = 10000 DefaultMaxCheckpointPageN = 10000
) )
// BusyTimeout is the timeout to wait for EBUSY from SQLite.
const BusyTimeout = 1 * time.Second
// DB represents a managed instance of a SQLite database in the file system. // DB represents a managed instance of a SQLite database in the file system.
type DB struct { type DB struct {
mu sync.RWMutex mu sync.RWMutex
@@ -357,8 +360,11 @@ func (db *DB) init() (err error) {
} }
db.uid, db.gid = fileinfo(fi) db.uid, db.gid = fileinfo(fi)
dsn := db.path
dsn += fmt.Sprintf("?_busy_timeout=%d", BusyTimeout.Milliseconds())
// Connect to SQLite database & enable WAL. // Connect to SQLite database & enable WAL.
if db.db, err = sql.Open("sqlite3", db.path); err != nil { if db.db, err = sql.Open("sqlite3", dsn); err != nil {
return err return err
} else if _, err := db.db.Exec(`PRAGMA journal_mode = wal;`); err != nil { } else if _, err := db.db.Exec(`PRAGMA journal_mode = wal;`); err != nil {
return fmt.Errorf("enable wal: %w", err) return fmt.Errorf("enable wal: %w", err)