From 3e95e8b8c3672e04766bb9968bb1f70e294c596f Mon Sep 17 00:00:00 2001 From: hujiebin Date: Mon, 6 Nov 2023 11:47:19 +0800 Subject: [PATCH] feat:rpc logs --- algo/algo.go | 56 ++++++++++++++++++++++++++++++++++++------- common/mysql/mysql.go | 37 ---------------------------- 2 files changed, 47 insertions(+), 46 deletions(-) diff --git a/algo/algo.go b/algo/algo.go index d881140..513e58f 100644 --- a/algo/algo.go +++ b/algo/algo.go @@ -10,6 +10,7 @@ import ( "hilo-algoCenter/common" "hilo-algoCenter/common/config" "hilo-algoCenter/common/mylogrus" + "hilo-algoCenter/common/mysql" "hilo-algoCenter/cv" "hilo-algoCenter/protocol" "hilo-algoCenter/protocol/userCenter" @@ -43,7 +44,7 @@ type MatchResult struct { ExcellentRelation float64 } -func matchSuccess(c userCenter.UserClient, uids []uint64, msgType uint32, data []byte) error { +func matchSuccess(c userCenter.UserClient, uids []uint64, msgType uint32, data []byte, msg *userProxy.MatchSuccess) error { ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) defer cancel() rsp, err := c.Multicast(ctx, &userCenter.MulticastMessage{ @@ -57,6 +58,11 @@ func matchSuccess(c userCenter.UserClient, uids []uint64, msgType uint32, data [ if rsp != nil && len(rsp.FailedUids) > 0 { err = fmt.Errorf("%d users failed", len(rsp.FailedUids)) } + //记录socket,注意闭包问题 + go func(uids []uint64, msg *userProxy.MatchSuccess, rspUids []uint64, err error) { + buf, _ := json.Marshal(msg) + AddRpcLogs(common.MsgTypeMatchV2Success, uids, string(buf[:]), rspUids, err) + }(uids, msg, rsp.FailedUids, err) return err } @@ -457,8 +463,7 @@ func processMatchFail(userClient userCenter.UserClient, val MatchResult, matchCy } if buffer, err := proto.Marshal(msg); err == nil { rPcErr := "" - matchSuccess(userClient, []uint64{val.User1Id}, common.MsgTypeMatchSuccess, buffer) // todo for updating - if err = matchSuccess(userClient, []uint64{val.User1Id}, common.MsgTypeMatchV2Success, buffer); err == nil { + if err = matchSuccess(userClient, []uint64{val.User1Id}, common.MsgTypeMatchV2Success, buffer, msg); err == nil { mylogrus.MyLog.Infof("match cycle:%v, match result sent msg success, single LocalUserId:%v, RemoteUserId %v\n", matchCycle, msg.LocalUserId, msg.RemoteUserId) } else { mylogrus.MyLog.Errorf("match cycle:%v, match result sent msg fail, single LocalUserId:%v, RemoteUserId %v\n", matchCycle, msg.LocalUserId, msg.RemoteUserId) @@ -500,9 +505,8 @@ func processMatchSuccess(userClient userCenter.UserClient, val MatchResult, matc ok1 := false ok2 := false if buffer, err := proto.Marshal(msg); err == nil { - matchSuccess(userClient, uids, common.MsgTypeMatchSuccess, buffer) // todo for updating - if err = matchSuccess(userClient, uids, common.MsgTypeMatchV2Success, buffer); err == nil { - mylogrus.MyLog.Infof("match cycle:%v, match result sent msg begin LocalUserId:%v, RemoteUserId:%v, LocalUserId:%v, RemoteUserId:%v\n", matchCycle, msg.LocalUserId, msg.RemoteUserId, val.User1Id, val.User2Id) + if err = matchSuccess(userClient, uids, common.MsgTypeMatchV2Success, buffer, msg); err == nil { + mylogrus.MyLog.Infof("match cycle:%v, match result sent msg begin LocalUserId:%v, RemoteUserId:%v, LocalUserId:%v, RemoteUserId:%v,matchUid:%v", matchCycle, msg.LocalUserId, msg.RemoteUserId, val.User1Id, val.User2Id, msg.MatchUniqueId) ok1 = true // 交换数据 uids[0] = val.User2Id @@ -510,10 +514,9 @@ func processMatchSuccess(userClient userCenter.UserClient, val MatchResult, matc msg.RemoteUserId = val.user1External msg.RemoteUser = user1Info if buffer, err = proto.Marshal(msg); err == nil { - matchSuccess(userClient, uids, common.MsgTypeMatchSuccess, buffer) // todo for updating - if err = matchSuccess(userClient, uids, common.MsgTypeMatchV2Success, buffer); err == nil { + if err = matchSuccess(userClient, uids, common.MsgTypeMatchV2Success, buffer, msg); err == nil { ok2 = true - mylogrus.MyLog.Infof("match cycle:%v, match result sent msg success LocalUserId:%v, RemoteUserId:%v, LocalUserId:%v, RemoteUserId:%v\n", matchCycle, msg.LocalUserId, msg.RemoteUserId, val.User1Id, val.User2Id) + mylogrus.MyLog.Infof("match cycle:%v, match result sent msg success LocalUserId:%v, RemoteUserId:%v, LocalUserId:%v, RemoteUserId:%v,matchUid:%v", matchCycle, msg.LocalUserId, msg.RemoteUserId, val.User1Id, val.User2Id, msg.MatchUniqueId) } } } @@ -775,3 +778,38 @@ func toSingleMatch(matchCycle *MatchCycle, hasMatch map[string]bool, tradeUnionM } return singleResults } + +type TypeRpc uint8 + +type RpcLog struct { + ID uint64 `gorm:"primary_key"` + Type TypeRpc + UserId string + Msg string + Err string + FailUids string +} + +func (RpcLog) TableName() string { + month := time.Now().Format("200601") + return fmt.Sprintf("rpc_log_%s", month) +} + +func AddRpcLogs(t TypeRpc, userIds []uint64, msg string, failUids []uint64, err error) { + errStr := "" + if err != nil { + errStr = err.Error() + } + failUidStr, _ := json.Marshal(failUids) + userIdStr, _ := json.Marshal(userIds) + logRpc := RpcLog{ + Type: t, + UserId: string(userIdStr[:]), + Msg: msg, + Err: errStr, + FailUids: string(failUidStr[:]), + } + if e := mysql.Db.Table(RpcLog{}.TableName()).Create(&logRpc).Error; e != nil { + mylogrus.MyLog.Errorf("log rpc save fail, err:%v", e) + } +} diff --git a/common/mysql/mysql.go b/common/mysql/mysql.go index bed5c11..4dc0b66 100644 --- a/common/mysql/mysql.go +++ b/common/mysql/mysql.go @@ -69,40 +69,3 @@ func init() { Db.LogMode(true) Db.SetLogger(log.New(os.Stdout, "\r\n", 0))*/ } - -/*func updateTimeStampForUpdateCallback(scope *gorm.Scope) { - if _, ok := scope.Get("gorm:update_column"); !ok { - _ = scope.SetColumn("UpdatedTime", time.Now().Unix()) - } -} - -func updateTimeStampForCreateCallback(scope *gorm.Scope) { - if !scope.HasError() { - nowTime := time.Now().Unix() - if createTimeField, ok := scope.FieldByName("CreatedTime"); ok { - if createTimeField.IsBlank { - _ = createTimeField.Set(nowTime) - } - } - - if modifyTimeField, ok := scope.FieldByName("UpdatedTime"); ok { - if modifyTimeField.IsBlank { - _ = modifyTimeField.Set(nowTime) - } - } - } -}*/ - -func HasTable(tableName string) bool { - //var num int - //err := Db.Exec("SELECT COUNT(*) num FROM information_schema.TABLES WHERE table_name =%s;", tableName).Pluck("num", &num).Error - //if err != nil { - // mylogrus.MyLog.Errorf("HasTable err: %v, stack: %v", err, string(debug.Stack())) - // return - //} - //if num > 0 { - // has = true - //} - - return Db.Migrator().HasTable(tableName) -} -- 2.22.0