Skip to content

Commit

Permalink
feat: cmd2value add call information (#723)
Browse files Browse the repository at this point in the history
* feat: code adjustment

* feat: Cmd2Value carry caller

* feat: Cmd2Value carry caller

* feat: Cmd2Value carry caller

* feat: Cmd2Value carry caller
  • Loading branch information
withchao authored Sep 27, 2024
1 parent d2f919f commit fa6d38a
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 100 deletions.
2 changes: 1 addition & 1 deletion internal/conversation_msg/conversation_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ func (c *Conversation) batchUpdateMessageList(ctx context.Context, updateMsg map
conversation.LatestMsg = utils.StructToJsonString(latestMsg)

c.doUpdateConversation(common.Cmd2Value{Value: common.UpdateConNode{ConID: conversation.ConversationID,
Action: constant.AddConOrUpLatMsg, Args: *conversation, Caller: "batchUpdateMessageList"}})
Action: constant.AddConOrUpLatMsg, Args: *conversation}})

}
}
Expand Down
16 changes: 12 additions & 4 deletions internal/conversation_msg/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ const (
const InitSyncProgress = 10

func (c *Conversation) Work(c2v common.Cmd2Value) {
log.ZDebug(c2v.Ctx, "NotificationCmd start", "cmd", c2v.Cmd, "value", c2v.Value)
defer log.ZDebug(c2v.Ctx, "NotificationCmd end", "cmd", c2v.Cmd, "value", c2v.Value)
log.ZDebug(c2v.Ctx, "NotificationCmd start", "caller", c2v.Caller, "cmd", c2v.Cmd, "value", c2v.Value)
defer log.ZDebug(c2v.Ctx, "NotificationCmd end", "caller", c2v.Caller, "cmd", c2v.Cmd, "value", c2v.Value)
switch c2v.Cmd {
case constant.CmdNewMsgCome:
c.doMsgNew(c2v)
Expand Down Expand Up @@ -191,6 +191,9 @@ func (c *Conversation) getConversationLatestMsgClientID(latestMsg string) string
}

func (c *Conversation) doUpdateConversation(c2v common.Cmd2Value) {
if c2v.Caller == "" {
c2v.Caller = common.GetCaller(2)
}
ctx := c2v.Ctx
node := c2v.Value.(common.UpdateConNode)
log.ZInfo(ctx, "doUpdateConversation", "node", node)
Expand All @@ -209,7 +212,9 @@ func (c *Conversation) doUpdateConversation(c2v common.Cmd2Value) {
oc.LatestMsgSendTime = lc.LatestMsgSendTime
oc.LatestMsg = lc.LatestMsg
list = append(list, oc)
c.ConversationListener().OnConversationChanged(utils.StructToJsonString(list))
data := utils.StructToJsonString(list)
log.ZInfo(ctx, "OnConversationChanged", "data", data)
c.ConversationListener().OnConversationChanged(data)
}
}
} else {
Expand Down Expand Up @@ -295,7 +300,9 @@ func (c *Conversation) doUpdateConversation(c2v common.Cmd2Value) {
newCList = append(newCList, v)
}
}
c.ConversationListener().OnConversationChanged(utils.StructToJsonStringDefault(newCList))
data := utils.StructToJsonStringDefault(newCList)
log.ZInfo(ctx, "OnConversationChanged", "data", data)
c.ConversationListener().OnConversationChanged(data)
}
case constant.NewCon:
cidList := node.Args.([]string)
Expand All @@ -310,6 +317,7 @@ func (c *Conversation) doUpdateConversation(c2v common.Cmd2Value) {
}
case constant.ConChangeDirect:
cidList := node.Args.(string)
log.ZInfo(ctx, "ConversationChanged", "cidList", cidList)
c.ConversationListener().OnConversationChanged(cidList)

case constant.NewConDirect:
Expand Down
2 changes: 1 addition & 1 deletion internal/conversation_msg/read_drawing.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (c *Conversation) doUnreadCount(ctx context.Context, conversation *model_st
}
if (!latestMsg.IsRead) && datautil.Contain(latestMsg.Seq, seqs...) {
c.doUpdateConversation(common.Cmd2Value{Value: common.UpdateConNode{ConID: conversation.ConversationID,
Action: constant.UpdateLatestMessageChange, Args: []string{conversation.ConversationID}, Caller: "doUnreadCount"}, Ctx: ctx})
Action: constant.UpdateLatestMessageChange, Args: []string{conversation.ConversationID}}, Ctx: ctx})
}
} else {
if err := c.db.UpdateColumnsConversation(ctx, conversation.ConversationID, map[string]interface{}{"unread_count": 0}); err != nil {
Expand Down
24 changes: 0 additions & 24 deletions open_im_sdk/file.go

This file was deleted.

5 changes: 5 additions & 0 deletions open_im_sdk/third.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package open_im_sdk

import (
"github.com/openimsdk/openim-sdk-core/v3/internal/third/file"
"github.com/openimsdk/openim-sdk-core/v3/open_im_sdk_callback"
"github.com/openimsdk/openim-sdk-core/v3/pkg/sdkerrs"
)
Expand All @@ -38,3 +39,7 @@ func Logs(callback open_im_sdk_callback.Base, operationID string, logLevel int,
}
call(callback, operationID, UserForSDK.Third().Log, logLevel, file, line, msgs, err, keyAndValue)
}

func UploadFile(callback open_im_sdk_callback.Base, operationID string, req string, progress open_im_sdk_callback.UploadFileCallback) {
call(callback, operationID, UserForSDK.File().UploadFile, req, file.UploadFileCallback(progress))
}
2 changes: 1 addition & 1 deletion open_im_sdk/userRelated.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func setListener[T any](ctx context.Context, listener *T, getter func() T, setFu
func (u *LoginMgr) run(ctx context.Context) {
u.longConnMgr.Run(ctx)
go u.msgSyncer.DoListener(ctx)
go common.DoListener(u.conversation, u.ctx)
go common.DoListener(u.ctx, u.conversation)
go u.logoutListener(ctx)
}

Expand Down
90 changes: 90 additions & 0 deletions pkg/common/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package common

import (
"context"
"fmt"
"github.com/openimsdk/tools/log"
"runtime"
"runtime/debug"
"strings"
"time"
)

var packet string

func init() {
build, ok := debug.ReadBuildInfo()
if !ok {
return
}
packet = build.Main.Path
if packet != "" && !strings.HasSuffix(packet, "/") {
packet += "/"
}
}

type Cmd2Value struct {
Cmd string
Value any
Caller string
Ctx context.Context
}

func sendCmd(ch chan<- Cmd2Value, value Cmd2Value, timeout time.Duration) error {
if value.Caller == "" {
value.Caller = GetCaller(3)
}
log.ZDebug(value.Ctx, "sendCmd chan success", "caller", value.Caller, "cmd", value.Cmd, "value", value.Value)
if ch == nil {
log.ZError(value.Ctx, "sendCmd chan is nil", ErrChanNil, "caller", value.Caller, "cmd", value.Cmd, "value", value.Value)
return ErrChanNil
}
timer := time.NewTimer(timeout)
defer timer.Stop()
select {
case ch <- value:
log.ZInfo(value.Ctx, "sendCmd chan success", "caller", value.Caller, "cmd", value.Cmd, "value", value.Value)
return nil
case <-timer.C:
log.ZError(value.Ctx, "sendCmd chan timeout", ErrTimeout, "caller", value.Caller, "cmd", value.Cmd, "value", value.Value)
return ErrTimeout
}
}

func GetCaller(skip int) string {
pc, _, line, ok := runtime.Caller(skip)
if !ok {
return "runtime.caller.failed"
}
name := runtime.FuncForPC(pc).Name()
if packet != "" {
name = strings.TrimPrefix(name, packet)
}
return fmt.Sprintf("%s:%d", name, line)
}

type goroutine interface {
Work(cmd Cmd2Value)
GetCh() chan Cmd2Value
}

func DoListener(ctx context.Context, li goroutine) {
defer func() {
if r := recover(); r != nil {
err := fmt.Sprintf("panic: %+v\n%s", r, debug.Stack())
log.ZWarn(ctx, "DoListener panic", nil, "panic info", err)
}
}()

for {
select {
case cmd := <-li.GetCh():
log.ZInfo(cmd.Ctx, "recv cmd", "caller", cmd.Caller, "cmd", cmd.Cmd, "value", cmd.Value)
li.Work(cmd)
log.ZInfo(cmd.Ctx, "done cmd", "caller", cmd.Caller, "cmd", cmd.Cmd, "value", cmd.Value)
case <-ctx.Done():
log.ZInfo(ctx, "conversation done sdk logout.....")
return
}
}
}
Loading

0 comments on commit fa6d38a

Please sign in to comment.