forked from mtusi2026/mtusi2026lab2
fix: address lab2 review feedback
This commit is contained in:
+15
-1
@@ -147,7 +147,7 @@ func main() {
|
||||
|
||||
server := &http.Server{
|
||||
Addr: host + ":" + port,
|
||||
Handler: middleware.RequestID(mux),
|
||||
Handler: middleware.RequestID(databaseDeadline(mux)),
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
ReadTimeout: 15 * time.Second,
|
||||
WriteTimeout: 15 * time.Second,
|
||||
@@ -189,3 +189,17 @@ func writeHealthOK(w http.ResponseWriter) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte(`{"status":"ok"}`))
|
||||
}
|
||||
|
||||
// databaseDeadline bounds every API request, including queries on already-open
|
||||
// PostgreSQL connections. The handler maps context/driver errors to HTTP 503.
|
||||
func databaseDeadline(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/health/live" {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
|
||||
defer cancel()
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user