Docs Menu
Docs Home
/ / /
Rust Driver
/ /

Connection Pool Monitoring

On this page

  • Overview
  • Event Descriptions
  • Event Monitoring Example
  • Example Event Documents
  • PoolCreatedEvent
  • PoolReadyEvent
  • PoolClearedEvent
  • PoolClosedEvent
  • ConnectionCreatedEvent
  • ConnectionReadyEvent
  • ConnectionClosedEvent
  • ConnectionCheckOutStartedEvent
  • ConnectionCheckoutFailedEvent
  • ConnectionCheckedOutEvent
  • ConnectionCheckedInEvent
  • Additional Information
  • API Documentation

This guide shows you how to use the Rust driver to monitor the driver's connection pool. A connection pool is a set of open Transmission Control Protocol (TCP) connections that your driver maintains with a MongoDB instance. Connection pools help reduce the number of new connections your application needs to create, which might make your application run faster.

You can use information about changes to your connection pool in your application, or you can monitor the connection pool to learn more about how the driver uses connections.

This guide includes the following sections:

  • Event Descriptions describes the connection pool events that the driver can generate.

  • Event Subscription Example provides sample code that shows how to subscribe to connection pool events.

  • Example Event Documents provides samples of each connection pool event.

  • Additional Information provides links to resources and API documentation for types and methods mentioned in this guide.

You can monitor the following connection pool events:

Event Name
Description

Created when a connection pool is created.

Created when a connection pool is ready.

Created when a connection pool is cleared.

Created when a connection pool is closed, before the destruction of the server instance.

Created when a connection is created, but not necessarily when it is used for an operation.

Created after a connection successfully completes a handshake and is ready to be used for operations.

Created when a connection is closed.

Created when an operation attempts to acquire a connection for execution.

Created when an operation cannot acquire a connection for execution.

Created when an operation successfully acquires a connection for execution.

Created when a connection is checked back into the pool after an operation is executed.

You can monitor connection pool events by assigning an EventHandler instance as the value of the cmap_event_handler client option. To construct an EventHandler, which processes all connection pool events, use the callback() or async_callback() method.

The following example connects to a MongoDB deployment, instructs the client to monitor connection pool events, and prints each event:

let mut client_options = ClientOptions::parse("<connection string>").await?;
client_options.cmap_event_handler = Some(EventHandler::callback(|ev| println!("{:?}", ev)));
let client = Client::with_options(client_options)?;
// ... perform actions with the client to generate events

The following sections show sample output for each type of connection pool monitoring event.

PoolCreatedEvent {
address: ...,
options: {...}
}
PoolReadyEvent {
address: ...
}
PoolClearedEvent {
address: ...,
service_id: ...,
}
PoolClosedEvent {
address: ...
}
ConnectionCreatedEvent {
address: ...,
connection_id: 1
}
ConnectionReadyEvent {
address: ...,
connection_id: 1
}
ConnectionClosedEvent {
address: ...,
connection_id: 1,
reason: ...,
/* private fields */
}
ConnectionCheckOutStartedEvent {
address: ...,
}
ConnectionCheckOutFailedEvent {
address: ...,
reason: ...,
/* private fields */
}
ConnectionCheckedOutEvent {
address: ...,
connection_id: 1
}
ConnectionCheckedInEvent {
address: ...,
connection_id: 1
}

To learn more about monitoring a MongoDB deployment, see the How to Monitor MongoDB article.

To learn more about connecting to MongoDB, see the Connection Guide.

To learn more about improving the performance of your application, see the guide on Performance Considerations.

To learn more about the methods and types mentioned in this guide, see the following API documentation:

Back

Command Monitoring