From e8fe7cc853f35009d50c5c1ae932995e90ef859c Mon Sep 17 00:00:00 2001 From: wenxu12345 <44203734@qq.com> Date: Wed, 25 May 2022 17:14:09 +0800 Subject: [PATCH 01/12] debug --- internal/push/logic/push_to_client.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/internal/push/logic/push_to_client.go b/internal/push/logic/push_to_client.go index 21e8e3023..6e9b8787b 100644 --- a/internal/push/logic/push_to_client.go +++ b/internal/push/logic/push_to_client.go @@ -17,6 +17,7 @@ import ( "Open_IM/pkg/utils" "context" "encoding/json" + "google.golang.org/grpc" "strings" ) @@ -32,11 +33,16 @@ type AtContent struct { IsAtSelf bool `json:"isAtSelf"` } +var grpcCons []*grpc.ClientConn + func MsgToUser(pushMsg *pbPush.PushMsgReq) { var wsResult []*pbRelay.SingleMsgToUser isOfflinePush := utils.GetSwitchFromOptions(pushMsg.MsgData.Options, constant.IsOfflinePush) log.Debug(pushMsg.OperationID, "Get msg from msg_transfer And push msg", pushMsg.String()) - grpcCons := getcdv3.GetConn4Unique(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImOnlineMessageRelayName) + if len(grpcCons) == 0 { + log.NewWarn(pushMsg.OperationID, "first GetConn4Unique ") + grpcCons = getcdv3.GetConn4Unique(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImOnlineMessageRelayName) + } //Online push message log.Debug("test", pushMsg.OperationID, "len grpc", len(grpcCons), "data", pushMsg.String()) for _, v := range grpcCons { From 837cf10e36afff6ca1c8597ae0e94f2ac04636ac Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Wed, 25 May 2022 17:38:49 +0800 Subject: [PATCH 02/12] del msg --- test/mongo/main.go | 33 +++++++++++++++++++++++++++++++++ test/mongo/mongo_utils.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 test/mongo/main.go create mode 100644 test/mongo/mongo_utils.go diff --git a/test/mongo/main.go b/test/mongo/main.go new file mode 100644 index 000000000..29ea4ea4f --- /dev/null +++ b/test/mongo/main.go @@ -0,0 +1,33 @@ +package mongo + +import ( + "context" + "flag" + "fmt" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" +) + +var ( + client *mongo.Client +) + +func initDB() { + clientOptions := options.Client().ApplyURI("mongodb://127.0.0.1:37017/openIM/?maxPoolSize=100") + client, err := mongo.Connect(context.TODO(), clientOptions) + if err != nil { + panic(err) + } + err = client.Ping(context.TODO(), nil) + if err != nil { + panic(err) + } + fmt.Println("Connected to MongoDB!") +} + +func main() { + userID := flag.String("userID", "", "userID") + flag.Parse() + fmt.Println("userID:", userID) + GetUserAllChat(*userID) +} diff --git a/test/mongo/mongo_utils.go b/test/mongo/mongo_utils.go new file mode 100644 index 000000000..e2c14ccdd --- /dev/null +++ b/test/mongo/mongo_utils.go @@ -0,0 +1,36 @@ +package mongo + +import ( + "Open_IM/pkg/common/config" + "Open_IM/pkg/common/db" + server_api_params "Open_IM/pkg/proto/sdk_ws" + "context" + "fmt" + "github.com/golang/protobuf/proto" + "go.mongodb.org/mongo-driver/bson/primitive" + "gopkg.in/mgo.v2/bson" +) + +func GetUserAllChat(uid string) { + collection := client.Database(config.Config.Mongo.DBDatabase).Collection("msg") + var userChatList []db.UserChat + result, err := collection.Find(context.Background(), bson.M{"uid": primitive.Regex{Pattern: uid}}) + if err != nil { + fmt.Println(err.Error()) + return + } + if err := result.All(context.Background(), &userChatList); err != nil { + fmt.Println(err.Error()) + } + for _, userChat := range userChatList { + for _, msg := range userChat.Msg { + msgData := &server_api_params.MsgData{} + err := proto.Unmarshal(msg.Msg, msgData) + if err != nil { + fmt.Println(err.Error(), msg) + continue + } + fmt.Println(*msgData) + } + } +} From 13b9749e8f3443fe71ac742ef74e881ac45d67a6 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Wed, 25 May 2022 17:41:32 +0800 Subject: [PATCH 03/12] del msg --- test/mongo/{ => cmd}/main.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) rename test/mongo/{ => cmd}/main.go (86%) diff --git a/test/mongo/main.go b/test/mongo/cmd/main.go similarity index 86% rename from test/mongo/main.go rename to test/mongo/cmd/main.go index 29ea4ea4f..d3711e2ba 100644 --- a/test/mongo/main.go +++ b/test/mongo/cmd/main.go @@ -1,6 +1,7 @@ -package mongo +package cmd import ( + mongo2 "Open_IM/test/mongo" "context" "flag" "fmt" @@ -12,7 +13,7 @@ var ( client *mongo.Client ) -func initDB() { +func init() { clientOptions := options.Client().ApplyURI("mongodb://127.0.0.1:37017/openIM/?maxPoolSize=100") client, err := mongo.Connect(context.TODO(), clientOptions) if err != nil { @@ -29,5 +30,5 @@ func main() { userID := flag.String("userID", "", "userID") flag.Parse() fmt.Println("userID:", userID) - GetUserAllChat(*userID) + mongo2.GetUserAllChat(*userID) } From 72c7cc2c67d3f9393d52b914331b8197656e42e1 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Wed, 25 May 2022 17:42:45 +0800 Subject: [PATCH 04/12] script --- test/mongo/cmd/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mongo/cmd/main.go b/test/mongo/cmd/main.go index d3711e2ba..75ec8323f 100644 --- a/test/mongo/cmd/main.go +++ b/test/mongo/cmd/main.go @@ -1,4 +1,4 @@ -package cmd +package main import ( mongo2 "Open_IM/test/mongo" From 76b5affaaab8f9a63891c76f19156d2d03b64298 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Wed, 25 May 2022 17:46:02 +0800 Subject: [PATCH 05/12] script --- test/mongo/cmd/main.go | 9 +++------ test/mongo/mongo_utils.go | 8 +++++++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/test/mongo/cmd/main.go b/test/mongo/cmd/main.go index 75ec8323f..8c8b2dbef 100644 --- a/test/mongo/cmd/main.go +++ b/test/mongo/cmd/main.go @@ -9,17 +9,14 @@ import ( "go.mongodb.org/mongo-driver/mongo/options" ) -var ( - client *mongo.Client -) - func init() { clientOptions := options.Client().ApplyURI("mongodb://127.0.0.1:37017/openIM/?maxPoolSize=100") - client, err := mongo.Connect(context.TODO(), clientOptions) + var err error + mongo2.Client, err = mongo.Connect(context.TODO(), clientOptions) if err != nil { panic(err) } - err = client.Ping(context.TODO(), nil) + err = mongo2.Client.Ping(context.TODO(), nil) if err != nil { panic(err) } diff --git a/test/mongo/mongo_utils.go b/test/mongo/mongo_utils.go index e2c14ccdd..6f211cd69 100644 --- a/test/mongo/mongo_utils.go +++ b/test/mongo/mongo_utils.go @@ -4,15 +4,21 @@ import ( "Open_IM/pkg/common/config" "Open_IM/pkg/common/db" server_api_params "Open_IM/pkg/proto/sdk_ws" + "Open_IM/test/mongo/cmd" "context" "fmt" "github.com/golang/protobuf/proto" "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/mongo" "gopkg.in/mgo.v2/bson" ) +var ( + Client *mongo.Client +) + func GetUserAllChat(uid string) { - collection := client.Database(config.Config.Mongo.DBDatabase).Collection("msg") + collection := Client.Database(config.Config.Mongo.DBDatabase).Collection("msg") var userChatList []db.UserChat result, err := collection.Find(context.Background(), bson.M{"uid": primitive.Regex{Pattern: uid}}) if err != nil { From c02f95c2fc1bdcdeec265f6f01ed78586386e5be Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Wed, 25 May 2022 17:47:04 +0800 Subject: [PATCH 06/12] script --- test/mongo/mongo_utils.go | 1 - 1 file changed, 1 deletion(-) diff --git a/test/mongo/mongo_utils.go b/test/mongo/mongo_utils.go index 6f211cd69..86c305e26 100644 --- a/test/mongo/mongo_utils.go +++ b/test/mongo/mongo_utils.go @@ -4,7 +4,6 @@ import ( "Open_IM/pkg/common/config" "Open_IM/pkg/common/db" server_api_params "Open_IM/pkg/proto/sdk_ws" - "Open_IM/test/mongo/cmd" "context" "fmt" "github.com/golang/protobuf/proto" From fc677ca0e90f678a1195ac9e0cc3e9af560b98c1 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Wed, 25 May 2022 17:48:28 +0800 Subject: [PATCH 07/12] script --- test/mongo/cmd/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mongo/cmd/main.go b/test/mongo/cmd/main.go index 8c8b2dbef..ffc4a6eb0 100644 --- a/test/mongo/cmd/main.go +++ b/test/mongo/cmd/main.go @@ -26,6 +26,6 @@ func init() { func main() { userID := flag.String("userID", "", "userID") flag.Parse() - fmt.Println("userID:", userID) + fmt.Println("userID:", *userID) mongo2.GetUserAllChat(*userID) } From 0ed223ef012e9429148a239b1771abd307ab7c3e Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Wed, 25 May 2022 17:58:05 +0800 Subject: [PATCH 08/12] script --- test/mongo/cmd/main.go | 18 ++++++++++++++++-- test/mongo/mongo_utils.go | 6 ++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/test/mongo/cmd/main.go b/test/mongo/cmd/main.go index ffc4a6eb0..2a8d41523 100644 --- a/test/mongo/cmd/main.go +++ b/test/mongo/cmd/main.go @@ -1,6 +1,7 @@ package main import ( + "Open_IM/pkg/common/config" mongo2 "Open_IM/test/mongo" "context" "flag" @@ -10,9 +11,22 @@ import ( ) func init() { - clientOptions := options.Client().ApplyURI("mongodb://127.0.0.1:37017/openIM/?maxPoolSize=100") + uri := "mongodb://sample.host:27017/?maxPoolSize=20&w=majority" + if config.Config.Mongo.DBUri != "" { + // example: mongodb://$user:$password@mongo1.mongo:27017,mongo2.mongo:27017,mongo3.mongo:27017/$DBDatabase/?replicaSet=rs0&readPreference=secondary&authSource=admin&maxPoolSize=$DBMaxPoolSize + uri = config.Config.Mongo.DBUri + } else { + if config.Config.Mongo.DBPassword != "" && config.Config.Mongo.DBUserName != "" { + uri = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d", config.Config.Mongo.DBUserName, config.Config.Mongo.DBPassword, config.Config.Mongo.DBAddress[0], + config.Config.Mongo.DBDatabase, config.Config.Mongo.DBMaxPoolSize) + } else { + uri = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d", + config.Config.Mongo.DBAddress[0], config.Config.Mongo.DBDatabase, + config.Config.Mongo.DBMaxPoolSize) + } + } var err error - mongo2.Client, err = mongo.Connect(context.TODO(), clientOptions) + mongo2.Client, err = mongo.Connect(context.TODO(), options.Client().ApplyURI(uri)) if err != nil { panic(err) } diff --git a/test/mongo/mongo_utils.go b/test/mongo/mongo_utils.go index 86c305e26..53835c355 100644 --- a/test/mongo/mongo_utils.go +++ b/test/mongo/mongo_utils.go @@ -10,6 +10,7 @@ import ( "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" "gopkg.in/mgo.v2/bson" + "time" ) var ( @@ -17,14 +18,15 @@ var ( ) func GetUserAllChat(uid string) { + ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second) collection := Client.Database(config.Config.Mongo.DBDatabase).Collection("msg") var userChatList []db.UserChat result, err := collection.Find(context.Background(), bson.M{"uid": primitive.Regex{Pattern: uid}}) if err != nil { - fmt.Println(err.Error()) + fmt.Println("find error", err.Error()) return } - if err := result.All(context.Background(), &userChatList); err != nil { + if err := result.All(ctx, &userChatList); err != nil { fmt.Println(err.Error()) } for _, userChat := range userChatList { From 730bedf0a5f32eacd24053416fa30eb6969f9be4 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Wed, 25 May 2022 18:01:39 +0800 Subject: [PATCH 09/12] script --- test/mongo/mongo_utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mongo/mongo_utils.go b/test/mongo/mongo_utils.go index 53835c355..aac63e548 100644 --- a/test/mongo/mongo_utils.go +++ b/test/mongo/mongo_utils.go @@ -37,7 +37,7 @@ func GetUserAllChat(uid string) { fmt.Println(err.Error(), msg) continue } - fmt.Println(*msgData) + fmt.Println("seq:", msgData.Seq, "status:", msgData.Status) } } } From 93d63508dccfc9957070b542dfb32f7fa5bbb41a Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Wed, 25 May 2022 18:05:24 +0800 Subject: [PATCH 10/12] script --- test/mongo/mongo_utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mongo/mongo_utils.go b/test/mongo/mongo_utils.go index aac63e548..f736f0d54 100644 --- a/test/mongo/mongo_utils.go +++ b/test/mongo/mongo_utils.go @@ -21,7 +21,7 @@ func GetUserAllChat(uid string) { ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second) collection := Client.Database(config.Config.Mongo.DBDatabase).Collection("msg") var userChatList []db.UserChat - result, err := collection.Find(context.Background(), bson.M{"uid": primitive.Regex{Pattern: uid}}) + result, err := collection.Find(context.Background(), bson.M{"uid": bson.M{"$regex": uid}}) if err != nil { fmt.Println("find error", err.Error()) return From f9698414642f617d87af44f94cb9dacf9fc1656b Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Wed, 25 May 2022 18:05:47 +0800 Subject: [PATCH 11/12] script --- test/mongo/mongo_utils.go | 1 - 1 file changed, 1 deletion(-) diff --git a/test/mongo/mongo_utils.go b/test/mongo/mongo_utils.go index f736f0d54..243dd6d00 100644 --- a/test/mongo/mongo_utils.go +++ b/test/mongo/mongo_utils.go @@ -7,7 +7,6 @@ import ( "context" "fmt" "github.com/golang/protobuf/proto" - "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" "gopkg.in/mgo.v2/bson" "time" From d791aa2e4fbc51ab44710c2e2f3aea671b3fd847 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Wed, 25 May 2022 18:09:41 +0800 Subject: [PATCH 12/12] script --- test/mongo/mongo_utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mongo/mongo_utils.go b/test/mongo/mongo_utils.go index 243dd6d00..748cfc9fb 100644 --- a/test/mongo/mongo_utils.go +++ b/test/mongo/mongo_utils.go @@ -36,7 +36,7 @@ func GetUserAllChat(uid string) { fmt.Println(err.Error(), msg) continue } - fmt.Println("seq:", msgData.Seq, "status:", msgData.Status) + fmt.Println("sendID: ", msgData.SendID, "recvID: ", msgData.RecvID, "seq:", msgData.Seq, "status:", msgData.Status) } } }