You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ti...@apache.org on 2021/02/05 02:57:52 UTC
[servicecomb-kie] branch master updated: retry once if EureDB fails
to avoid netword occassionally failure (#171)
This is an automated email from the ASF dual-hosted git repository.
tianxiaoliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-kie.git
The following commit(s) were added to refs/heads/master by this push:
new 98ea6a3 retry once if EureDB fails to avoid netword occassionally failure (#171)
98ea6a3 is described below
commit 98ea6a346b08782492f47aee4b3e693e74c8f5aa
Author: Shawn <xi...@gmail.com>
AuthorDate: Fri Feb 5 10:57:42 2021 +0800
retry once if EureDB fails to avoid netword occassionally failure (#171)
---
server/service/mongo/session/session.go | 25 +++++++++++++++++++++----
1 file changed, 21 insertions(+), 4 deletions(-)
diff --git a/server/service/mongo/session/session.go b/server/service/mongo/session/session.go
index 4d1378b..4f85031 100644
--- a/server/service/mongo/session/session.go
+++ b/server/service/mongo/session/session.go
@@ -200,10 +200,7 @@ func GetColInfo(ctx context.Context, name string) (*CollectionInfo, error) {
//EnsureDB build mongo db schema
func EnsureDB() {
- session, err := mgo.Dial(config.GetDB().URI)
- if err != nil {
- openlog.Fatal("can not dial db:" + err.Error())
- }
+ session := OpenSession()
defer session.Close()
session.SetMode(mgo.Monotonic, true)
@@ -218,6 +215,26 @@ func EnsureDB() {
ensureKVLongPolling(session)
}
+func OpenSession() *mgo.Session {
+ var timeout time.Duration
+ var err error
+ if config.GetDB().Timeout != "" {
+ timeout, err = time.ParseDuration(config.GetDB().Timeout)
+ if err != nil {
+ openlog.Fatal("invalid timeout :" + err.Error())
+ }
+ }
+ session, err := mgo.DialWithTimeout(config.GetDB().URI, timeout)
+ if err != nil {
+ openlog.Warn("can not dial db, retry once:" + err.Error())
+ session, err = mgo.DialWithTimeout(config.GetDB().URI, timeout)
+ if err != nil {
+ openlog.Fatal("can not dial db:" + err.Error())
+ }
+ }
+ return session
+}
+
func ensureKVLongPolling(session *mgo.Session) {
c := session.DB(DBName).C(CollectionPollingDetail)
err := c.Create(&mgo.CollectionInfo{Validator: bson.M{