登录
首页 >  Golang >  Go问答

将 mgo 会话转换为 mongo-go-driver 客户端的连接池使用方法介绍

来源:stackoverflow

时间:2024-03-03 08:00:27 207浏览 收藏

从现在开始,我们要努力学习啦!今天我给大家带来《将 mgo 会话转换为 mongo-go-driver 客户端的连接池使用方法介绍》,感兴趣的朋友请继续看下去吧!下文中的内容我们主要会涉及到等等知识点,如果在阅读本文过程中有遇到不清楚的地方,欢迎留言呀!我们一起讨论,一起学习!

问题内容

很久很久以前,当我们使用 mgo.v2 时,我们创建了一些包装函数来复制会话、设置读取首选项并返回该函数以供其他库使用,例如

func newmonotonicconnection() (conn *connection, success bool) {
    conn := &connection{
        session: basesession.copy(),
    }
    conn.session.setmode(mongo.monotonic, true)

    return conn, true
}

我们现在只需在 init 函数中传递默认客户端(使用 mongo.connect 初始化并传递到连接单例中),然后像这样使用:

func NewMonotonicConnection() (conn *Connection, success bool) {
    conn = defaultConnection
    return conn, true
}

我的理解是,要利用连接池,您需要使用相同的客户端(包含在 defaultconn 中),并且会话现在在 .all()/cursor 拆卸内部隐式处理。如果我这里错了,请纠正我。

如果我们仍然可以在这些连接上设置 readpref(例如,在返回之前在此连接上设置 nearestmode),那就太好了,但是社区/标准的做法是什么?

  • 我知道我可以一遍又一遍地调用 mongo.connect,但是这样贵吗?
  • 我可以创建不同的客户端 - 每个客户端都有不同的 readpref - 但我在想,如果在该连接上发生写入,它永远不会返回从从属设备读取。
  • 看起来我*可以显式创建会话,但我不确定我应该或者在新驱动程序中显式管理这些会话是否有任何影响。

解决方案


我在这个任务中通过 mongo-go-driver 代码库学到了一些东西,我认为在结束这个问题之前我应该​​与世界分享这些东西。如果我错了 - 请纠正我。

如果您想利用连接池,则不应反复调用 connect()。看起来每次调用 connect() 时都会创建一个新套接字。这意味着随着时间的推移,存在套接字耗尽的风险,除非您每次都手动 defer close()-ing 它。

在 mongo-go-driver 中,当您调用执行查询时(例如 all()),会话现在会在幕后自动处理。您可以*显式地创建和拆除会话,但您不能使用我上面提出的单例方法来使用它,而不必更改所有调用方函数。 这是因为您无法再在会话上调用查询操作,而是必须在数据库操作本身使用 withsession 函数来使用它

我意识到 writeconcernreadprefreadconcern 都可以在以下位置设置:

  • 客户端级别(这些是默认值,如果不覆盖,所有内容都会使用)
  • 会话级别
  • 数据库级别
  • 查询级别

所以我所做的是创建数据库选项并重载 *mongo.database 例如:

// Database is a meta-helper that allows us to wrap and overload
// the standard *mongo.Database type
type Database struct {
    *mongo.Database
}

// NewEventualConnection returns a new instantiated Connection
// to the DB using the 'Nearest' read preference.
// Per https://github.com/go-mgo/mgo/blob/v2/session.go#L61
// Eventual is the same as Nearest, but may change servers between reads.
// Nearest: The driver reads from a member whose network latency falls within
// the acceptable latency window. Reads in the nearest mode do not consider
// whether a member is a primary or secondary when routing read operations;
// primaries and secondaries are treated equivalently.
func NewEventualConnection() (conn *Connection, success bool) {
    conn = &Connection{
        client: baseConnection.client,
        dbOptions: options.Database().
            SetReadConcern(readconcern.Local()).
            SetReadPreference(readpref.Nearest()).
            SetWriteConcern(writeconcern.New(
                writeconcern.W(1))),
    }

    return conn, true
}
// GetDB returns an overloaded Database object
func (conn Connection) GetDB(dbname string) *Database {
    dbByName := &Database{conn.client.Database(dbname, conn.dbOptions)}
}

这使我能够利用连接池并保持与我们的代码库的向后兼容性。希望这对其他人有帮助。

理论要掌握,实操不能落!以上关于《将 mgo 会话转换为 mongo-go-driver 客户端的连接池使用方法介绍》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>