fix: address lab2 review feedback

This commit is contained in:
Fedoroff
2026-10-27 20:03:08 +03:00
parent c251f2cb7d
commit f08011385a
11 changed files with 280 additions and 82 deletions
+15 -1
View File
@@ -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))
})
}