开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (18)
标签 (41)
master
clickhouse
clickhouse-new-tmp
clickhouse-old-2021年12月22日
suppress-msan-2
master-old-2021年12月22日
nm/fix-uninitialized-memory
cmake-3-19
clickhouse-patch-leshikus2
update
boringssl
2.15.3
clickhouse_old_2.11
ssl-default-verify-paths
CPP-747-experiment
gh-pages
2.0
1.0
2.15.2
2.15.1
2.15.0
2.14.1
2.14.0
2.13.0
2.12.0
2.11.0
2.10.0
2.10.0-beta1
2.9.0
2.8.1
2.8.0
2.7.1
2.7.0
2.6.0
2.5.0
2.4.3
2.4.2
2.4.1
master
分支 (18)
标签 (41)
master
clickhouse
clickhouse-new-tmp
clickhouse-old-2021年12月22日
suppress-msan-2
master-old-2021年12月22日
nm/fix-uninitialized-memory
cmake-3-19
clickhouse-patch-leshikus2
update
boringssl
2.15.3
clickhouse_old_2.11
ssl-default-verify-paths
CPP-747-experiment
gh-pages
2.0
1.0
2.15.2
2.15.1
2.15.0
2.14.1
2.14.0
2.13.0
2.12.0
2.11.0
2.10.0
2.10.0-beta1
2.9.0
2.8.1
2.8.0
2.7.1
2.7.0
2.6.0
2.5.0
2.4.3
2.4.2
2.4.1
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (18)
标签 (41)
master
clickhouse
clickhouse-new-tmp
clickhouse-old-2021年12月22日
suppress-msan-2
master-old-2021年12月22日
nm/fix-uninitialized-memory
cmake-3-19
clickhouse-patch-leshikus2
update
boringssl
2.15.3
clickhouse_old_2.11
ssl-default-verify-paths
CPP-747-experiment
gh-pages
2.0
1.0
2.15.2
2.15.1
2.15.0
2.14.1
2.14.0
2.13.0
2.12.0
2.11.0
2.10.0
2.10.0-beta1
2.9.0
2.8.1
2.8.0
2.7.1
2.7.0
2.6.0
2.5.0
2.4.3
2.4.2
2.4.1
cpp-driver
/
src
/
connection_pool.cpp
cpp-driver
/
src
/
connection_pool.cpp
connection_pool.cpp 9.80 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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
/*
Copyright (c) DataStax, Inc.
Licensed 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 "connection_pool.hpp"
#include "config.hpp"
#include "connection_pool_manager.hpp"
#include "metrics.hpp"
#include "utils.hpp"
#include <algorithm>
using namespace datastax;
using namespace datastax::internal::core;
static inline bool least_busy_comp(const PooledConnection::Ptr& a, const PooledConnection::Ptr& b) {
// Don't consider closed connections to be the least busy.
if (a->is_closing()) { // "a" is closed so it can't be the least busy.
return false;
} else if (b->is_closing()) { // "a" is not close, but "b" is closed so "a" is less busy.
return true;
}
// Both "a" and "b" are not closed so compare their inflight request counts.
return a->inflight_request_count() < b->inflight_request_count();
}
ConnectionPoolSettings::ConnectionPoolSettings()
: num_connections_per_host(CASS_DEFAULT_NUM_CONNECTIONS_PER_HOST)
, reconnection_policy(new ExponentialReconnectionPolicy()) {}
ConnectionPoolSettings::ConnectionPoolSettings(const Config& config)
: connection_settings(config)
, num_connections_per_host(config.core_connections_per_host())
, reconnection_policy(config.reconnection_policy()) {}
class NopConnectionPoolListener : public ConnectionPoolListener {
public:
virtual void on_pool_up(const Address& address) {}
virtual void on_pool_down(const Address& address) {}
virtual void on_pool_critical_error(const Address& address, Connector::ConnectionError code,
const String& message) {}
virtual void on_close(ConnectionPool* pool) {}
};
NopConnectionPoolListener nop_connection_pool_listener__;
ConnectionPool::ConnectionPool(const Connection::Vec& connections, ConnectionPoolListener* listener,
const String& keyspace, uv_loop_t* loop, const Host::Ptr& host,
ProtocolVersion protocol_version,
const ConnectionPoolSettings& settings, Metrics* metrics)
: listener_(listener ? listener : &nop_connection_pool_listener__)
, keyspace_(keyspace)
, loop_(loop)
, host_(host)
, protocol_version_(protocol_version)
, settings_(settings)
, metrics_(metrics)
, close_state_(CLOSE_STATE_OPEN)
, notify_state_(NOTIFY_STATE_NEW) {
inc_ref(); // Reference for the lifetime of the pooled connections
set_pointer_keys(reconnection_schedules_);
set_pointer_keys(to_flush_);
for (Connection::Vec::const_iterator it = connections.begin(), end = connections.end(); it != end;
++it) {
const Connection::Ptr& connection(*it);
if (!connection->is_closing()) {
add_connection(PooledConnection::Ptr(new PooledConnection(this, connection)));
}
}
notify_up_or_down();
// We had non-critical errors or some connections closed
assert(connections.size() <= settings_.num_connections_per_host);
size_t needed = settings_.num_connections_per_host - connections_.size();
for (size_t i = 0; i < needed; ++i) {
schedule_reconnect();
}
}
PooledConnection::Ptr ConnectionPool::find_least_busy() const {
PooledConnection::Vec::const_iterator it =
std::min_element(connections_.begin(), connections_.end(), least_busy_comp);
if (it == connections_.end() || (*it)->is_closing()) {
return PooledConnection::Ptr();
}
return *it;
}
bool ConnectionPool::has_connections() const { return !connections_.empty(); }
void ConnectionPool::flush() {
for (DenseHashSet<PooledConnection*>::const_iterator it = to_flush_.begin(),
end = to_flush_.end();
it != end; ++it) {
(*it)->flush();
}
to_flush_.clear();
}
void ConnectionPool::close() { internal_close(); }
void ConnectionPool::attempt_immediate_connect() {
for (DelayedConnector::Vec::iterator it = pending_connections_.begin(),
end = pending_connections_.end();
it != end; ++it) {
(*it)->attempt_immediate_connect();
}
}
void ConnectionPool::set_listener(ConnectionPoolListener* listener) {
listener_ = listener ? listener : &nop_connection_pool_listener__;
}
void ConnectionPool::set_keyspace(const String& keyspace) { keyspace_ = keyspace; }
void ConnectionPool::requires_flush(PooledConnection* connection, ConnectionPool::Protected) {
if (to_flush_.empty()) {
listener_->on_requires_flush(this);
}
to_flush_.insert(connection);
}
void ConnectionPool::close_connection(PooledConnection* connection, Protected) {
if (metrics_) {
metrics_->total_connections.dec();
}
connections_.erase(std::remove(connections_.begin(), connections_.end(), connection),
connections_.end());
to_flush_.erase(connection);
if (close_state_ != CLOSE_STATE_OPEN) {
maybe_closed();
return;
}
// When there are no more connections available then notify that the host
// is down.
notify_up_or_down();
schedule_reconnect();
}
void ConnectionPool::add_connection(const PooledConnection::Ptr& connection) {
if (metrics_) {
metrics_->total_connections.inc();
}
connections_.push_back(connection);
}
void ConnectionPool::notify_up_or_down() {
if ((notify_state_ == NOTIFY_STATE_NEW || notify_state_ == NOTIFY_STATE_UP) &&
connections_.empty()) {
notify_state_ = NOTIFY_STATE_DOWN;
listener_->on_pool_down(host_->address());
} else if ((notify_state_ == NOTIFY_STATE_NEW || notify_state_ == NOTIFY_STATE_DOWN) &&
!connections_.empty()) {
notify_state_ = NOTIFY_STATE_UP;
listener_->on_pool_up(host_->address());
}
}
void ConnectionPool::notify_critical_error(Connector::ConnectionError code, const String& message) {
if (notify_state_ != NOTIFY_STATE_CRITICAL) {
notify_state_ = NOTIFY_STATE_CRITICAL;
listener_->on_pool_critical_error(host_->address(), code, message);
}
}
void ConnectionPool::schedule_reconnect(ReconnectionSchedule* schedule) {
DelayedConnector::Ptr connector(new DelayedConnector(
host_, protocol_version_, bind_callback(&ConnectionPool::on_reconnect, this)));
if (!schedule) {
schedule = settings_.reconnection_policy->new_reconnection_schedule();
}
reconnection_schedules_[connector.get()] = schedule;
uint64_t delay_ms = schedule->next_delay_ms();
LOG_INFO("Scheduling %s reconnect for host %s in %llums on connection pool (%p) ",
settings_.reconnection_policy->name(), host_->address().to_string().c_str(),
static_cast<unsigned long long>(delay_ms), static_cast<void*>(this));
pending_connections_.push_back(connector);
connector->with_keyspace(keyspace())
->with_metrics(metrics_)
->with_settings(settings_.connection_settings)
->delayed_connect(loop_, delay_ms);
}
void ConnectionPool::internal_close() {
if (close_state_ == CLOSE_STATE_OPEN) {
close_state_ = CLOSE_STATE_CLOSING;
// Make copies of connection/connector data structures to prevent iterator
// invalidation.
PooledConnection::Vec connections(connections_);
for (PooledConnection::Vec::iterator it = connections.begin(), end = connections.end();
it != end; ++it) {
(*it)->close();
}
DelayedConnector::Vec pending_connections(pending_connections_);
for (DelayedConnector::Vec::iterator it = pending_connections.begin(),
end = pending_connections.end();
it != end; ++it) {
(*it)->cancel();
}
close_state_ = CLOSE_STATE_WAITING_FOR_CONNECTIONS;
maybe_closed();
}
}
void ConnectionPool::maybe_closed() {
// Remove the pool once all current connections and pending connections
// are terminated.
if (close_state_ == CLOSE_STATE_WAITING_FOR_CONNECTIONS && connections_.empty() &&
pending_connections_.empty()) {
close_state_ = CLOSE_STATE_CLOSED;
// Only mark DOWN if it's UP otherwise we might get multiple DOWN events
// when connecting the pool.
if (notify_state_ == NOTIFY_STATE_UP) {
listener_->on_pool_down(host_->address());
}
listener_->on_close(this);
dec_ref();
}
}
void ConnectionPool::on_reconnect(DelayedConnector* connector) {
pending_connections_.erase(
std::remove(pending_connections_.begin(), pending_connections_.end(), connector),
pending_connections_.end());
ReconnectionSchedules::iterator it = reconnection_schedules_.find(connector);
assert(it != reconnection_schedules_.end() &&
"No reconnection schedule associated with connector");
ScopedPtr<ReconnectionSchedule> schedule(it->second);
reconnection_schedules_.erase(it);
if (close_state_ != CLOSE_STATE_OPEN) {
maybe_closed();
return;
}
if (connector->is_ok()) {
add_connection(
PooledConnection::Ptr(new PooledConnection(this, connector->release_connection())));
notify_up_or_down();
} else if (!connector->is_canceled()) {
if (connector->is_critical_error()) {
LOG_ERROR("Closing established connection pool to host %s because of the following error: %s",
address().to_string().c_str(), connector->error_message().c_str());
notify_critical_error(connector->error_code(), connector->error_message());
internal_close();
} else {
LOG_WARN(
"Connection pool was unable to reconnect to host %s because of the following error: %s",
address().to_string().c_str(), connector->error_message().c_str());
schedule_reconnect(schedule.release());
}
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

cpp-driver加速
暂无标签
Apache-2.0
使用 Apache-2.0 开源许可协议
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/cyys/cpp-driver.git
git@gitee.com:cyys/cpp-driver.git
cyys
cpp-driver
cpp-driver
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /