优化mgo驱动库默认配置项
This commit is contained in:
parent
3bf2feba6b
commit
b0a5b7ffe9
@ -3,7 +3,6 @@ package mgo
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
|
||||||
|
|
||||||
"go_dreamfactory/lego/core"
|
"go_dreamfactory/lego/core"
|
||||||
|
|
||||||
@ -12,7 +11,6 @@ import (
|
|||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
"go.mongodb.org/mongo-driver/mongo/readconcern"
|
"go.mongodb.org/mongo-driver/mongo/readconcern"
|
||||||
"go.mongodb.org/mongo-driver/mongo/readpref"
|
"go.mongodb.org/mongo-driver/mongo/readpref"
|
||||||
"go.mongodb.org/mongo-driver/mongo/writeconcern"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func newSys(options Options) (sys *Mongodb, err error) {
|
func newSys(options Options) (sys *Mongodb, err error) {
|
||||||
@ -28,20 +26,20 @@ type Mongodb struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *Mongodb) init() (err error) {
|
func (this *Mongodb) init() (err error) {
|
||||||
want, err := readpref.New(readpref.SecondaryMode) //表示只使用辅助节点
|
// want, err := readpref.New(readpref.SecondaryMode) //表示只使用辅助节点
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return fmt.Errorf("数据库设置辅助节点 err=%s", err.Error())
|
// return fmt.Errorf("数据库设置辅助节点 err=%s", err.Error())
|
||||||
}
|
// }
|
||||||
wc := writeconcern.New(writeconcern.WMajority())
|
// wc := writeconcern.New(writeconcern.W(1))
|
||||||
readconcern.Majority()
|
readconcern.Majority()
|
||||||
//链接mongo服务
|
//链接mongo服务
|
||||||
opt := options.Client().ApplyURI(this.options.MongodbUrl)
|
opt := options.Client().ApplyURI(this.options.MongodbUrl)
|
||||||
opt.SetLocalThreshold(3 * time.Second) //只使用与mongo操作耗时小于3秒的
|
// opt.SetLocalThreshold(3 * time.Second) //只使用与mongo操作耗时小于3秒的
|
||||||
opt.SetMaxConnIdleTime(5 * time.Second) //指定连接可以保持空闲的最大毫秒数
|
// opt.SetMaxConnIdleTime(5 * time.Second) //指定连接可以保持空闲的最大毫秒数
|
||||||
opt.SetMaxPoolSize(this.options.MaxPoolSize) //使用最大的连接数
|
opt.SetMaxPoolSize(this.options.MaxPoolSize) //使用最大的连接数
|
||||||
opt.SetReadPreference(want) //表示只使用辅助节点
|
// opt.SetReadPreference(want) //表示只使用辅助节点
|
||||||
opt.SetReadConcern(readconcern.Majority()) //指定查询应返回实例的最新数据确认为,已写入副本集中的大多数成员
|
// opt.SetReadConcern(readconcern.Majority()) //指定查询应返回实例的最新数据确认为,已写入副本集中的大多数成员
|
||||||
opt.SetWriteConcern(wc) //请求确认写操作传播到大多数mongod实例
|
// opt.SetWriteConcern(wc) //请求确认写操作传播到大多数mongod实例
|
||||||
if client, err := mongo.Connect(this.getContext(), opt); err != nil {
|
if client, err := mongo.Connect(this.getContext(), opt); err != nil {
|
||||||
return fmt.Errorf("连接数据库错误 err=%s", err.Error())
|
return fmt.Errorf("连接数据库错误 err=%s", err.Error())
|
||||||
} else {
|
} else {
|
||||||
|
@ -40,7 +40,7 @@ func SetTimeOut(v time.Duration) Option {
|
|||||||
|
|
||||||
func newOptions(config map[string]interface{}, opts ...Option) Options {
|
func newOptions(config map[string]interface{}, opts ...Option) Options {
|
||||||
options := Options{
|
options := Options{
|
||||||
MaxPoolSize: 1000,
|
MaxPoolSize: 100,
|
||||||
TimeOut: time.Second * 3,
|
TimeOut: time.Second * 3,
|
||||||
}
|
}
|
||||||
if config != nil {
|
if config != nil {
|
||||||
@ -54,7 +54,7 @@ func newOptions(config map[string]interface{}, opts ...Option) Options {
|
|||||||
|
|
||||||
func newOptionsByOption(opts ...Option) Options {
|
func newOptionsByOption(opts ...Option) Options {
|
||||||
options := Options{
|
options := Options{
|
||||||
MaxPoolSize: 1000,
|
MaxPoolSize: 100,
|
||||||
TimeOut: time.Second * 3,
|
TimeOut: time.Second * 3,
|
||||||
}
|
}
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
|
Loading…
Reference in New Issue
Block a user