forked from apache/pulsar-client-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionPool.cc
More file actions
134 lines (113 loc) · 4.94 KB
/
Copy pathConnectionPool.cc
File metadata and controls
134 lines (113 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include "ConnectionPool.h"
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl.hpp>
#include "ClientConnection.h"
#include "ExecutorService.h"
#include "LogUtils.h"
using boost::asio::ip::tcp;
namespace ssl = boost::asio::ssl;
typedef ssl::stream<tcp::socket> ssl_socket;
DECLARE_LOG_OBJECT()
namespace pulsar {
ConnectionPool::ConnectionPool(const ClientConfiguration& conf, ExecutorServiceProviderPtr executorProvider,
const AuthenticationPtr& authentication, const std::string& clientVersion)
: clientConfiguration_(conf),
executorProvider_(executorProvider),
authentication_(authentication),
clientVersion_(clientVersion),
randomDistribution_(0, conf.getConnectionsPerBroker() - 1),
randomEngine_(std::chrono::high_resolution_clock::now().time_since_epoch().count()) {}
bool ConnectionPool::close() {
bool expectedState = false;
if (!closed_.compare_exchange_strong(expectedState, true)) {
return false;
}
std::unique_lock<std::recursive_mutex> lock(mutex_);
for (auto cnxIt = pool_.begin(); cnxIt != pool_.end(); cnxIt++) {
auto& cnx = cnxIt->second;
if (cnx) {
// The 2nd argument is false because removing a value during the iteration will cause segfault
cnx->close(ResultDisconnected, false);
}
}
pool_.clear();
return true;
}
Future<Result, ClientConnectionWeakPtr> ConnectionPool::getConnectionAsync(const std::string& logicalAddress,
const std::string& physicalAddress,
size_t keySuffix) {
if (closed_) {
Promise<Result, ClientConnectionWeakPtr> promise;
promise.setFailed(ResultAlreadyClosed);
return promise.getFuture();
}
std::unique_lock<std::recursive_mutex> lock(mutex_);
std::stringstream ss;
ss << logicalAddress << '-' << keySuffix;
const std::string key = ss.str();
PoolMap::iterator cnxIt = pool_.find(key);
if (cnxIt != pool_.end()) {
auto& cnx = cnxIt->second;
if (!cnx->isClosed()) {
// Found a valid or pending connection in the pool
LOG_DEBUG("Got connection from pool for " << key << " use_count: " //
<< (cnx.use_count()) << " @ " << cnx.get());
return cnx->getConnectFuture();
} else {
// The closed connection should have been removed from the pool in ClientConnection::close
LOG_WARN("Deleting stale connection from pool for " << key << " use_count: " << (cnx.use_count())
<< " @ " << cnx.get());
pool_.erase(key);
}
}
// No valid or pending connection found in the pool, creating a new one
ClientConnectionPtr cnx;
try {
cnx.reset(new ClientConnection(logicalAddress, physicalAddress, executorProvider_->get(keySuffix),
clientConfiguration_, authentication_, clientVersion_, *this,
keySuffix));
} catch (Result result) {
Promise<Result, ClientConnectionWeakPtr> promise;
promise.setFailed(result);
return promise.getFuture();
} catch (const std::runtime_error& e) {
lock.unlock();
LOG_ERROR("Failed to create connection: " << e.what())
Promise<Result, ClientConnectionWeakPtr> promise;
promise.setFailed(ResultConnectError);
return promise.getFuture();
}
LOG_INFO("Created connection for " << key);
Future<Result, ClientConnectionWeakPtr> future = cnx->getConnectFuture();
pool_.insert(std::make_pair(key, cnx));
lock.unlock();
cnx->tcpConnectAsync();
return future;
}
void ConnectionPool::remove(const std::string& key, ClientConnection* value) {
std::lock_guard<std::recursive_mutex> lock(mutex_);
auto it = pool_.find(key);
if (it != pool_.end() && it->second.get() == value) {
LOG_INFO("Remove connection for " << key);
pool_.erase(it);
}
}
} // namespace pulsar