Skip to content

Commit

Permalink
feat: optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
zjwshisb committed Jan 21, 2025
1 parent 5215ba0 commit 6a09618
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 31 deletions.
2 changes: 1 addition & 1 deletion internal/logic/chat/admin_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

func newAdminManager(cluster bool) *adminManager {
adminM = &adminManager{
newManager(10, 100, time.Minute, cluster, consts.WsTypeAdmin),
newManager(10, time.Minute, cluster, consts.WsTypeAdmin),
}
adminM.on(eventRegister, adminM.onRegister)
adminM.on(eventUnRegister, adminM.onUnRegister)
Expand Down
5 changes: 1 addition & 4 deletions internal/logic/chat/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,7 @@ func (c *client) readMsg() {
}
msg.CustomerId = c.getCustomerId()
msg.ReceivedAt = gtime.Now()
c.manager.receiveMessage(&chatConnMessage{
Msg: msg,
Conn: c,
})
c.manager.handleMessage(c, msg)
c.lastActive = gtime.Now()
}
}
Expand Down
35 changes: 10 additions & 25 deletions internal/logic/chat/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ type connManager interface {
run()
ping()
SendAction(act *api.ChatAction, conn ...iWsConn)
receiveMessage(cm *chatConnMessage)
handleReceiveMessage()
handleMessage(conn iWsConn, msg *model.CustomerChatMessage)
noticeRead(ctx context.Context, customerId uint, uid uint, msgIds []uint, forceLocal ...bool) error
}

Expand All @@ -58,10 +57,9 @@ type eventArg struct {
msg *model.CustomerChatMessage
}

func newManager(shareCount uint, msgCount int, pingDuration time.Duration, cluster bool, types string) *manager {
func newManager(shareCount uint, pingDuration time.Duration, cluster bool, types string) *manager {
return &manager{
shardCount: shareCount,
connMessages: make(chan *chatConnMessage, msgCount),
events: nil,
pingDuration: pingDuration,
cluster: cluster,
Expand All @@ -72,7 +70,6 @@ func newManager(shareCount uint, msgCount int, pingDuration time.Duration, clust
type manager struct {
shardCount uint // 分组数量, 默认 10
shard []*shard // 分组切片
connMessages chan *chatConnMessage // 接受从conn所读取消息的chan
events map[string][]eventHandle // 事件
pingDuration time.Duration // default to 10 seconds
cluster bool
Expand Down Expand Up @@ -143,25 +140,14 @@ func (m *manager) getSpread(customerId uint) *shard {
return m.shard[m.getMod(customerId)]
}

// ReceiveMessage 接受消息
func (m *manager) receiveMessage(cm *chatConnMessage) {
m.connMessages <- cm
}

// 从conn接受消息并处理
func (m *manager) handleReceiveMessage() {
for {
payload := <-m.connMessages
go func() {
ctx := gctx.New()
err := m.trigger(ctx, eventMessage, eventArg{
conn: payload.Conn,
msg: payload.Msg,
})
if err != nil {
log.Errorf(ctx, "%+v", err)
}
}()
func (m *manager) handleMessage(conn iWsConn, msg *model.CustomerChatMessage) {
ctx := gctx.New()
err := m.trigger(ctx, eventMessage, eventArg{
conn: conn,
msg: msg,
})
if err != nil {
log.Errorf(ctx, "%+v", err)
}
}

Expand Down Expand Up @@ -430,6 +416,5 @@ func (m *manager) run() {
mutex: &sync.RWMutex{},
}
}
go m.handleReceiveMessage()
go m.ping()
}
2 changes: 1 addition & 1 deletion internal/logic/chat/user_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

func newUserManager(cluster bool) *userManager {
userM = &userManager{
newManager(10, 100, time.Minute, cluster, consts.WsTypeUser),
newManager(10, time.Minute, cluster, consts.WsTypeUser),
}
userM.on(eventRegister, userM.onRegister)
userM.on(eventUnRegister, userM.onUnRegister)
Expand Down

0 comments on commit 6a09618

Please sign in to comment.