tianxiaoliang commented on a change in pull request #735:
URL: 
https://github.com/apache/servicecomb-service-center/pull/735#discussion_r517133042



##########
File path: datasource/mongo/client/mongo.go
##########
@@ -0,0 +1,232 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package client
+
+import (
+       "context"
+       "errors"
+       "github.com/apache/servicecomb-service-center/pkg/gopool"
+       "github.com/apache/servicecomb-service-center/pkg/log"
+       "github.com/apache/servicecomb-service-center/server/config"
+       "github.com/go-chassis/go-chassis/v2/storage"
+       "go.mongodb.org/mongo-driver/mongo"
+       "go.mongodb.org/mongo-driver/mongo/options"
+       "time"
+)
+
+const (
+       MongoDB             = "servicecenter"
+       MongoCheckDelay     = 2 * time.Second
+       HeathChekRetryTimes = 3
+)
+
+var (
+       mc *MongoClient
+)
+
+type MongoClient struct {
+       client      *mongo.Client
+       collections map[string]*mongo.Collection
+       db          *mongo.Database
+       dbconfig    storage.DB
+
+       err       chan error
+       ready     chan struct{}
+       goroutine *gopool.Pool
+}
+
+func GetMongoClient() *MongoClient {
+       return mc
+}
+
+func NewMongoClient() {
+       inst := &MongoClient{}
+       if err := inst.Initialize(); err != nil {
+               inst.err <- err
+       }
+       mc = inst
+}
+
+func (mc *MongoClient) Initialize() (err error) {
+       mc.err = make(chan error, 1)
+       mc.ready = make(chan struct{})
+       mc.goroutine = gopool.New(context.Background())
+       mc.dbconfig.URI = config.GetString("registry.mongo.endpoints", "", nil)
+       err = mc.newClient(context.Background())
+       if err != nil {
+               return
+       }
+       mc.StartHealthCheck()
+       close(mc.ready)
+       return nil
+}
+
+func (mc *MongoClient) Err() <-chan error {
+       return mc.err
+}
+
+func (mc *MongoClient) Ready() <-chan struct{} {
+       return mc.ready
+}
+
+func (mc *MongoClient) Close() {
+       if mc.client != nil {
+               if err := mc.client.Disconnect(context.TODO()); err != nil {
+                       log.Errorf(err, "[close mongo client] failed disconnect 
the mongo client")
+               }
+       }
+}
+
+func (mc *MongoClient) StartHealthCheck() {
+       mc.goroutine.Do(mc.HealthCheck)
+}
+
+func (mc *MongoClient) HealthCheck(ctx context.Context) {
+       for {
+               select {
+               case <-ctx.Done():
+                       mc.Close()
+                       return
+               case <-time.After(MongoCheckDelay):
+                       for i := 0; i < HeathChekRetryTimes; i++ {
+                               err := mc.client.Ping(context.Background(), nil)
+                               if err == nil {
+                                       break
+                               }
+                               log.Errorf(err, "retry to connect to mongodb %s 
after %s", mc.dbconfig.URI, MongoCheckDelay)
+                               select {
+                               case <-ctx.Done():
+                                       mc.Close()
+                                       return
+                               case <-time.After(MongoCheckDelay):
+                               }
+                       }
+               }
+       }
+}
+
+func (mc *MongoClient) newClient(ctx context.Context) (err error) {
+       clientOptions := options.Client().ApplyURI("")

Review comment:
       ApplyURI 参数空的?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to