Merge branch 'develop' of https://gitee.com/fusenpack/fusenapi into develop

This commit is contained in:
eson 2023-10-20 13:25:40 +08:00
commit d9d16753c0
9 changed files with 103 additions and 64 deletions

View File

@ -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)

View File

@ -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,

View File

@ -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))

View File

@ -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]
}

View File

@ -5,7 +5,7 @@ import (
"fusenapi/utils/basic"
)
type GetRecommandProductListReq struct {
type GetRecommendProductListReq struct {
Num int64 `form:"num,optional"`
ProductId int64 `form:"product_id"`
}

View File

@ -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)
}
}
}

View File

@ -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
}
}
}
}

View File

@ -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()
}

View File

@ -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"`
}