/
health.go
51 lines (42 loc) · 1.02 KB
/
health.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package api
import (
"database/sql"
"database/sql/driver"
"fmt"
"net/http"
"time"
"golang.org/x/net/context"
"gopkg.in/gin-gonic/gin.v1"
)
func HealthCheckHandler(c *gin.Context) {
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
defer cancel()
mdb := c.MustGet("MDB_DB").(*sql.DB)
// Uncomment once this lib/pq PR is merged
// https://github.com/lib/pq/pull/737
//err := mdb.PingContext(ctx)
err := Ping(ctx, mdb)
if err != nil {
c.JSON(http.StatusFailedDependency, gin.H{
"status": "error",
"error": fmt.Sprintf("MDB ping: %s", err.Error()),
})
return
}
if ctx.Err() == context.DeadlineExceeded {
c.JSON(http.StatusServiceUnavailable, gin.H{
"status": "error",
"error": "timeout",
})
return
}
c.JSON(http.StatusOK, gin.H{"status": "ok"})
}
func Ping(ctx context.Context, db *sql.DB) error {
rows, err := db.QueryContext(ctx, "select 1")
if err != nil {
return driver.ErrBadConn // https://golang.org/pkg/database/sql/driver/#Pinger
}
defer rows.Close()
return nil
}