lab2: fix review — cache consistency, readiness, timeouts, uuid validation, tuple index

This commit is contained in:
2026-10-10 01:14:56 +03:00
parent a327909856
commit 40f260612f
21 changed files with 185 additions and 189 deletions
+26 -4
View File
@@ -1,22 +1,44 @@
package http
import (
"context"
"net/http"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/redis/go-redis/v9"
"github.com/charawein/mtusi2026lab2/internal/service"
"github.com/charawein/mtusi2026lab2/internal/transport/httputil"
)
type HealthHandler struct{}
func NewHealthHandler() *HealthHandler {
return &HealthHandler{}
type HealthHandler struct {
pool *pgxpool.Pool
redis *redis.Client
}
func NewHealthHandler(pool *pgxpool.Pool, redisClient *redis.Client) *HealthHandler {
return &HealthHandler{pool: pool, redis: redisClient}
}
// Live проверяет только жизнь процесса.
func (h *HealthHandler) Live(w http.ResponseWriter, r *http.Request) {
httputil.WriteJSON(w, r, http.StatusOK, healthResponse{Status: "ok"})
}
// Ready проверяет доступность PostgreSQL и Redis.
func (h *HealthHandler) Ready(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second)
defer cancel()
if err := h.pool.Ping(ctx); err != nil {
httputil.WriteError(w, r, service.ErrUnavailable)
return
}
if err := h.redis.Ping(ctx).Err(); err != nil {
httputil.WriteError(w, r, service.ErrUnavailable)
return
}
httputil.WriteJSON(w, r, http.StatusOK, healthResponse{Status: "ok"})
}