From 66b116ecb2fddd08d256f2db4e6453e7d836a832 Mon Sep 17 00:00:00 2001 From: Aaron Greenberg
Date: Fri, 10 Nov 2017 16:38:02 -0500 Subject: [PATCH 1/3] Use a linear backoff when connecting to DB Closes #159 --- cmd/clair/main.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/cmd/clair/main.go b/cmd/clair/main.go index 23d33a48..b6fa1c4e 100644 --- a/cmd/clair/main.go +++ b/cmd/clair/main.go @@ -132,10 +132,22 @@ 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 + maxConnectionAttempts := 20 + for attempts := 1; attempts <= maxConnectionAttempts; attempts++ { + db, dbError = database.Open(config.Database) + if dbError == nil { + break + } + log.Error(dbError); + time.Sleep(time.Duration(attempts) * time.Second) } + + if dbError != nil { + log.Fatal(dbError) + } + defer db.Close() // Start notifier From 0df9e7075c5d03bc285ae8036ad71f6f13998555 Mon Sep 17 00:00:00 2001 From: Aaron Greenberg
Date: Tue, 12 Dec 2017 22:32:00 -0500 Subject: [PATCH 2/3] Make max DB connection attempts a constant --- cmd/clair/main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/clair/main.go b/cmd/clair/main.go index b6fa1c4e..a7a72c5b 100644 --- a/cmd/clair/main.go +++ b/cmd/clair/main.go @@ -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...) @@ -134,8 +136,7 @@ func Boot(config *Config) { // Open database var db database.Datastore var dbError error - maxConnectionAttempts := 20 - for attempts := 1; attempts <= maxConnectionAttempts; attempts++ { + for attempts := 1; attempts <= maxDBConnectionAttempts; attempts++ { db, dbError = database.Open(config.Database) if dbError == nil { break @@ -143,7 +144,6 @@ func Boot(config *Config) { log.Error(dbError); time.Sleep(time.Duration(attempts) * time.Second) } - if dbError != nil { log.Fatal(dbError) } From 46bf4c470a32e70b8c310834091c7227e0926c32 Mon Sep 17 00:00:00 2001 From: Aaron Greenberg
Date: Tue, 12 Dec 2017 22:34:11 -0500 Subject: [PATCH 3/3] Fix logging of error when connecting to database --- cmd/clair/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/clair/main.go b/cmd/clair/main.go index a7a72c5b..e802cbbd 100644 --- a/cmd/clair/main.go +++ b/cmd/clair/main.go @@ -141,7 +141,7 @@ func Boot(config *Config) { if dbError == nil { break } - log.Error(dbError); + log.WithError(dbError).Error("failed to connect to database") time.Sleep(time.Duration(attempts) * time.Second) } if dbError != nil {