【问题标题】:How can I convert my mgo sessions to mongo-go-driver clients using connection pooling?如何使用连接池将我的 mgo 会话转换为 mongo-go-driver 客户端?
【发布时间】:2020-01-19 17:50:58
【问题描述】:

很久很久以前,当我们使用 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 - 但我在想,如果在该连接上发生写入,它永远不会返回从从属设备读取。
  • 看起来我*可以显式创建会话,但我不确定我是否应该或者是否对在新驱动程序中显式管理这些会话有任何影响。

【问题讨论】:

    标签: mongodb go mgo mongo-go


    【解决方案1】:

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

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

    在 mongo-go-driver 中,当您调用执行查询时,会话会自动在后台自动处理(例如 All())。你可以*显式地创建和拆除一个会话,但你不能使用我上面提出的单例方法来使用它,而不必更改所有调用者函数。 这是因为您不能再对会话调用查询操作,而必须在 DB 操作本身使用 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)}
    }
    

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

    【讨论】:

    • 需要注意的一点:mongo-go 的 session 和 mgo 的 session 不是同一个概念。 mgo 的Session 大致相当于 mongo-go 的Client。在 mongo-go 中,会话用于对可能在单个事务中执行的一系列操作进行分组(mgo 不支持)。
    猜你喜欢
    • 1970-01-01
    • 2014-06-07
    • 2019-07-13
    • 2021-08-03
    • 2018-07-04
    • 1970-01-01
    • 2019-05-21
    • 2019-08-05
    • 2017-08-04
    相关资源
    最近更新 更多