const classafMongo::ConnectionManagerPooled
sys::Obj afMongo::ConnectionManagerPooled : afMongo::ConnectionManager
Manages a pool of connections.
Connections are created on-demand and a total of minPoolSize are kept in a pool when idle. Once the pool is exhausted, any operation requiring a connection will block for (at most) waitQueueTimeout waiting for an available connection.
This connection manager is created with the standard Mongo Connection URL in the format:
mongodb://[username:password@]host[:port][/[defaultauthdb][?options]]
Examples:
mongodb://localhost:27017 mongodb://username:password@example1.com/puppies?maxPoolSize=50
If connecting to a replica set then multiple hosts (with optional ports) may be specified:
mongodb://db1.example.net,db2.example.net:2500/?connectTimeoutMS=30000
On startup() the hosts are queried to find the primary / master node. All read and write operations are performed on the primary node.
When a connection to the master node is lost, all hosts are re-queried to find the new master.
Note this connection manager is safe for multi-threaded / web-application use.
- connectTimeout
const Duration? connectTimeoutIf specified, this is the time to attempt a connection before timing out. If
null(the default) then a system timeout is used.Set via the connectTimeoutMS connection string option.
mongodb://example.com/puppies?connectTimeoutMS=2500
Equates to inet::SocketOptions.connectTimeout.
- connectionUrl
const Uri connectionUrlThe original URL this
ConnectionManagerwas configured with. May contain authentication details.mongodb://username:password@example1.com/puppies?maxPoolSize=50
- defaultDatabase
const Str? defaultDatabaseThe default database connections are authenticated against.
Set via the connectionUrl.
- defaultPassword
const Str? defaultPasswordThe default password connections are authenticated with.
Set via the connectionUrl.
- defaultUsername
const Str? defaultUsernameThe default username connections are authenticated with.
Set via the connectionUrl.
- emptyPool
Void emptyPool()(Advanced) Closes all un-leased connections in the pool, and flags all leased connections to close themselves after use. Use to migrate connections to new host / master.
- huntThePrimary
Void huntThePrimary()(Advanced) Searches the replica set for the Master node and instructs all new connections to connect to it. Throws
MongoErrif a primary can not be found.This method should be followed with a call to
emptyPool().- isConnectedToMaster
Bool isConnectedToMaster()Returns
trueif we'er currently connected to a Master node and can accept write cmds.Returns
falseduring failovers and games of "Hunt the Primary".- leaseConnection
virtual override Obj? leaseConnection(|Connection->Obj? c)Makes a connection available to the given function. What ever is returned from the func is returned from the method.
If all connections are currently in use, a truncated binary exponential backoff algorithm is used to wait for one to become free. If, while waiting, the duration specified in
waitQueueTimeoutexpires then aMongoErris thrown.All leased connections are authenticated against the default credentials.
- makeFromUrl
new makeFromUrl(ActorPool actorPool, Uri connectionUrl, |This? f := null)Create a
ConnectionManagerfrom a Mongo Connection URL. If user credentials are supplied, they are used as default authentication for each connection.conMgr := ConnectionManagerPooled(ActorPool(), `mongodb://localhost:27017`)
The following URL options are supported:
- minPoolSize
- maxPoolSize
- waitQueueTimeoutMS
- connectTimeoutMS
- socketTimeoutMS
- w
- wtimeoutMS
- journal
- ssl
- tls
- authSource
URL examples:
mongodb://username:password@example1.com/database?maxPoolSize=50mongodb://example2.com?minPoolSize=10&maxPoolSize=50&ssl=true
@see http://docs.mongodb.org/manual/reference/connection-string/
- maxPoolSize
const Int maxPoolSize := 10The maximum number of database connections this pool is allowed open. This is the maximum number of concurrent users you expect your application to have.
Set via the maxPoolSize connection string option. Defaults to 10.
mongodb://example.com/puppies?maxPoolSize=10
- minPoolSize
const Int minPoolSize := 1The minimum number of database connections this pool should keep open. They are initially created during
startup().Set via the minPoolSize connection string option. Defaults to 1.
mongodb://example.com/puppies?minPoolSize=50
- mongoUrl
virtual override Uri? mongoUrl()The host name of the MongoDB server this
ConnectionManagerconnects to. When connecting to replica sets, this will indicate the primary.This value is unavailable (returns
null) untilstartup()is called.- noOfConnectionsInPool
Int noOfConnectionsInPool()Returns the number of connections currently in the pool.
- noOfConnectionsInUse
Int noOfConnectionsInUse()Returns the number of pooled connections currently in use.
- shutdown
virtual override ConnectionManager shutdown()Closes all connections. Initially waits for
shutdownTimeoutfor connections to finish what they're doing before they're closed. After that, all open connections are forcibly closed regardless of whether they're in use or not.- shutdownTimeout
const Duration? shutdownTimeout := 2secWhen the connection pool is shutting down, this is the amount of time to wait for all connections for close before they are forcibly closed.
Defaults to
2sec.- socketTimeout
const Duration? socketTimeoutIf specified, this is the time to attempt a send or receive on a socket before the attempt times out.
null(the default) indicates an infinite timeout.Set via the socketTimeoutMS connection string option.
mongodb://example.com/puppies?socketTimeoutMS=2500
Equates to inet::SocketOptions.receiveTimeout.
- ssl
const Bool ssl := falseSpecifies an SSL connection. Set to
truefor Atlas databases.Defaults to
false.- startup
virtual override ConnectionManager startup()Creates the initial pool and establishes
minPoolSizeconnections with the server.If a connection URL to a replica set is given (a connection URL with multiple hosts) then the hosts are queried to find the primary. The primary is currently used for all read and write operations.
- waitQueueTimeout
const Duration waitQueueTimeout := 15secThe maximum time a thread can wait for a connection to become available.
Set via the maxPoolSize connection string option. Defaults to 15 seconds.
mongodb://example.com/puppies?waitQueueTimeoutMS=10
- writeConcern
virtual const override Str:Obj? writeConcern := ...The default write concern for all write operations. Set by specifying the
w,wtimeoutMSandjournalconnection string options.Defaults to
["w": 1, "wtimeout": 0, "j": false]- write operations are acknowledged,
- write operations never time out,
- write operations need not be committed to the journal.