当使用 golang 连接到 mongodb 实例时,我们可以指定 maxPoolSize
的值通过 connection string或使用 ClientOptions.SetMaxPoolSize
来自 mongo-driver
包裹。
这意味着初始化连接的代码片段可能如下所示:
opts := options.Client().ApplyURI(mongodb://foor:bar@localhost:27017/test?maxPoolSize=123)
opts.SetMaxPoolSize(234)
client, err := mongo.NewClient(opts)
鉴于上述情况,我的问题是 maxPoolSize
中的哪一个规范优先,是连接字符串中的那个还是通过驱动程序指定的那个?
最佳答案
借助 D. SM 的提示我能够找到文档中指定的位置。 mongo-driver
的优先规则可以在函数的 godoc 中找到 ClientOptions.ApplyURI
:
If the connection string contains any options that have previously been set, it will overwrite them. Options that correspond to multiple URI parameters, such as WriteConcern, will be completely overwritten if any of the query parameters are specified. If an option is set on ClientOptions after this method is called, that option will override any option applied via the connection string.
这意味着最后指定的内容优先。所以在上述情况下
maxPoolSize
通过驱动程序设置的将是客户端使用的那个。
关于mongodb - 带有 golang mongo-driver 的多个 maxPoolSize 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63943152/