Skip to content

Commit

Permalink
Make DB connection max lifetime configurable.
Browse files Browse the repository at this point in the history
  • Loading branch information
rgooch committed Mar 10, 2021
1 parent fdcdac6 commit e5106dd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/keymasterd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ type OpenIDConnectIDPConfig struct {

type ProfileStorageConfig struct {
AwsSecretId string `yaml:"aws_secret_id"`
ConnectionLifetime time.Duration `yaml:"connection_lifetime"`
StorageUrl string `yaml:"storage_url"`
SyncDelay time.Duration `yaml:"sync_delay"`
SyncInterval time.Duration `yaml:"sync_interval"`
Expand Down
13 changes: 12 additions & 1 deletion cmd/keymasterd/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const (
profileDBFilename = "userProfiles.sqlite3"
cachedDBFilename = "cachedDB.sqlite3"

dbConnectionLifetimeDefault = time.Minute * 15
dbConnectionLifetimeMaximum = time.Hour

dbSyncDelayDefault = time.Second * 3
dbSyncDelayMinimum = time.Second
dbSyncDelayMaximum = time.Minute
Expand All @@ -47,6 +50,14 @@ func (config *ProfileStorageConfig) setSyncLimits() {
} else if config.SyncInterval > dbSyncIntervalMaximum {
config.SyncInterval = dbSyncIntervalMaximum
}
if config.ConnectionLifetime < 1 {
config.ConnectionLifetime = dbConnectionLifetimeDefault
}
if config.ConnectionLifetime < config.SyncInterval {
config.ConnectionLifetime = config.SyncInterval
} else if config.ConnectionLifetime > dbConnectionLifetimeMaximum {
config.ConnectionLifetime = dbConnectionLifetimeMaximum
}
}

func (state *RuntimeState) expandStorageUrl() error {
Expand Down Expand Up @@ -130,7 +141,7 @@ func initDBPostgres(state *RuntimeState) (err error) {
}
}
// Ensure that broken connections are replaced.
state.db.SetConnMaxLifetime(state.Config.ProfileStorage.SyncInterval >> 1)
state.db.SetConnMaxLifetime(state.Config.ProfileStorage.ConnectionLifetime)
return nil
}

Expand Down

0 comments on commit e5106dd

Please sign in to comment.