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

update

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