Skip to content

client如何设置SetMaxConnIdleTime? #335

Open

Description

@beyondyyh
var (
	mgoClient     *qmgo.Client
	mgoClientOnce sync.Once
)

func mgoConnConfig() *qmgo.Config {
	cfg := "定义好的配置文件解析之后的struct"
	// logger.Infof("mgo cfg raw:%+v", cfg)
	mode, err := readpref.ModeFromString(cfg.ReadPref.Mode)
	if err != nil {
		log.Panicf("Init MgoConnConfig failed: %+v", err)
	}
	config := &qmgo.Config{
		// URI example: [mongodb://][user:pass@]host1[:port1][,host2[:port2],...][/database][?options]
		Uri:              fmt.Sprintf("mongodb://%s:%s@%s", cfg.User, cfg.Password, strings.Join(cfg.ServerNodes, ",")),
		Database:         cfg.Database,
		ConnectTimeoutMS: &cfg.ConnectTimeoutMS,
		SocketTimeoutMS:  &cfg.SocketTimeoutMS,
		MaxPoolSize:      &cfg.MaxPoolSize,
		MinPoolSize:      &cfg.MinPoolSize,
		ReadPreference: &qmgo.ReadPref{
			Mode: mode,
		},
		Auth: &qmgo.Credential{
			AuthSource: cfg.Auth.AuthSource,
		},
	}
	return config
}

func mgoLazyinit() {
	stringfy := func(evt *event.PoolEvent) string {
		var opts string
		if evt.PoolOptions != nil {
			buf, _ := json.Marshal(evt.PoolOptions)
			opts = string(buf)
		}
		return fmt.Sprintf("addr:%s, opts:%s, reason:%s", evt.Address, opts, evt.Reason)
	}
	mgoClientOnce.Do(func() {
		logger.Info("lazyinit mgoClient")
		poolMonitor := &event.PoolMonitor{
			Event: func(evt *event.PoolEvent) {
				switch evt.Type {
				case event.PoolCreated:
					logger.Infof("mgoClient: PoolCreated, %s", stringfy(evt))
				case event.PoolReady:
					logger.Infof("mgoClient: PoolReady, %s", stringfy(evt))
				case event.PoolCleared:
					logger.Infof("mgoClient: PoolCleared, %s", stringfy(evt))
				case event.GetSucceeded:
					logger.Infof("mgoClient: GetSucceeded, %s", stringfy(evt))
				case event.GetFailed:
					logger.Errorf("mgoClient: GetFailed, %s", stringfy(evt))
				case event.ConnectionClosed:
					logger.Infof("mgoClient: ConnectionClosed, %s", stringfy(evt))
				case event.PoolClosedEvent:
					logger.Warningf("mgoClient: PoolClosedEvent, %s", stringfy(evt))
				}
			},
		}
		clientOpt := options.ClientOptions{
			ClientOptions: mgoDriverOptions.Client().
				// SetMaxConnIdleTime specifies the maximum amount of time that a connection will remain idle in a connection pool before it is removed from the pool and closed.
				SetMaxConnIdleTime(time.Millisecond * time.Duration(1000)).
				// SetPoolMonitor specifies a PoolMonitor to receive connection pool events.
				SetPoolMonitor(poolMonitor),
		}
		var err error
		mgoClient, err = qmgo.NewClient(context.Background(), mgoConnConfig(), clientOpt)
		if err != nil {
			logger.Errorf("lazyinit qmgo.NewClient error: %v", err)
			log.Panicf("lazyinit qmgo.NewClient error: %v", err)
		}
	})
}

func GetMgoCollection(tableName string) *qmgo.Collection {
	mgoLazyinit()
	return mgoClient.Database("db").Collection(tableName)
}```

SetMaxConnIdleTime 在这里并不生效,请问是为啥?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions