fix(health): readiness pings postgres and redis instead of returning 200
This commit is contained in:
@@ -49,7 +49,7 @@ func main() {
|
||||
|
||||
store := postgres.New(pool)
|
||||
|
||||
sessionRepo, closeSession := newSessionRepo(cfg, store)
|
||||
sessionRepo, cachePing, closeSession := newSessionRepo(cfg, store)
|
||||
defer closeSession()
|
||||
|
||||
hasher := service.NewArgon2idHasher()
|
||||
@@ -59,9 +59,16 @@ func main() {
|
||||
chatSvc := service.NewChatService(store.Chats(), store.Users())
|
||||
messageSvc := service.NewMessageService(store.Messages(), store.Chats())
|
||||
|
||||
healthChecks := []handler.HealthCheck{
|
||||
func(ctx context.Context) error { return pool.Ping(ctx) },
|
||||
}
|
||||
if cachePing != nil {
|
||||
healthChecks = append(healthChecks, cachePing)
|
||||
}
|
||||
|
||||
router := httpapi.NewRouter(
|
||||
httpapi.Handlers{
|
||||
Health: handler.NewHealthHandler(),
|
||||
Health: handler.NewHealthHandler(healthChecks...),
|
||||
User: handler.NewUserHandler(userSvc),
|
||||
Chat: handler.NewChatHandler(chatSvc),
|
||||
Message: handler.NewMessageHandler(messageSvc),
|
||||
@@ -92,10 +99,10 @@ func main() {
|
||||
log.Println("Сервер остановлен")
|
||||
}
|
||||
|
||||
func newSessionRepo(cfg *config.Config, store *postgres.Storage) (domain.SessionRepository, func()) {
|
||||
func newSessionRepo(cfg *config.Config, store *postgres.Storage) (domain.SessionRepository, handler.HealthCheck, func()) {
|
||||
switch strings.ToLower(cfg.SessionStore) {
|
||||
case "", "postgres":
|
||||
return store.Sessions(), func() {}
|
||||
return store.Sessions(), nil, func() {}
|
||||
case "redis", "valkey":
|
||||
if cfg.CacheURL == "" {
|
||||
log.Fatalf("SESSION_STORE=%s требует CACHE_URL_FILE или CACHE_URL", cfg.SessionStore)
|
||||
@@ -104,9 +111,12 @@ func newSessionRepo(cfg *config.Config, store *postgres.Storage) (domain.Session
|
||||
if err != nil {
|
||||
log.Fatalf("redis: %v", err)
|
||||
}
|
||||
return redisstore.NewSessionRepo(client), func() { _ = client.Close() }
|
||||
ping := func(ctx context.Context) error {
|
||||
return client.Ping(ctx).Err()
|
||||
}
|
||||
return redisstore.NewSessionRepo(client), ping, func() { _ = client.Close() }
|
||||
default:
|
||||
log.Fatalf("SESSION_STORE=%q не поддерживается", cfg.SessionStore)
|
||||
return nil, nil
|
||||
return nil, nil, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,24 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
"high-loud-apps-labs/internal/httpapi/dto"
|
||||
)
|
||||
|
||||
type HealthHandler struct{}
|
||||
type HealthCheck func(ctx context.Context) error
|
||||
|
||||
func NewHealthHandler() *HealthHandler {
|
||||
return &HealthHandler{}
|
||||
type HealthHandler struct {
|
||||
checks []HealthCheck
|
||||
}
|
||||
|
||||
func NewHealthHandler(checks ...HealthCheck) *HealthHandler {
|
||||
return &HealthHandler{checks: checks}
|
||||
}
|
||||
|
||||
func (h *HealthHandler) Live(c *gin.Context) {
|
||||
@@ -19,5 +26,17 @@ func (h *HealthHandler) Live(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (h *HealthHandler) Ready(c *gin.Context) {
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second)
|
||||
defer cancel()
|
||||
|
||||
for _, check := range h.checks {
|
||||
if check == nil {
|
||||
continue
|
||||
}
|
||||
if err := check(ctx); err != nil {
|
||||
respondError(c, &domain.UnavailableError{Reason: "зависимость недоступна"})
|
||||
return
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, dto.HealthResponse{Status: "ok"})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user