Skip to content

Commit

Permalink
add 300ms timeout to database ping in health check
Browse files Browse the repository at this point in the history
  • Loading branch information
Dylan Bourque committed Feb 28, 2023
1 parent 1a1d953 commit bb9902b
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion internal/server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io/fs"
"net/http"
"time"

gwruntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
Expand Down Expand Up @@ -45,7 +46,9 @@ func handleUX() http.Handler {
// healthy (can connect to the Perseus database) and '500 Internal Server Error' if not
func handleHealthz(db store.Store) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := db.Ping(r.Context()); err != nil {
ctx, cancel := context.WithTimeout(r.Context(), 300*time.Millisecond)
defer cancel()
if err := db.Ping(ctx); err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "a connection to the database is unavailable")
return
Expand Down

0 comments on commit bb9902b

Please sign in to comment.