Files
backend/internal/handler/swagger.go
lan a111872b32
Some checks failed
Build / build (push) Successful in 2m23s
Build / build-docker (push) Failing after 1m37s
feat(auth): upgrade casbin to v3 and enhance connection pool configurations
- Upgrade casbin from v2 to v3 across go.mod and pkg/auth/casbin.go
- Add slide captcha verification to registration flow (CheckVerified, ConsumeVerified)
- Add DB wrapper with connection pool statistics and health checks
- Add Redis connection pool optimizations with stats and health monitoring
- Add new config options: ConnMaxLifetime, HealthCheckInterval, EnableRetryOnError
- Optimize slow query threshold from 200ms to 100ms
- Add ping with retry mechanism for database and Redis connections
2026-02-25 19:00:50 +08:00

96 lines
1.8 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package handler
import (
"context"
"errors"
"net/http"
"time"
"carrotskin/pkg/database"
"carrotskin/pkg/redis"
"github.com/gin-gonic/gin"
)
// HealthCheck 健康检查,检查依赖服务状态
func HealthCheck(c *gin.Context) {
ctx, cancel := context.WithTimeout(c.Request.Context(), 5*time.Second)
defer cancel()
checks := make(map[string]string)
status := "ok"
// 检查数据库
if err := checkDatabase(ctx); err != nil {
checks["database"] = "unhealthy: " + err.Error()
status = "degraded"
} else {
checks["database"] = "healthy"
}
// 检查Redis
if err := checkRedis(ctx); err != nil {
checks["redis"] = "unhealthy: " + err.Error()
status = "degraded"
} else {
checks["redis"] = "healthy"
}
// 根据状态返回相应的HTTP状态码
httpStatus := http.StatusOK
if status == "degraded" {
httpStatus = http.StatusServiceUnavailable
}
c.JSON(httpStatus, gin.H{
"status": status,
"message": "CarrotSkin API health check",
"checks": checks,
"timestamp": time.Now().Unix(),
})
}
// checkDatabase 检查数据库连接
func checkDatabase(ctx context.Context) error {
db, err := database.GetDB()
if err != nil {
return err
}
sqlDB, err := db.DB()
if err != nil {
return err
}
// 使用Ping检查连接
if err := sqlDB.PingContext(ctx); err != nil {
return err
}
// 执行简单查询验证
var result int
if err := db.WithContext(ctx).Raw("SELECT 1").Scan(&result).Error; err != nil {
return err
}
return nil
}
// checkRedis 检查Redis连接
func checkRedis(ctx context.Context) error {
client, err := redis.GetClient()
if err != nil {
return err
}
if client == nil {
return errors.New("Redis客户端未初始化")
}
// 使用Ping检查连接封装后的方法直接返回error
if err := client.Ping(ctx); err != nil {
return err
}
return nil
}