thread-master/apps/backend/internal/logic/ping/ping_logic.go

44 lines
1.1 KiB
Go

package ping
import (
"context"
"net/http"
"time"
"apps/backend/internal/response"
"apps/backend/internal/svc"
"apps/backend/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type PingLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewPingLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PingLogic {
return &PingLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}
func (l *PingLogic) Ping() (resp *types.HealthData, err error) {
if err := redisHealth(l.ctx, l.svcCtx); err != nil {
l.Errorf("ping redis health failed: %v", err)
return nil, err
}
return &types.HealthData{Status: "ok", Service: "haixun-gateway"}, nil
}
func redisHealth(ctx context.Context, svcCtx *svc.ServiceContext) error {
if svcCtx == nil || svcCtx.Redis == nil {
return response.Biz(http.StatusServiceUnavailable, 503001, "redis unavailable")
}
pingCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
if !svcCtx.Redis.PingCtx(pingCtx) {
return response.Biz(http.StatusServiceUnavailable, 503001, "redis unavailable")
}
return nil
}