Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[improve] add physicalAddress as part of connection pool key #411

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ClientConnection.cc
Original file line number Diff line number Diff line change
@@ -1321,7 +1321,7 @@ void ClientConnection::close(Result result, bool detach) {
}
// Remove the connection from the pool before completing any promise
if (detach) {
pool_.remove(logicalAddress_ + "-" + std::to_string(poolIndex_), this);
pool_.remove(logicalAddress_, physicalAddress_, poolIndex_, this);
}

auto self = shared_from_this();
15 changes: 11 additions & 4 deletions lib/ConnectionPool.cc
Original file line number Diff line number Diff line change
@@ -66,6 +66,13 @@ bool ConnectionPool::close() {
return true;
}

static const std::string getKey(const std::string& logicalAddress, const std::string& physicalAddress,
size_t keySuffix) {
std::stringstream ss;
ss << logicalAddress << '-' << physicalAddress << '-' << keySuffix;
return ss.str();
}

Future<Result, ClientConnectionWeakPtr> ConnectionPool::getConnectionAsync(const std::string& logicalAddress,
const std::string& physicalAddress,
size_t keySuffix) {
@@ -77,9 +84,7 @@ Future<Result, ClientConnectionWeakPtr> ConnectionPool::getConnectionAsync(const

std::unique_lock<std::recursive_mutex> lock(mutex_);

std::stringstream ss;
ss << logicalAddress << '-' << keySuffix;
const std::string key = ss.str();
auto key = getKey(logicalAddress, physicalAddress, keySuffix);

PoolMap::iterator cnxIt = pool_.find(key);
if (cnxIt != pool_.end()) {
@@ -127,7 +132,9 @@ Future<Result, ClientConnectionWeakPtr> ConnectionPool::getConnectionAsync(const
return future;
}

void ConnectionPool::remove(const std::string& key, ClientConnection* value) {
void ConnectionPool::remove(const std::string& logicalAddress, const std::string& physicalAddress,
size_t keySuffix, ClientConnection* value) {
auto key = getKey(logicalAddress, physicalAddress, keySuffix);
std::lock_guard<std::recursive_mutex> lock(mutex_);
auto it = pool_.find(key);
if (it != pool_.end() && it->second.get() == value) {
3 changes: 2 additions & 1 deletion lib/ConnectionPool.h
Original file line number Diff line number Diff line change
@@ -51,7 +51,8 @@ class PULSAR_PUBLIC ConnectionPool {
*/
bool close();

void remove(const std::string& key, ClientConnection* value);
void remove(const std::string& logicalAddress, const std::string& physicalAddress, size_t keySuffix,
ClientConnection* value);

/**
* Get a connection from the pool.