Skip to content

Commit bf46364

Browse files
Merge pull request #328 from gabriel-samfira/add-busy-timeout
Add knob to tweak _busy_timeout
2 parents 09e3df2 + 6814b69 commit bf46364

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
@@ -548,7 +548,8 @@ func (d *Database) Validate() error {
548548

549549
// SQLite is the config entry for the sqlite3 section
550550
type SQLite struct {
551-
DBFile string `toml:"db_file" json:"db-file"`
551+
DBFile string `toml:"db_file" json:"db-file"`
552+
BusyTimeoutSeconds int `toml:"busy_timeout_seconds" json:"busy-timeout-seconds"`
552553
}
553554

554555
func (s *SQLite) Validate() error {
@@ -568,7 +569,12 @@ func (s *SQLite) Validate() error {
568569
}
569570

570571
func (s *SQLite) ConnectionString() (string, error) {
571-
return fmt.Sprintf("%s?_journal_mode=WAL&_foreign_keys=ON", s.DBFile), nil
572+
connectionString := fmt.Sprintf("%s?_journal_mode=WAL&_foreign_keys=ON", s.DBFile)
573+
if s.BusyTimeoutSeconds > 0 {
574+
timeout := s.BusyTimeoutSeconds * 1000
575+
connectionString = fmt.Sprintf("%s&_busy_timeout=%d", connectionString, timeout)
576+
}
577+
return connectionString, nil
572578
}
573579

574580
// 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)