“文档” 菜单
文档首页
/ / /
Go 驱动程序
/ /

连接监控

在此页面上

  • 概述
  • 订阅事件
  • 事件描述
  • 示例事件文档
  • connectionPoolCreated
  • connectionPoolReady
  • connectionPoolCleared
  • connectionPoolClosed
  • connectionCreated
  • connectionReady
  • connectionClosed
  • connectionCheckOutStarted
  • connectionCheckOutFailed
  • connectionCheckedOut
  • connectionCheckedIn
  • 更多信息
  • API 文档

本指南向您展示如何使用 Go 驱动程序来监控驱动程序的连接池。 连接池是驱动程序与 MongoDB 实例维护的一组开放式传输控制协议 (TCP) 连接。 连接池有助于减少应用程序需要创建的新连接数量,这可能会提高应用程序的运行速度。

您可以使用有关应用程序中连接池事件的信息来优化性能或了解客户端生命周期。

您可以通过在应用程序中订阅连接池事件来访问有关连接池事件的详细信息。 以下示例演示了如何通过实例化PoolMonitor并连接到部署来订阅 PoolEvent事件:

var eventArray []*event.PoolEvent
cxnMonitor := &event.PoolMonitor{
Started: func(e *event.PoolEvent) {
eventArray = append(eventArray, e)
},
}
clientOpts := options.Client().ApplyURI(uri).SetPoolMonitor(cxnMonitor)
client, err := mongo.Connect(context.TODO(), clientOpts)

下表描述了驱动程序发出的池事件类型:

池事件类型
说明
ConnectionPoolCreated
在创建连接池时创建。
ConnectionPoolReady
当连接池准备就绪时创建。
ConnectionPoolCleared
当池中的所有连接都关闭时创建。
ConnectionPoolClosed
在销毁服务器实例之前关闭连接池时创建。
ConnectionCreated
在创建连接时创建,但不一定是在用于操作时创建。
ConnectionReady
在连接完成握手并准备好用于操作后创建。
ConnectionClosed
在连接关闭时创建。
ConnectionCheckOutStarted
当操作尝试获取用于执行的连接时创建。
ConnectionCheckOutFailed
当操作无法获取用于执行的连接时创建。
ConnectionCheckedOut
当操作成功获取用于执行的连接时创建。
ConnectionCheckedIn
在执行操作后,当连接被检回连接池时创建。

以下几节显示了每种类型的连接池监控事件的样本输出。

*event.PoolEvent
{
"type": "ConnectionPoolCreated",
"address": "...",
"connectionId": 0,
"options": {
"maxPoolSize": 100,
"minPoolSize": 0,
"maxIdleTimeMS": 0
},
"reason": "",
"serviceId": null,
"error": null
}
*event.PoolEvent
{
"type": "ConnectionPoolReady",
"address": "...",
"connectionId": 0,
"options": null,
"reason": "",
"serviceId": null,
"error": null
}
*event.PoolEvent
{
"type": "ConnectionPoolCleared",
"address": "...",
"connectionId": 0,
"options": null,
"reason": "",
"serviceId": null,
"error": null
}
*event.PoolEvent
{
"type": "ConnectionPoolClosed",
"address": "...",
"connectionId": 0,
"options": null,
"reason": "",
"serviceId": null,
"error": null
}
*event.PoolEvent
{
"type": "ConnectionCreated",
"address": "...",
"connectionId": 1,
"options": null,
"reason": "",
"serviceId": null,
"error": null
}
*event.PoolEvent
{
"type": "ConnectionReady",
"address": "...",
"connectionId": 1,
"options": null,
"reason": "",
"serviceId": null,
"error": null
}
*event.PoolEvent
{
"type": "ConnectionClosed",
"address": "...",
"connectionId": 1,
"options": null,
"reason": "",
"serviceId": null,
"error": null
}
*event.PoolEvent
{
"type": "ConnectionCheckOutStarted",
"address": "...",
"connectionId": 0,
"options": null,
"reason": "",
"serviceId": null,
"error": null
}
*event.PoolEvent
{
"type": "ConnectionCheckOutFailed",
"address": "...",
"connectionId": 0,
"options": null,
"reason": "",
"serviceId": null,
"error": null
}
*event.PoolEvent
{
"type": "ConnectionCheckedOut",
"address": "...",
"connectionId": 1,
"options": null,
"reason": "",
"serviceId": null,
"error": null
}
*event.PoolEvent
{
"type": "ConnectionCheckedIn",
"address": "...",
"connectionId": 1,
"options": null,
"reason": "",
"serviceId": null,
"error": null
}

要了解有关监控 MongoDB 部署的更多信息,请参阅如何监控 MongoDB一文。

要了解有关连接到 MongoDB 的更多信息,请参阅连接指南。

要进一步了解本指南所提及的方法和类型,请参阅以下 API 文档:

← 命令监控
GridFS →