Public Member Functions | Protected Member Functions

GridClientConnectionPool Class Reference

#include <gridclientconnectionpool.hpp>

Inheritance diagram for GridClientConnectionPool:
GridClientRecurringEventThread::EventListener

List of all members.

Public Member Functions

 GridClientConnectionPool (const GridClientConfiguration &config)
virtual ~GridClientConnectionPool ()
std::shared_ptr
< GridClientTcpConnection > 
rentTcpConnection (const std::string &host, int port)
std::shared_ptr
< GridClientHttpConnection > 
rentHttpConnection (const std::string &host, int port)
void turnBack (const TConnectionPtr &conn)
void steal (const TConnectionPtr &conn)
void close ()
size_t getAvailableConnectionsCount () const
size_t getAllConnectionsCount () const
template<class T >
std::shared_ptr< T > availableConnection (const string &host, int port)

Protected Member Functions

template<class T >
std::shared_ptr< T > availableConnection (const std::string &host, int port)
void authenticateHttpConnection (const std::string &host, int port, std::shared_ptr< GridClientHttpConnection > conn)
void trackConnection (const TConnectionPtr &connection)
void onTimerEvent ()
void removeConnection (const TConnectionPtr &connection)

Detailed Description

Connection pool that implements rent/turn-back logic.

When a thread needs a connection, it takes it for rent from this pool. If no such connection is available, it is created and returned.

If connection is available, it is returned to the thread and is no more available to other thread. The thread must then turn back this connection, so that other threads can use it.

Thus, several connections to the same endpoint may be created by this pool, if several threads need to communicate with this endpoint simultaneously.


Constructor & Destructor Documentation

GridClientConnectionPool::GridClientConnectionPool ( const GridClientConfiguration &  config  ) 

Constructor.

GridClientConnectionPool::~GridClientConnectionPool (  )  [virtual]

Destructor.


Member Function Documentation

void GridClientConnectionPool::authenticateHttpConnection ( const std::string &  host,
int  port,
std::shared_ptr< GridClientHttpConnection >  conn 
) [protected]

Assigns an existing session token to HTTP connection or authenticates connection if there's no session token yet.

Parameters:
host Host for this connection. port Port for this connection.
conn Shared pointer to HTTP connection.
template<class T >
std::shared_ptr<T> GridClientConnectionPool::availableConnection ( const std::string &  host,
int  port 
) [protected]

Finds an connection, available for rent.

Parameters:
host A host for the connection.
port A port for the connection.
Returns:
A connection (either TCP or HTTP depending on template parameter), that is available for rent.
template<class T >
std::shared_ptr<T> GridClientConnectionPool::availableConnection ( const string &  host,
int  port 
)

Find an existing TCP or HTTP connection to a host.

Returns:
Shared pointer to a connection.
void GridClientConnectionPool::close (  ) 

Closes all connections in this pool and marks this pool as closed.

size_t GridClientConnectionPool::getAllConnectionsCount (  )  const

Returns the total number of connections, currently managed by this pool.

size_t GridClientConnectionPool::getAvailableConnectionsCount (  )  const

Returns the number of connections available for rent.

void GridClientConnectionPool::onTimerEvent (  )  [protected, virtual]

Callback function for connection tracker thread.

Implements GridClientRecurringEventThread::EventListener.

void GridClientConnectionPool::removeConnection ( const TConnectionPtr &  connection  )  [protected]

Utility method, that removes a connection from this pool.

This method is NOT thread-safe.

Parameters:
connection A connection to remove.
std::shared_ptr<GridClientHttpConnection> GridClientConnectionPool::rentHttpConnection ( const std::string &  host,
int  port 
)

Gives an HTTP connection for rent.

If no connection to the given host-port is currently available, a new one is returned.

After no longer needed, the connection should be turned back using turnBack() method.

Parameters:
host A host to connect to.
port A port to connect to.
Returns:
An HTTP connection.
std::shared_ptr< GridClientTcpConnection > GridClientConnectionPool::rentTcpConnection ( const std::string &  host,
int  port 
)

Gives a TCP connection for rent.

If no connection to the given host-port is currently available, a new one is returned.

After no longer needed, the connection should be turned back using turnBack() method.

Parameters:
host A host to connect to.
port A port to connect to.
Returns:
A TCP connection in the connected state (isOpen() returns true).
Exceptions:
GridClientClosedException if this pool is closed; GridServerUnreachableException if the specified endpoint (host-port) is not reachable; GridClientConnectionResetException if the connection cannot be authenticated.
void GridClientConnectionPool::steal ( const TConnectionPtr &  conn  ) 

Removes this connecton from the list of registered connections.

The pool won't be able to access the connection or give it for rent after this method has run.

Parameters:
conn A connection to steal. It should be taken for rent, otherwise the assertion will fail.
void GridClientConnectionPool::trackConnection ( const TConnectionPtr &  connection  )  [protected]

Action, performed by connection tracker for each connection.

Parameters:
connection A connection to process.
void GridClientConnectionPool::turnBack ( const TConnectionPtr &  conn  ) 

Turns a connection back.

After turned back, this connection is available to other threads, until one of the threads takes it for rent.

Parameters:
conn A connection to turn back.

The documentation for this class was generated from the following files:
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends