feat: implement horizontal scaling optimizations for authz service
- Add /health and /ready endpoints for load balancer health checks - Replace in-memory JWT token cache with Redis for multi-replica support - Reduce DB connection pool from 100 to 25 connections per replica - Add distributed rate limiting (100 req/min + 20 burst) using Redis - Implement circuit breakers for DB and Redis to prevent cascading failures This enables the service to scale horizontally with multiple replicas behind a load balancer without exhausting database connections or maintaining separate token caches per instance.
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"authorization/db"
|
||||
"authorization/models"
|
||||
"authorization/redisclient"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// HealthHandler provides a basic liveness check
|
||||
// @Summary Health check endpoint
|
||||
// @Description Returns service health status for load balancer health checks
|
||||
// @Tags health
|
||||
// @Produce json
|
||||
// @Success 200 {object} HealthResponse
|
||||
// @Router /health [get]
|
||||
func HealthHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(models.HealthResponse{
|
||||
Status: "ok",
|
||||
})
|
||||
}
|
||||
|
||||
// ReadyHandler checks if the service is ready to handle requests
|
||||
// @Summary Readiness check endpoint
|
||||
// @Description Returns readiness status including database and Redis connectivity
|
||||
// @Tags health
|
||||
// @Produce json
|
||||
// @Success 200 {object} HealthResponse
|
||||
// @Failure 503 {object} HealthResponse
|
||||
// @Router /ready [get]
|
||||
func ReadyHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
services := make(map[string]string)
|
||||
allHealthy := true
|
||||
|
||||
// Check database
|
||||
if db.DB != nil {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
if err := db.DB.PingContext(ctx); err != nil {
|
||||
services["database"] = "unhealthy"
|
||||
allHealthy = false
|
||||
} else {
|
||||
services["database"] = "healthy"
|
||||
}
|
||||
} else {
|
||||
services["database"] = "not_initialized"
|
||||
allHealthy = false
|
||||
}
|
||||
|
||||
// Check Redis
|
||||
if redisclient.RDB != nil {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
if _, err := redisclient.RDB.Ping(ctx).Result(); err != nil {
|
||||
services["redis"] = "unhealthy"
|
||||
allHealthy = false
|
||||
} else {
|
||||
services["redis"] = "healthy"
|
||||
}
|
||||
} else {
|
||||
services["redis"] = "not_initialized"
|
||||
allHealthy = false
|
||||
}
|
||||
|
||||
status := "ready"
|
||||
statusCode := http.StatusOK
|
||||
if !allHealthy {
|
||||
status = "not_ready"
|
||||
statusCode = http.StatusServiceUnavailable
|
||||
}
|
||||
|
||||
w.WriteHeader(statusCode)
|
||||
json.NewEncoder(w).Encode(models.HealthResponse{
|
||||
Status: status,
|
||||
Services: services,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user