Use PingContext instead of Ping

This commit is contained in:
Till Faelligen 2020-10-08 18:44:19 +02:00
parent 3243a42a49
commit 010511be13

View file

@ -15,6 +15,7 @@
package httputil package httputil
import ( import (
"context"
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"net/http" "net/http"
@ -42,12 +43,12 @@ func HealthCheckHandler(dbConfig ...config.DatabaseOptions) http.HandlerFunc {
conns[i] = c conns[i] = c
} }
return func(rw http.ResponseWriter, _ *http.Request) { return func(rw http.ResponseWriter, req *http.Request) {
resp := &healthResponse{ resp := &healthResponse{
Code: http.StatusOK, Code: http.StatusOK,
FirstError: "", FirstError: "",
} }
err := dbPingCheck(conns, resp) err := dbPingCheck(conns, resp, req.Context())
if err != nil { if err != nil {
rw.WriteHeader(resp.Code) rw.WriteHeader(resp.Code)
} }
@ -58,10 +59,10 @@ func HealthCheckHandler(dbConfig ...config.DatabaseOptions) http.HandlerFunc {
} }
} }
func dbPingCheck(conns []*sql.DB, resp *healthResponse) error { func dbPingCheck(conns []*sql.DB, resp *healthResponse, ctx context.Context) error {
// check every database connection // check every database connection
for _, conn := range conns { for _, conn := range conns {
if err := conn.Ping(); err != nil { if err := conn.PingContext(ctx); err != nil {
resp.Code = http.StatusInternalServerError resp.Code = http.StatusInternalServerError
resp.FirstError = err.Error() resp.FirstError = err.Error()
return err return err