|
|
@ -42,9 +42,9 @@ type Mongo struct {
|
|
|
|
// NewMongo Initialize MongoDB connection
|
|
|
|
// NewMongo Initialize MongoDB connection
|
|
|
|
func NewMongo() (*Mongo, error) {
|
|
|
|
func NewMongo() (*Mongo, error) {
|
|
|
|
specialerror.AddReplace(mongo.ErrNoDocuments, errs.ErrRecordNotFound)
|
|
|
|
specialerror.AddReplace(mongo.ErrNoDocuments, errs.ErrRecordNotFound)
|
|
|
|
url := "mongodb://sample.host:27017/?maxPoolSize=20&w=majority"
|
|
|
|
uri := "mongodb://sample.host:27017/?maxPoolSize=20&w=majority"
|
|
|
|
if config.Config.Mongo.Uri != "" {
|
|
|
|
if config.Config.Mongo.Uri != "" {
|
|
|
|
url = config.Config.Mongo.Uri
|
|
|
|
uri = config.Config.Mongo.Uri
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
mongodbHosts := ""
|
|
|
|
mongodbHosts := ""
|
|
|
|
for i, v := range config.Config.Mongo.Address {
|
|
|
|
for i, v := range config.Config.Mongo.Address {
|
|
|
@ -55,22 +55,22 @@ func NewMongo() (*Mongo, error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if config.Config.Mongo.Password != "" && config.Config.Mongo.Username != "" {
|
|
|
|
if config.Config.Mongo.Password != "" && config.Config.Mongo.Username != "" {
|
|
|
|
url = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d&authSource=admin",
|
|
|
|
uri = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d&authSource=admin",
|
|
|
|
config.Config.Mongo.Username, config.Config.Mongo.Password, mongodbHosts,
|
|
|
|
config.Config.Mongo.Username, config.Config.Mongo.Password, mongodbHosts,
|
|
|
|
config.Config.Mongo.Database, config.Config.Mongo.MaxPoolSize)
|
|
|
|
config.Config.Mongo.Database, config.Config.Mongo.MaxPoolSize)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
url = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d&authSource=admin",
|
|
|
|
uri = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d&authSource=admin",
|
|
|
|
mongodbHosts, config.Config.Mongo.Database,
|
|
|
|
mongodbHosts, config.Config.Mongo.Database,
|
|
|
|
config.Config.Mongo.MaxPoolSize)
|
|
|
|
config.Config.Mongo.MaxPoolSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println("mongo:", url)
|
|
|
|
fmt.Println("mongo:", uri)
|
|
|
|
var mongoClient *mongo.Client
|
|
|
|
var mongoClient *mongo.Client
|
|
|
|
var err error = nil
|
|
|
|
var err error = nil
|
|
|
|
for i := 0; i <= maxRetry; i++ {
|
|
|
|
for i := 0; i <= maxRetry; i++ {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
|
|
|
defer cancel()
|
|
|
|
defer cancel()
|
|
|
|
mongoClient, err = mongo.Connect(ctx, options.Client().ApplyURI(url))
|
|
|
|
mongoClient, err = mongo.Connect(ctx, options.Client().ApplyURI(uri))
|
|
|
|
if err == nil {
|
|
|
|
if err == nil {
|
|
|
|
return &Mongo{db: mongoClient}, nil
|
|
|
|
return &Mongo{db: mongoClient}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|