提交 ba03c808 authored 作者: mooncake9527's avatar mooncake9527

update

上级 ce21a10a
...@@ -197,7 +197,6 @@ func (m *memoryCache) MGet(ctx context.Context, keys []string) ([]any, error) { ...@@ -197,7 +197,6 @@ func (m *memoryCache) MGet(ctx context.Context, keys []string) ([]any, error) {
} }
logger.Info(fmt.Sprintf("%s mget", logPrefix), logger.Info(fmt.Sprintf("%s mget", logPrefix),
logger.Any("key", xslice.Join(keys, " ")), logger.Any("key", xslice.Join(keys, " ")),
logger.Any("val", rs),
logger.Any("cost", time.Since(st).String()), logger.Any("cost", time.Since(st).String()),
logger.Any("expiration", DefaultNotFoundExpireTime.String()), logger.Any("expiration", DefaultNotFoundExpireTime.String()),
ctxUtil.CtxTraceIDField(ctx)) ctxUtil.CtxTraceIDField(ctx))
......
...@@ -112,10 +112,14 @@ func CtxTraceIDField(c context.Context) zap.Field { ...@@ -112,10 +112,14 @@ func CtxTraceIDField(c context.Context) zap.Field {
return zap.String(ContextTraceIDKey, CtxRequestID(c)) return zap.String(ContextTraceIDKey, CtxRequestID(c))
} }
func NewCtx() context.Context { func NewEmptyCtx() context.Context {
return context.WithValue(context.Background(), ContextTraceIDKey, GenerateTid()) return context.WithValue(context.Background(), ContextTraceIDKey, GenerateTid())
} }
func NewCtx(ctx context.Context) context.Context {
return context.WithValue(context.Background(), ContextTraceIDKey, ctx.Value(ContextTraceIDKey))
}
func GetGinUserID(c *gin.Context) xsf.ID { func GetGinUserID(c *gin.Context) xsf.ID {
return getGinVal[xsf.ID](c, KeyUID) return getGinVal[xsf.ID](c, KeyUID)
} }
......
...@@ -4,11 +4,13 @@ package response ...@@ -4,11 +4,13 @@ package response
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"gitlab.wanzhuangkj.com/tush/xpkg/httpcli/entity"
"net/http" "net/http"
"strconv" "strconv"
"time" "time"
"gitlab.wanzhuangkj.com/tush/xpkg/httpcli/entity"
"gitlab.wanzhuangkj.com/tush/xpkg/sgorm/query"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gitlab.wanzhuangkj.com/tush/xpkg/errcode" "gitlab.wanzhuangkj.com/tush/xpkg/errcode"
...@@ -164,11 +166,25 @@ func Success(c *gin.Context, data ...interface{}) { ...@@ -164,11 +166,25 @@ func Success(c *gin.Context, data ...interface{}) {
// SuccessWithPage return success // SuccessWithPage return success
func SuccessWithPage[T any](c *gin.Context, list []*T, total int64) { func SuccessWithPage[T any](c *gin.Context, list []*T, total int64) {
currPage := 1
pageSize := 10
if req, ok := c.Get("reqbody"); ok {
if pagination, ok := req.(query.IPagination); ok {
currPage = pagination.GetPageIndex()
pageSize = pagination.GetPageSize()
}
}
totalPage := total / int64(pageSize)
if total%int64(pageSize) > 0 {
totalPage = totalPage + 1
}
c.Set(ctxUtils.KeyRspCode, 1) c.Set(ctxUtils.KeyRspCode, 1)
if list == nil { if list == nil {
list = []*T{} list = []*T{}
} }
respJSONWith200(c, errcode.Success.Code(), errcode.Success.Msg(), gin.H{"list": list, "total": total}) respJSONWith200(c, errcode.Success.Code(), errcode.Success.Msg(), gin.H{
"list": list, "totalCount": total, "currPage": currPage, "pageSize": pageSize, "totalPage": totalPage,
})
} }
// SuccessWithList return success // SuccessWithList return success
......
...@@ -66,6 +66,8 @@ var logicMap = map[string]string{ ...@@ -66,6 +66,8 @@ var logicMap = map[string]string{
type IPagination interface { type IPagination interface {
GetOffset() int GetOffset() int
GetLimit() int GetLimit() int
GetPageIndex() int
GetPageSize() int
} }
type Pagination struct { type Pagination struct {
......
...@@ -26,7 +26,15 @@ func (f ID) MarshalJSON() ([]byte, error) { ...@@ -26,7 +26,15 @@ func (f ID) MarshalJSON() ([]byte, error) {
return buff, nil return buff, nil
} }
func (f *ID) UnmarshalParam(src string) error {
return f.unmarshal([]byte(src))
}
func (f *ID) UnmarshalJSON(b []byte) error { func (f *ID) UnmarshalJSON(b []byte) error {
return f.unmarshal(b)
}
func (f *ID) unmarshal(b []byte) error {
if len(b) == 0 { if len(b) == 0 {
return nil return nil
} }
......
...@@ -44,8 +44,12 @@ type WsConnManager struct { ...@@ -44,8 +44,12 @@ type WsConnManager struct {
} }
type Hook interface { type Hook interface {
OnHeartbeatOK(receiverID xsf.ID) error
OnHeartbeatFail(receiverID xsf.ID) error
AfterSend(receiverID xsf.ID, payload *WsMessage) error AfterSend(receiverID xsf.ID, payload *WsMessage) error
AfterAccept(userID xsf.ID) error OnConnNotFound(receiverID xsf.ID, payload *WsMessage) error
OnSendFail(receiverID xsf.ID, payload *WsMessage, err error) error
OnConnAccept(userID xsf.ID) error
AfterUnregister(userID xsf.ID) error AfterUnregister(userID xsf.ID) error
OnReceive(senderID xsf.ID, messageType int, msg []byte) OnReceive(senderID xsf.ID, messageType int, msg []byte)
} }
...@@ -120,7 +124,7 @@ func (m *WsConnManager) accept(c *gin.Context) error { ...@@ -120,7 +124,7 @@ func (m *WsConnManager) accept(c *gin.Context) error {
m.register(client) m.register(client)
for _, h := range m.hooks { for _, h := range m.hooks {
h.AfterAccept(userID) h.OnConnAccept(userID)
} }
return nil return nil
} }
...@@ -135,11 +139,20 @@ func (m *WsConnManager) getConn(userID xsf.ID) (*wsConn, bool) { ...@@ -135,11 +139,20 @@ func (m *WsConnManager) getConn(userID xsf.ID) (*wsConn, bool) {
func (m *WsConnManager) Send(ctx context.Context, receiverID xsf.ID, payload *WsMessage) error { func (m *WsConnManager) Send(ctx context.Context, receiverID xsf.ID, payload *WsMessage) error {
conn, exists := m.getConn(receiverID) conn, exists := m.getConn(receiverID)
if exists { if exists {
err := conn.send(ctx, payload) if err := conn.send(ctx, payload); err != nil {
if err != nil { for _, h := range m.hooks {
go func() {
h.OnSendFail(receiverID, payload, err)
}()
}
return err return err
} }
} else { } else {
for _, h := range m.hooks {
go func() {
h.OnConnNotFound(receiverID, payload)
}()
}
return xerror.Newf("user[id:%d] ws conn not found", receiverID) return xerror.Newf("user[id:%d] ws conn not found", receiverID)
} }
for _, h := range m.hooks { for _, h := range m.hooks {
...@@ -177,10 +190,20 @@ func (c *wsConn) heartBeat() { ...@@ -177,10 +190,20 @@ func (c *wsConn) heartBeat() {
select { select {
case <-ticker.C: case <-ticker.C:
if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil { if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil {
for _, h := range c.manager.hooks {
go func() {
h.OnHeartbeatFail(c.receiverID)
}()
}
c.log.Error("[ws][hearbeat]ping failed", logger.Err(err)) c.log.Error("[ws][hearbeat]ping failed", logger.Err(err))
c.stop <- struct{}{} c.stop <- struct{}{}
return return
} }
for _, h := range c.manager.hooks {
go func() {
h.OnHeartbeatOK(c.receiverID)
}()
}
case <-c.stop: case <-c.stop:
c.log.Info("[ws][hearbeat]stop signal received") c.log.Info("[ws][hearbeat]stop signal received")
return return
...@@ -265,7 +288,8 @@ const ( ...@@ -265,7 +288,8 @@ const (
) )
type WsMessage struct { type WsMessage struct {
Seq string `json:"seq"` Seq string `json:"seq"`
Cmd Cmd `json:"cmd"` Cmd Cmd `json:"cmd"`
Data json.RawMessage `json:"payload"` // 原始JSON,目前是models.ChatMessage ReceiverID xsf.ID `json:"receiverID"`
Data json.RawMessage `json:"payload"` // 原始JSON,目前是models.ChatMessage
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论