From 2b9a3536e78dac7a272cc23bf51e78f01f740fb7 Mon Sep 17 00:00:00 2001 From: wenxu12345 <44203734@qq.com> Date: Tue, 26 Oct 2021 10:46:00 +0800 Subject: [PATCH 1/3] exception handling --- src/common/db/mongoModel.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/common/db/mongoModel.go b/src/common/db/mongoModel.go index a6e2651fe..6d315193e 100644 --- a/src/common/db/mongoModel.go +++ b/src/common/db/mongoModel.go @@ -147,7 +147,12 @@ func (d *DataBases) SaveUserChat(uid string, sendTime int64, m proto.Message) er if session == nil { return errors.New("session == nil") } - defer session.Close() + defer func() { + if session != nil { + session.Close() + } + }() + log.NewInfo("", "get mgoSession cost time", getCurrentTimestampByMill()-newTime) c := session.DB(config.Config.Mongo.DBDatabase).C(cChat) From 29f3fd46a8623af2bd54740661c3be599cf1ef31 Mon Sep 17 00:00:00 2001 From: wenxu12345 <44203734@qq.com> Date: Tue, 26 Oct 2021 12:03:16 +0800 Subject: [PATCH 2/3] exception handling --- src/common/db/model.go | 6 +++++- src/common/db/mongoModel.go | 8 ++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/common/db/model.go b/src/common/db/model.go index f34b4c305..f8be3747a 100644 --- a/src/common/db/model.go +++ b/src/common/db/model.go @@ -22,7 +22,6 @@ func key(dbAddress, dbName string) string { func init() { //mysql init initMysqlDB() - // mongo init mgoDailInfo := &mgo.DialInfo{ Addrs: config.Config.Mongo.DBAddress, Direct: config.Config.Mongo.DBDirect, @@ -40,6 +39,11 @@ func init() { DB.mgoSession = mgoSession DB.mgoSession.SetMode(mgo.Monotonic, true) + c := DB.mgoSession.DB(config.Config.Mongo.DBDatabase).C(cChat) + err = c.EnsureIndexKey("uid") + if err != nil { + panic(err) + } // redis pool init DB.redisPool = &redis.Pool{ MaxIdle: config.Config.Redis.DBMaxIdle, diff --git a/src/common/db/mongoModel.go b/src/common/db/mongoModel.go index 6d315193e..a0fbbfb18 100644 --- a/src/common/db/mongoModel.go +++ b/src/common/db/mongoModel.go @@ -22,6 +22,7 @@ type MsgInfo struct { type UserChat struct { UID string Msg []MsgInfo + BId bson.ObjectId `bson:"bid"` } type GroupMember struct { @@ -155,12 +156,15 @@ func (d *DataBases) SaveUserChat(uid string, sendTime int64, m proto.Message) er log.NewInfo("", "get mgoSession cost time", getCurrentTimestampByMill()-newTime) c := session.DB(config.Config.Mongo.DBDatabase).C(cChat) - + err := c.EnsureIndexKey("uid") + if err != nil { + log.NewError("", "EnsureIndexKey uid failed ", err.Error()) + } n, err := c.Find(bson.M{"uid": uid}).Count() if err != nil { return err } - log.NewInfo("", "find mgo uid cost time", getCurrentTimestampByMill()-newTime) + log.NewInfo("", "find and create index mgo uid cost time", getCurrentTimestampByMill()-newTime) sMsg := MsgInfo{} sMsg.SendTime = sendTime if sMsg.Msg, err = proto.Marshal(m); err != nil { From 8b981f2ec6143d4183afc71d94b584388267845e Mon Sep 17 00:00:00 2001 From: wenxu12345 <44203734@qq.com> Date: Tue, 26 Oct 2021 14:53:12 +0800 Subject: [PATCH 3/3] mongo index --- src/common/db/mongoModel.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/common/db/mongoModel.go b/src/common/db/mongoModel.go index a0fbbfb18..7a5faa5d0 100644 --- a/src/common/db/mongoModel.go +++ b/src/common/db/mongoModel.go @@ -22,7 +22,6 @@ type MsgInfo struct { type UserChat struct { UID string Msg []MsgInfo - BId bson.ObjectId `bson:"bid"` } type GroupMember struct { @@ -156,10 +155,6 @@ func (d *DataBases) SaveUserChat(uid string, sendTime int64, m proto.Message) er log.NewInfo("", "get mgoSession cost time", getCurrentTimestampByMill()-newTime) c := session.DB(config.Config.Mongo.DBDatabase).C(cChat) - err := c.EnsureIndexKey("uid") - if err != nil { - log.NewError("", "EnsureIndexKey uid failed ", err.Error()) - } n, err := c.Find(bson.M{"uid": uid}).Count() if err != nil { return err @@ -171,6 +166,8 @@ func (d *DataBases) SaveUserChat(uid string, sendTime int64, m proto.Message) er return err } + log.NewInfo("insert len: ", len(sMsg.Msg)) + if n == 0 { sChat := UserChat{} sChat.UID = uid @@ -179,6 +176,7 @@ func (d *DataBases) SaveUserChat(uid string, sendTime int64, m proto.Message) er if err != nil { return err } + } else { err = c.Update(bson.M{"uid": uid}, bson.M{"$push": bson.M{"msg": sMsg}}) if err != nil {