Merge pull request #487 from ajgreenb/db-connection-backoff

Use a backoff approach when connecting to database
This commit is contained in:
Jimmy Zelinskie 2017-12-13 11:37:54 -05:00 committed by GitHub
commit 838dcb0872
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -61,6 +61,8 @@ import (
_ "github.com/coreos/clair/ext/vulnsrc/ubuntu"
)
const maxDBConnectionAttempts = 20
func waitForSignals(signals ...os.Signal) {
interrupts := make(chan os.Signal, 1)
signal.Notify(interrupts, signals...)
@ -132,10 +134,20 @@ func Boot(config *Config) {
st := stopper.NewStopper()
// Open database
db, err := database.Open(config.Database)
if err != nil {
log.Fatal(err)
var db database.Datastore
var dbError error
for attempts := 1; attempts <= maxDBConnectionAttempts; attempts++ {
db, dbError = database.Open(config.Database)
if dbError == nil {
break
}
log.WithError(dbError).Error("failed to connect to database")
time.Sleep(time.Duration(attempts) * time.Second)
}
if dbError != nil {
log.Fatal(dbError)
}
defer db.Close()
// Start notifier