|
| 1 | +package logic |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "net/url" |
| 8 | + |
| 9 | + "github.com/zeromicro/go-zero/core/logx" |
| 10 | + "github.com/zgsm-ai/codebase-indexer/internal/svc" |
| 11 | + "github.com/zgsm-ai/codebase-indexer/internal/types" |
| 12 | +) |
| 13 | + |
| 14 | +type HealthCheckLogic struct { |
| 15 | + logx.Logger |
| 16 | + ctx context.Context |
| 17 | + svcCtx *svc.ServiceContext |
| 18 | +} |
| 19 | + |
| 20 | +func NewHealthCheckLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HealthCheckLogic { |
| 21 | + return &HealthCheckLogic{ |
| 22 | + Logger: logx.WithContext(ctx), |
| 23 | + ctx: ctx, |
| 24 | + svcCtx: svcCtx, |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// CheckHealth 执行探活检查 |
| 29 | +func (l *HealthCheckLogic) CheckHealth(authorization string, req *types.IndexSummaryRequest) error { |
| 30 | + // 如果探活检查未启用,直接返回成功 |
| 31 | + if !l.svcCtx.Config.HealthCheck.Enabled { |
| 32 | + l.Info("health check is disabled, skipping") |
| 33 | + return nil |
| 34 | + } |
| 35 | + |
| 36 | + // 创建HTTP客户端 |
| 37 | + client := &http.Client{ |
| 38 | + Timeout: l.svcCtx.Config.HealthCheck.Timeout, |
| 39 | + } |
| 40 | + |
| 41 | + // 构建请求URL |
| 42 | + healthCheckURL := l.svcCtx.Config.HealthCheck.URL |
| 43 | + |
| 44 | + // 将req参数添加到查询参数中 |
| 45 | + if req != nil { |
| 46 | + // 解析基础URL |
| 47 | + parsedURL, err := url.Parse(healthCheckURL) |
| 48 | + if err != nil { |
| 49 | + l.Errorf("failed to parse health check URL: %v", err) |
| 50 | + return fmt.Errorf("failed to parse health check URL: %w", err) |
| 51 | + } |
| 52 | + |
| 53 | + // 获取现有查询参数或创建新的 |
| 54 | + q := parsedURL.Query() |
| 55 | + |
| 56 | + // 添加req中的参数 |
| 57 | + if req.ClientId != "" { |
| 58 | + q.Set("clientId", req.ClientId) |
| 59 | + } |
| 60 | + if req.CodebasePath != "" { |
| 61 | + q.Set("codebasePath", req.CodebasePath) |
| 62 | + } |
| 63 | + |
| 64 | + // 将查询参数设置回URL |
| 65 | + parsedURL.RawQuery = q.Encode() |
| 66 | + healthCheckURL = parsedURL.String() |
| 67 | + } |
| 68 | + |
| 69 | + // 创建请求 |
| 70 | + httpReq, err := http.NewRequestWithContext(l.ctx, "GET", healthCheckURL, nil) |
| 71 | + if err != nil { |
| 72 | + l.Errorf("failed to create health check request: %v", err) |
| 73 | + return fmt.Errorf("failed to create health check request: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + // 设置请求头 |
| 77 | + httpReq.Header.Set("Authorization", authorization) |
| 78 | + httpReq.Header.Set("X-Costrict-Version", "v1.0.6") |
| 79 | + |
| 80 | + // 重试逻辑 |
| 81 | + var lastErr error |
| 82 | + |
| 83 | + // 发送请求 |
| 84 | + resp, err := client.Do(httpReq) |
| 85 | + if err != nil { |
| 86 | + lastErr = fmt.Errorf("health check request failed: %w", err) |
| 87 | + return lastErr |
| 88 | + } |
| 89 | + |
| 90 | + // 确保响应体被关闭 |
| 91 | + defer resp.Body.Close() |
| 92 | + |
| 93 | + // 检查状态码 |
| 94 | + if resp.StatusCode == http.StatusOK { |
| 95 | + l.Info("health check passed") |
| 96 | + return nil |
| 97 | + } |
| 98 | + |
| 99 | + lastErr = fmt.Errorf("health check returned status code: %d", resp.StatusCode) |
| 100 | + |
| 101 | + return fmt.Errorf("health check failed : %w", lastErr) |
| 102 | +} |
0 commit comments