From 04b00e0933c8623c89d99bf291303b24512cacd8 Mon Sep 17 00:00:00 2001 From: Corey Hulen Date: Tue, 4 Jul 2017 00:01:23 -0700 Subject: Adding back ping retry (#6810) --- store/sql_supplier.go | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'store') diff --git a/store/sql_supplier.go b/store/sql_supplier.go index c062d0ff0..6f51cbd09 100644 --- a/store/sql_supplier.go +++ b/store/sql_supplier.go @@ -4,6 +4,7 @@ package store import ( + "context" dbsql "database/sql" "encoding/json" "errors" @@ -24,6 +25,8 @@ const ( INDEX_TYPE_FULL_TEXT = "full_text" INDEX_TYPE_DEFAULT = "default" MAX_DB_CONN_LIFETIME = 60 + DB_PING_ATTEMPTS = 18 + DB_PING_TIMEOUT_SECS = 10 ) const ( @@ -162,12 +165,23 @@ func setupConnection(con_type string, driver string, dataSource string, maxIdle os.Exit(EXIT_DB_OPEN) } - l4g.Info(utils.T("store.sql.pinging.info"), con_type) - err = db.Ping() - if err != nil { - l4g.Critical(utils.T("store.sql.ping.critical"), err) - time.Sleep(time.Second) - os.Exit(EXIT_PING) + for i := 0; i < DB_PING_ATTEMPTS; i++ { + l4g.Info("Pinging SQL %v database", con_type) + ctx, cancel := context.WithTimeout(context.Background(), DB_PING_TIMEOUT_SECS*time.Second) + defer cancel() + err = db.PingContext(ctx) + if err == nil { + break + } else { + if i == DB_PING_ATTEMPTS-1 { + l4g.Critical("Failed to ping DB, server will exit err=%v", err) + time.Sleep(time.Second) + os.Exit(EXIT_PING) + } else { + l4g.Error("Failed to ping DB retrying in %v seconds err=%v", DB_PING_TIMEOUT_SECS, err) + time.Sleep(DB_PING_TIMEOUT_SECS * time.Second) + } + } } db.SetMaxIdleConns(maxIdle) -- cgit v1.2.3-1-g7c22