diff options
author | Corey Hulen <corey@hulen.com> | 2017-07-04 00:01:23 -0700 |
---|---|---|
committer | Christopher Speller <crspeller@gmail.com> | 2017-07-04 00:01:23 -0700 |
commit | 04b00e0933c8623c89d99bf291303b24512cacd8 (patch) | |
tree | 5a573acd1d2dacf62d5ea4ea4c8e9fbe910c60c8 /store/sql_supplier.go | |
parent | 0a3bb8fdb10f2ce72e5e975a35fc7d22637265f9 (diff) | |
download | chat-04b00e0933c8623c89d99bf291303b24512cacd8.tar.gz chat-04b00e0933c8623c89d99bf291303b24512cacd8.tar.bz2 chat-04b00e0933c8623c89d99bf291303b24512cacd8.zip |
Adding back ping retry (#6810)
Diffstat (limited to 'store/sql_supplier.go')
-rw-r--r-- | store/sql_supplier.go | 26 |
1 files changed, 20 insertions, 6 deletions
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) |