Merge remote-tracking branch 'origin/errcode' into errcode

test-errcode
withchao 1 year ago
commit e103e9ad3b

@ -38,6 +38,7 @@ func run(port int) error {
if err != nil { if err != nil {
return err return err
} }
fmt.Println("api start init discov client")
var client discoveryregistry.SvcDiscoveryRegistry var client discoveryregistry.SvcDiscoveryRegistry
client, err = openKeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, client, err = openKeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema,
openKeeper.WithFreq(time.Hour), openKeeper.WithUserNameAndPassword(config.Config.Zookeeper.UserName, openKeeper.WithFreq(time.Hour), openKeeper.WithUserNameAndPassword(config.Config.Zookeeper.UserName,
@ -45,14 +46,18 @@ func run(port int) error {
if err != nil { if err != nil {
return err return err
} }
fmt.Println("api init discov client success")
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
if err := yaml.NewEncoder(buf).Encode(config.Config); err != nil { if err := yaml.NewEncoder(buf).Encode(config.Config); err != nil {
return err return err
} }
fmt.Println("api register public config to discov")
if err := client.RegisterConf2Registry(constant.OpenIMCommonConfigKey, buf.Bytes()); err != nil { if err := client.RegisterConf2Registry(constant.OpenIMCommonConfigKey, buf.Bytes()); err != nil {
return err return err
} }
fmt.Println("api register public config to discov success")
router := api.NewGinRouter(client, rdb) router := api.NewGinRouter(client, rdb)
fmt.Println("api init router success")
var address string var address string
if config.Config.Api.ListenIP != "" { if config.Config.Api.ListenIP != "" {
address = net.JoinHostPort(config.Config.Api.ListenIP, strconv.Itoa(port)) address = net.JoinHostPort(config.Config.Api.ListenIP, strconv.Itoa(port))

@ -45,10 +45,10 @@ type PongHandler func(string) error
type Client struct { type Client struct {
w *sync.Mutex w *sync.Mutex
conn LongConn conn LongConn
platformID int PlatformID int `json:"platformID"`
isCompress bool IsCompress bool `json:"isCompress"`
userID string UserID string `json:"userID"`
isBackground bool IsBackground bool `json:"isBackground"`
ctx *UserConnContext ctx *UserConnContext
longConnServer LongConnServer longConnServer LongConnServer
closed bool closed bool
@ -59,21 +59,21 @@ func newClient(ctx *UserConnContext, conn LongConn, isCompress bool) *Client {
return &Client{ return &Client{
w: new(sync.Mutex), w: new(sync.Mutex),
conn: conn, conn: conn,
platformID: utils.StringToInt(ctx.GetPlatformID()), PlatformID: utils.StringToInt(ctx.GetPlatformID()),
isCompress: isCompress, IsCompress: isCompress,
userID: ctx.GetUserID(), UserID: ctx.GetUserID(),
ctx: ctx, ctx: ctx,
} }
} }
func (c *Client) ResetClient(ctx *UserConnContext, conn LongConn, isCompress bool, longConnServer LongConnServer) { func (c *Client) ResetClient(ctx *UserConnContext, conn LongConn, isCompress bool, longConnServer LongConnServer) {
c.w = new(sync.Mutex) c.w = new(sync.Mutex)
c.conn = conn c.conn = conn
c.platformID = utils.StringToInt(ctx.GetPlatformID()) c.PlatformID = utils.StringToInt(ctx.GetPlatformID())
c.isCompress = isCompress c.IsCompress = isCompress
c.userID = ctx.GetUserID() c.UserID = ctx.GetUserID()
c.ctx = ctx c.ctx = ctx
c.longConnServer = longConnServer c.longConnServer = longConnServer
c.isBackground = false c.IsBackground = false
c.closed = false c.closed = false
c.closedErr = nil c.closedErr = nil
} }
@ -126,7 +126,7 @@ func (c *Client) readMessage() {
} }
func (c *Client) handleMessage(message []byte) error { func (c *Client) handleMessage(message []byte) error {
if c.isCompress { if c.IsCompress {
var decompressErr error var decompressErr error
message, decompressErr = c.longConnServer.DeCompress(message) message, decompressErr = c.longConnServer.DeCompress(message)
if decompressErr != nil { if decompressErr != nil {
@ -141,10 +141,10 @@ func (c *Client) handleMessage(message []byte) error {
if err := c.longConnServer.Validate(binaryReq); err != nil { if err := c.longConnServer.Validate(binaryReq); err != nil {
return utils.Wrap(err, "") return utils.Wrap(err, "")
} }
if binaryReq.SendID != c.userID { if binaryReq.SendID != c.UserID {
return utils.Wrap(errors.New("exception conn userID not same to req userID"), binaryReq.String()) return utils.Wrap(errors.New("exception conn userID not same to req userID"), binaryReq.String())
} }
ctx := mcontext.WithMustInfoCtx([]string{binaryReq.OperationID, binaryReq.SendID, constant.PlatformIDToName(c.platformID), c.ctx.GetConnID()}) ctx := mcontext.WithMustInfoCtx([]string{binaryReq.OperationID, binaryReq.SendID, constant.PlatformIDToName(c.PlatformID), c.ctx.GetConnID()})
log.ZDebug(ctx, "gateway req message", "req", binaryReq.String()) log.ZDebug(ctx, "gateway req message", "req", binaryReq.String())
var messageErr error var messageErr error
var resp []byte var resp []byte
@ -173,7 +173,7 @@ func (c *Client) setAppBackgroundStatus(ctx context.Context, req Req) ([]byte, e
if messageErr != nil { if messageErr != nil {
return nil, messageErr return nil, messageErr
} }
c.isBackground = isBackground c.IsBackground = isBackground
//todo callback //todo callback
return resp, nil return resp, nil
@ -240,7 +240,7 @@ func (c *Client) writeBinaryMsg(resp Resp) error {
return utils.Wrap(err, "") return utils.Wrap(err, "")
} }
_ = c.conn.SetWriteDeadline(writeWait) _ = c.conn.SetWriteDeadline(writeWait)
if c.isCompress { if c.IsCompress {
var compressErr error var compressErr error
resultBuf, compressErr = c.longConnServer.Compress(encodedBuf) resultBuf, compressErr = c.longConnServer.Compress(encodedBuf)
if compressErr != nil { if compressErr != nil {

@ -60,10 +60,10 @@ func (s *Server) GetUsersOnlineStatus(ctx context.Context, req *msggateway.GetUs
for _, client := range clients { for _, client := range clients {
if client != nil { if client != nil {
ps := new(msggateway.GetUsersOnlineStatusResp_SuccessDetail) ps := new(msggateway.GetUsersOnlineStatusResp_SuccessDetail)
ps.Platform = constant.PlatformIDToName(client.platformID) ps.Platform = constant.PlatformIDToName(client.PlatformID)
ps.Status = constant.OnlineStatus ps.Status = constant.OnlineStatus
ps.ConnID = client.ctx.GetConnID() ps.ConnID = client.ctx.GetConnID()
ps.IsBackground = client.isBackground ps.IsBackground = client.IsBackground
temp.Status = constant.OnlineStatus temp.Status = constant.OnlineStatus
temp.DetailPlatformStatus = append(temp.DetailPlatformStatus, ps) temp.DetailPlatformStatus = append(temp.DetailPlatformStatus, ps)
} }
@ -98,15 +98,15 @@ func (s *Server) SuperGroupOnlineBatchPushOneMsg(ctx context.Context, req *msgga
if client != nil { if client != nil {
temp := &msggateway.SingleMsgToUserPlatform{ temp := &msggateway.SingleMsgToUserPlatform{
RecvID: v, RecvID: v,
RecvPlatFormID: int32(client.platformID), RecvPlatFormID: int32(client.PlatformID),
} }
if !client.isBackground { if !client.IsBackground {
err := client.PushMessage(ctx, req.MsgData) err := client.PushMessage(ctx, req.MsgData)
if err != nil { if err != nil {
temp.ResultCode = -2 temp.ResultCode = -2
resp = append(resp, temp) resp = append(resp, temp)
} else { } else {
if utils.IsContainInt(client.platformID, s.pushTerminal) { if utils.IsContainInt(client.PlatformID, s.pushTerminal) {
tempT.OnlinePush = true tempT.OnlinePush = true
prome.Inc(prome.MsgOnlinePushSuccessCounter) prome.Inc(prome.MsgOnlinePushSuccessCounter)
resp = append(resp, temp) resp = append(resp, temp)

@ -123,22 +123,22 @@ func (ws *WsServer) registerClient(client *Client) {
clientOK bool clientOK bool
cli []*Client cli []*Client
) )
cli, userOK, clientOK = ws.clients.Get(client.userID, client.platformID) cli, userOK, clientOK = ws.clients.Get(client.UserID, client.PlatformID)
if !userOK { if !userOK {
log.ZDebug(client.ctx, "user not exist", "userID", client.userID, "platformID", client.platformID) log.ZDebug(client.ctx, "user not exist", "userID", client.UserID, "platformID", client.PlatformID)
ws.clients.Set(client.userID, client) ws.clients.Set(client.UserID, client)
atomic.AddInt64(&ws.onlineUserNum, 1) atomic.AddInt64(&ws.onlineUserNum, 1)
atomic.AddInt64(&ws.onlineUserConnNum, 1) atomic.AddInt64(&ws.onlineUserConnNum, 1)
} else { } else {
log.ZDebug(client.ctx, "user exist", "userID", client.userID, "platformID", client.platformID) log.ZDebug(client.ctx, "user exist", "userID", client.UserID, "platformID", client.PlatformID)
if clientOK { //已经有同平台的连接存在 if clientOK { //已经有同平台的连接存在
ws.clients.Set(client.userID, client) ws.clients.Set(client.UserID, client)
ws.multiTerminalLoginChecker(cli) ws.multiTerminalLoginChecker(cli)
log.ZInfo(client.ctx, "repeat login", "userID", client.userID, "platformID", client.platformID, "old remote addr", getRemoteAdders(cli)) log.ZInfo(client.ctx, "repeat login", "userID", client.UserID, "platformID", client.PlatformID, "old remote addr", getRemoteAdders(cli))
atomic.AddInt64(&ws.onlineUserConnNum, 1) atomic.AddInt64(&ws.onlineUserConnNum, 1)
} else { } else {
ws.clients.Set(client.userID, client) ws.clients.Set(client.UserID, client)
atomic.AddInt64(&ws.onlineUserConnNum, 1) atomic.AddInt64(&ws.onlineUserConnNum, 1)
} }
} }
@ -161,7 +161,7 @@ func (ws *WsServer) multiTerminalLoginChecker(client []*Client) {
} }
func (ws *WsServer) unregisterClient(client *Client) { func (ws *WsServer) unregisterClient(client *Client) {
defer ws.clientPool.Put(client) defer ws.clientPool.Put(client)
isDeleteUser := ws.clients.delete(client.userID, client.ctx.GetRemoteAddr()) isDeleteUser := ws.clients.delete(client.UserID, client.ctx.GetRemoteAddr())
if isDeleteUser { if isDeleteUser {
atomic.AddInt64(&ws.onlineUserNum, -1) atomic.AddInt64(&ws.onlineUserNum, -1)
} }

@ -25,7 +25,7 @@ func (u *UserMap) Get(key string, platformID int) ([]*Client, bool, bool) {
if userExisted { if userExisted {
var clients []*Client var clients []*Client
for _, client := range allClients.([]*Client) { for _, client := range allClients.([]*Client) {
if client.platformID == platformID { if client.PlatformID == platformID {
clients = append(clients, client) clients = append(clients, client)
} }
} }

Loading…
Cancel
Save