Skip to content

Commit 6814b69

Browse files
Add knob to tweak _busy_timeout
In SQLite3 we may need to set a busy_timeout in the case of instances with high load. This change adds a knob that allows users to set a timeout if a database is locked for writing by another routine. Signed-off-by: Gabriel Adrian Samfira <[email protected]>
1 parent e0e60d4 commit 6814b69

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

config/config.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,8 @@ func (d *Database) Validate() error {
551551

552552
// SQLite is the config entry for the sqlite3 section
553553
type SQLite struct {
554-
DBFile string `toml:"db_file" json:"db-file"`
554+
DBFile string `toml:"db_file" json:"db-file"`
555+
BusyTimeoutSeconds int `toml:"busy_timeout_seconds" json:"busy-timeout-seconds"`
555556
}
556557

557558
func (s *SQLite) Validate() error {
@@ -571,7 +572,12 @@ func (s *SQLite) Validate() error {
571572
}
572573

573574
func (s *SQLite) ConnectionString() (string, error) {
574-
return fmt.Sprintf("%s?_journal_mode=WAL&_foreign_keys=ON", s.DBFile), nil
575+
connectionString := fmt.Sprintf("%s?_journal_mode=WAL&_foreign_keys=ON", s.DBFile)
576+
if s.BusyTimeoutSeconds > 0 {
577+
timeout := s.BusyTimeoutSeconds * 1000
578+
connectionString = fmt.Sprintf("%s&_busy_timeout=%d", connectionString, timeout)
579+
}
580+
return connectionString, nil
575581
}
576582

577583
// MySQL is the config entry for the mysql section

config/config_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,12 @@ func TestGormParams(t *testing.T) {
389389
require.Equal(t, SQLiteBackend, dbType)
390390
require.Equal(t, filepath.Join(dir, "garm.db?_journal_mode=WAL&_foreign_keys=ON"), uri)
391391

392+
cfg.SQLite.BusyTimeoutSeconds = 5
393+
dbType, uri, err = cfg.GormParams()
394+
require.Nil(t, err)
395+
require.Equal(t, SQLiteBackend, dbType)
396+
require.Equal(t, filepath.Join(dir, "garm.db?_journal_mode=WAL&_foreign_keys=ON&_busy_timeout=5000"), uri)
397+
392398
cfg.DbBackend = MySQLBackend
393399
cfg.MySQL = getMySQLDefaultConfig()
394400
cfg.SQLite = SQLite{}

testdata/config.toml

+5
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ time_to_live = "8760h"
9797
[database.sqlite3]
9898
# Path on disk to the sqlite3 database file.
9999
db_file = "/etc/garm/garm.db"
100+
# busy_timeout_seconds is an optional parameter that will set the
101+
# sqlite3_busy_timeout to the specified value. This is useful when
102+
# GARM may be under heavy load and the database is locked by some
103+
# other go routine. The default value is 0.
104+
busy_timeout_seconds = 5
100105

101106
# Currently, providers are defined statically in the config. This is due to the fact
102107
# that we have not yet added support for storing secrets in something like Barbican

0 commit comments

Comments
 (0)