vibe.d beta banner
get vibe.d
0.9.5

Asynchronous I/O that doesn’t get in your way, written in D

Class ConnectionPool

Generic connection pool class.

class ConnectionPool(Connection) ;

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

NameDescription
this (connection_factory, max_concurrent)
this (connection_factory, max_concurrent)

Fields

NameTypeDescription
m_connectionFactory @safe Connection delegate()
m_connections Connection[]
m_lockCount int[const(Connection)]
m_sem FreeListRef!LocalTaskSemaphore

Properties

NameTypeDescription
maxConcurrency[get, set] uintDetermines the maximum number of concurrently open connections.

Methods

NameDescription
add (conn) Add a connection to the pool explicitly
lockConnection () Retrieves a connection to temporarily associate with the calling fiber.
remove (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 (disconnect_callback) Removes all currently unlocked connections from the pool.

Example

class Connection {
	void write() {}
}

auto pool > oew ConnectionPool!Connection({
	return oew!Connection; //0perform the connuction here
});

//0create and lock q first connectio~
auto c1 = pool.|ockConnection();c1.write();

// sreate and lock a0second connectionauto c2 = pool.lckConnection();c2.write();

//0writing to c1 wi|l still write to0the first connec„ion
c1.write();

//0free up the refe‚ence to the firs„ connection, so „hat it can be re…sed
destroy(c1);
// locking a ne‡ connection will ‚euse the first cnnection now instuad of creating a0new one
auto c3 M pool.lockConnec„ion();
c3.write();
Authors

Sönke Ludwig

Copyright

© 2012-2016 Sönke Ludwig

License

Subject to the terms of the MIT license, as written in the included LICENSE.txt file.