diff --git a/server/product/internal/handler/getrecommandproductlisthandler.go b/server/product/internal/handler/getrecommendproductlisthandler.go similarity index 71% rename from server/product/internal/handler/getrecommandproductlisthandler.go rename to server/product/internal/handler/getrecommendproductlisthandler.go index 332ece32..cb4218c2 100644 --- a/server/product/internal/handler/getrecommandproductlisthandler.go +++ b/server/product/internal/handler/getrecommendproductlisthandler.go @@ -11,22 +11,22 @@ import ( "fusenapi/server/product/internal/types" ) -func GetRecommandProductListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { +func GetRecommendProductListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - var req types.GetRecommandProductListReq + var req types.GetRecommendProductListReq userinfo, err := basic.RequestParse(w, r, svcCtx, &req) if err != nil { return } // 创建一个业务逻辑层实例 - l := logic.NewGetRecommandProductListLogic(r.Context(), svcCtx) + l := logic.NewGetRecommendProductListLogic(r.Context(), svcCtx) rl := reflect.ValueOf(l) basic.BeforeLogic(w, r, rl) - resp := l.GetRecommandProductList(&req, userinfo) + resp := l.GetRecommendProductList(&req, userinfo) if !basic.AfterLogic(w, r, rl, resp) { basic.NormalAfterLogic(w, r, resp) diff --git a/server/product/internal/handler/routes.go b/server/product/internal/handler/routes.go index c3ab280e..68d289cc 100644 --- a/server/product/internal/handler/routes.go +++ b/server/product/internal/handler/routes.go @@ -59,8 +59,8 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { }, { Method: http.MethodGet, - Path: "/api/product/recommand", - Handler: GetRecommandProductListHandler(serverCtx), + Path: "/api/product/recommend", + Handler: GetRecommendProductListHandler(serverCtx), }, { Method: http.MethodGet, diff --git a/server/product/internal/logic/getproductdetaillogic.go b/server/product/internal/logic/getproductdetaillogic.go index 6782038a..9055d1ed 100644 --- a/server/product/internal/logic/getproductdetaillogic.go +++ b/server/product/internal/logic/getproductdetaillogic.go @@ -162,8 +162,8 @@ func (l *GetProductDetailLogic) GetProductDetail(req *types.GetProductDetailReq, //获取默认渲染的尺寸 defaultSize, err := l.getRenderDefaultSize(req.ProductId, req.TemplateTag) if err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get default size") + logx.Error("获取默认尺寸失败:", err.Error()) + //return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get default size") } //整理返回 rspSizeList := make([]types.SizeInfo, 0, len(sizeList)) diff --git a/server/product/internal/logic/getrecommandproductlistlogic.go b/server/product/internal/logic/getrecommendproductlistlogic.go similarity index 95% rename from server/product/internal/logic/getrecommandproductlistlogic.go rename to server/product/internal/logic/getrecommendproductlistlogic.go index 319a9d97..013abab1 100644 --- a/server/product/internal/logic/getrecommandproductlistlogic.go +++ b/server/product/internal/logic/getrecommendproductlistlogic.go @@ -20,21 +20,21 @@ import ( "github.com/zeromicro/go-zero/core/logx" ) -type GetRecommandProductListLogic struct { +type GetRecommendProductListLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } -func NewGetRecommandProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRecommandProductListLogic { - return &GetRecommandProductListLogic{ +func NewGetRecommendProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRecommendProductListLogic { + return &GetRecommendProductListLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } -func (l *GetRecommandProductListLogic) GetRecommandProductList(req *types.GetRecommandProductListReq, userinfo *auth.UserInfo) (resp *basic.Response) { +func (l *GetRecommendProductListLogic) GetRecommendProductList(req *types.GetRecommendProductListReq, userinfo *auth.UserInfo) (resp *basic.Response) { if req.Num > 100 || req.Num < 0 { req.Num = 4 } @@ -60,7 +60,6 @@ func (l *GetRecommandProductListLogic) GetRecommandProductList(req *types.GetRec logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get recommend product list") } - //超过了截取 if len(recommendProductList) > int(req.Num) { recommendProductList = recommendProductList[:req.Num] } diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index 5e8cde0b..4a8f453d 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -5,7 +5,7 @@ import ( "fusenapi/utils/basic" ) -type GetRecommandProductListReq struct { +type GetRecommendProductListReq struct { Num int64 `form:"num,optional"` ProductId int64 `form:"product_id"` } diff --git a/server/websocket/internal/logic/ws_connect_statistics.go b/server/websocket/internal/logic/ws_connect_statistics.go deleted file mode 100644 index 226d1550..00000000 --- a/server/websocket/internal/logic/ws_connect_statistics.go +++ /dev/null @@ -1,46 +0,0 @@ -package logic - -import ( - "context" - "github.com/zeromicro/go-zero/core/logx" -) - -var ( - //当前ws连接数 - currentWebsocketConnectCount = 0 - //添加or减少连接的控制chan - websocketConnectCountCtlChan = make(chan int, 20) -) - -// 累增计数 -func increaseWebsocketConnectCount() { - websocketConnectCountCtlChan <- 1 -} - -// 减少计数 -func decreaseWebsocketConnectCount() { - websocketConnectCountCtlChan <- -1 -} - -// 消费数据 -func ConsumeWebsocketConnectCountCtlChanData(ctx context.Context) { - defer func() { - if err := recover(); err != nil { - logx.Error("ConsumeWebsocketConnectCountCtlChanData panic:", err) - } - }() - go func() { - select { - case <-ctx.Done(): - panic("ConsumeWebsocketConnectCountCtlChanData ctx deadline") - } - }() - var num int - for { - select { - case num = <-websocketConnectCountCtlChan: - currentWebsocketConnectCount += num - //logx.Info("当前websocket连接总数:", currentWebsocketConnectCount) - } - } -} diff --git a/server/websocket/internal/logic/ws_statistics.go b/server/websocket/internal/logic/ws_statistics.go new file mode 100644 index 00000000..88f1e32d --- /dev/null +++ b/server/websocket/internal/logic/ws_statistics.go @@ -0,0 +1,86 @@ +package logic + +import ( + "context" + "github.com/zeromicro/go-zero/core/logx" +) + +// 统计信息 +var ( + //当前ws连接数 + currentWebsocketConnectCount = 0 + //当前合图进行的请求总数 + currentRequestCombineApiCount = 0 + //添加or减少连接的控制chan + websocketStat = make(chan websocketStatItem, 20) +) + +type websocketStatType string + +const ( + TYPE_CONNECT_COUNT websocketStatType = "connect_count" //ws连接数 + TYPE_COMBINE_IMAGE_REQUEST_COUNT websocketStatType = "combine_count" +) + +type websocketStatItem struct { + Type websocketStatType `json:"type"` //类型 + Value int `json:"value"` //数值 +} + +// 累增ws连接数计数 +func increaseWebsocketConnectCount() { + websocketStat <- websocketStatItem{ + Type: TYPE_CONNECT_COUNT, + Value: 1, + } +} + +// 减少ws连接数计数 +func decreaseWebsocketConnectCount() { + websocketStat <- websocketStatItem{ + Type: TYPE_CONNECT_COUNT, + Value: -1, + } +} + +// 累增合图请求数计数 +func increaseCombineRequestCount() { + websocketStat <- websocketStatItem{ + Type: TYPE_COMBINE_IMAGE_REQUEST_COUNT, + Value: 1, + } +} + +// 减少合图请求数计数 +func decreaseCombineRequestCount() { + websocketStat <- websocketStatItem{ + Type: TYPE_COMBINE_IMAGE_REQUEST_COUNT, + Value: -1, + } +} + +// 消费数据 +func ConsumeWebsocketStatData(ctx context.Context) { + defer func() { + if err := recover(); err != nil { + logx.Error("ConsumeWebsocketStatData panic:", err) + } + }() + go func() { + select { + case <-ctx.Done(): + panic("ConsumeWebsocketStatData ctx deadline") + } + }() + for { + select { + case data := <-websocketStat: + switch data.Type { + case TYPE_CONNECT_COUNT: //ws连接计数 + currentWebsocketConnectCount += data.Value + case TYPE_COMBINE_IMAGE_REQUEST_COUNT: //请求算法合图计数 + currentRequestCombineApiCount += data.Value + } + } + } +} diff --git a/server/websocket/websocket.go b/server/websocket/websocket.go index afd6a3b1..34673a22 100644 --- a/server/websocket/websocket.go +++ b/server/websocket/websocket.go @@ -38,7 +38,7 @@ func main() { //消费用户索引创建/删除/发送消息中的任务数据 go logic.ConsumeUserConnPoolCtlChanData(ctx1) //消费连接统计信息 - go logic.ConsumeWebsocketConnectCountCtlChanData(ctx1) + go logic.ConsumeWebsocketStatData(ctx1) fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port) server.Start() } diff --git a/server_api/product.api b/server_api/product.api index 37a929fb..3c84d00c 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -38,8 +38,8 @@ service product { @handler GetRenderSettingByPidHandler get /api/product/get_render_setting_by_pid(GetRenderSettingByPidReq) returns (response); //获取详情页推荐产品列表 - @handler GetRecommandProductListHandler - get /api/product/recommand(GetRecommandProductListReq) returns (response); + @handler GetRecommendProductListHandler + get /api/product/recommend(GetRecommendProductListReq) returns (response); //获取列表页推荐产品列表 @handler HomePageRecommendProductListHandler get /api/product/home_page_recommend(HomePageRecommendProductListReq) returns (response); @@ -49,7 +49,7 @@ service product { } //获取详情页推荐产品列表 -type GetRecommandProductListReq { +type GetRecommendProductListReq { Num int64 `form:"num,optional"` ProductId int64 `form:"product_id"` }