Commit c9463758 authored by Vaastav Anand's avatar Vaastav Anand

Add instrumentation for ClientPool

parent 01ddee03
......@@ -8,6 +8,7 @@
#include <chrono>
#include <string>
#include <xtrace/xtrace.h>
#include "logger.h"
namespace media_service {
......@@ -71,27 +72,36 @@ ClientPool<TClient>::~ClientPool() {
template<class TClient>
TClient * ClientPool<TClient>::Pop() {
XTRACE("Popping client from client pool", {{"clientType" , _client_type}});
TClient * client = nullptr;
XTRACE("Obtaining lock on client pool mutex");
std::unique_lock<std::mutex> cv_lock(_mtx); {
XTRACE("Obtained lock on client pool mutex");
while (_pool.size() == 0) {
// Create a new a client if current pool size is less than
// the max pool size.
if (_curr_pool_size < _max_pool_size) {
try {
XTRACE("Creating a new client");
client = new TClient(_addr, _port);
_curr_pool_size++;
break;
} catch (...) {
XTRACE("Failed to create a new client");
XTRACE("Releasing lock on client pool mutex");
cv_lock.unlock();
return nullptr;
}
} else {
XTRACE("Waiting until a client becomes available to use");
auto wait_time = std::chrono::system_clock::now() +
std::chrono::milliseconds(_timeout_ms);
bool wait_success = _cv.wait_until(cv_lock, wait_time,
[this] { return _pool.size() > 0; });
if (!wait_success) {
XTRACE("ClientPool pop timeout");
LOG(warning) << "ClientPool pop timeout";
XTRACE("Releasing lock on client pool mutex");
cv_lock.unlock();
return nullptr;
}
......@@ -103,13 +113,16 @@ TClient * ClientPool<TClient>::Pop() {
}
} // cv_lock(_mtx)
XTRACE("Releasing lock on client pool mutex");
cv_lock.unlock();
if (client) {
try {
XTRACE("Connecting to client");
client->Connect();
} catch (...) {
LOG(error) << "Failed to connect " + _client_type;
XTRACE("Failed to connect to client");
_pool.push_back(client);
throw;
}
......@@ -119,31 +132,45 @@ TClient * ClientPool<TClient>::Pop() {
template<class TClient>
void ClientPool<TClient>::Push(TClient *client) {
XTRACE("Pushing a client into client pool", {{"clientType", _client_type}});
XTRACE("Acquiring lock on mutex");
std::unique_lock<std::mutex> cv_lock(_mtx);
XTRACE("Acquired lock on mutex");
client->KeepAlive();
XTRACE("Pushing client back into client pool");
_pool.push_back(client);
XTRACE("Releasing lock on mutex");
cv_lock.unlock();
_cv.notify_one();
}
template<class TClient>
void ClientPool<TClient>::Push(TClient *client, int timeout_ms) {
XTRACE("Pushing a client into client pool", {{"clientType", _client_type}});
XTRACE("Acquiring lock on mutex");
std::unique_lock<std::mutex> cv_lock(_mtx);
XTRACE("Acquired lock on mutex");
client->KeepAlive(timeout_ms);
XTRACE("Pushing client back into client pool");
_pool.push_back(client);
XTRACE("Releasing lock on mutex");
cv_lock.unlock();
_cv.notify_one();
}
template<class TClient>
void ClientPool<TClient>::Remove(TClient *client) {
XTRACE("Removing a client from client pool", {{"clientType", _client_type}});
XTRACE("Acquiring lock on mutex");
std::unique_lock<std::mutex> lock(_mtx);
XTRACE("Acquired lock on mutex");
delete client;
_curr_pool_size--;
XTRACE("Releasing lock on mutex");
lock.unlock();
}
} // namespace media_service
#endif //MEDIA_MICROSERVICES_CLIENTPOOL_H
\ No newline at end of file
#endif //MEDIA_MICROSERVICES_CLIENTPOOL_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment