ConnectionPool

Generic connection pool class.

The connection pool is creating connections using the supplied factory function as needed whenever lockConnection is called. Connections are associated to the calling fiber, as long as any copy of the returned LockedConnection object still exists. Connections that are not associated to any fiber will be kept in a pool of open connections for later reuse.

Note that, after retrieving a connection with lockConnection, the caller has to make sure that the connection is actually physically open and to reopen it if necessary. The ConnectionPool class has no knowledge of the internals of the connection objects.

Constructors

this
this(Connection delegate() @(safe) connection_factory, uint max_concurrent)
Undocumented in source.
this
deprecated this(Connection delegate() connection_factory, uint max_concurrent)
Undocumented in source.

Members

Functions

add
bool add(Connection conn)

Add a connection to the pool explicitly

lockConnection
LockedConnection!Connection lockConnection()

Retrieves a connection to temporarily associate with the calling fiber.

remove
void remove(Connection conn)

Removes an existing connection from the pool It can be called with a locked connection, same connection can be added back to the pool anytime. Any fibers that hold a lock on this connection will keep behaving as expected.

removeUnused
void removeUnused(void delegate(Connection conn) @(safe) nothrow disconnect_callback)

Removes all currently unlocked connections from the pool.

Properties

maxConcurrency
uint maxConcurrency [@property setter]
uint maxConcurrency [@property getter]

Determines the maximum number of concurrently open connections.

Examples

class Connection {
	void write() {}
}

auto pool = new ConnectionPool!Connection({
	return new Connection; // perform the connection here
});

// create and lock a first connection
auto c1 = pool.lockConnection();
c1.write();

// create and lock a second connection
auto c2 = pool.lockConnection();
c2.write();

// writing to c1 will still write to the first connection
c1.write();

// free up the reference to the first connection, so that it can be reused
destroy(c1);

// locking a new connection will reuse the first connection now instead of creating a new one
auto c3 = pool.lockConnection();
c3.write();

Meta