forked from apache/cassandra-cpp-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelayed_connector.cpp
More file actions
102 lines (80 loc) · 3.04 KB
/
delayed_connector.cpp
File metadata and controls
102 lines (80 loc) · 3.04 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
/*
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 "delayed_connector.hpp"
#include "event_loop.hpp"
using namespace datastax;
using namespace datastax::internal::core;
DelayedConnector::DelayedConnector(const Host::Ptr& host, ProtocolVersion protocol_version,
const Callback& callback)
: connector_(
new Connector(host, protocol_version, bind_callback(&DelayedConnector::on_connect, this)))
, callback_(callback)
, is_canceled_(false) {}
DelayedConnector* DelayedConnector::with_keyspace(const String& keyspace) {
connector_->with_keyspace(keyspace);
return this;
}
DelayedConnector* DelayedConnector::with_metrics(Metrics* metrics) {
connector_->with_metrics(metrics);
return this;
}
DelayedConnector* DelayedConnector::with_settings(const ConnectionSettings& settings) {
connector_->with_settings(settings);
return this;
}
void DelayedConnector::delayed_connect(uv_loop_t* loop, uint64_t wait_time_ms) {
inc_ref();
if (wait_time_ms > 0) {
delayed_connect_timer_.start(loop, wait_time_ms,
bind_callback(&DelayedConnector::on_delayed_connect, this));
} else {
internal_connect(loop);
}
}
void DelayedConnector::attempt_immediate_connect() {
if (delayed_connect_timer_.is_running() && !is_canceled_) {
internal_connect(delayed_connect_timer_.loop());
delayed_connect_timer_.stop();
}
}
void DelayedConnector::cancel() {
is_canceled_ = true;
if (delayed_connect_timer_.is_running()) {
delayed_connect_timer_.stop();
callback_(this);
dec_ref();
} else {
connector_->cancel();
}
}
Connection::Ptr DelayedConnector::release_connection() { return connector_->release_connection(); }
bool DelayedConnector::is_canceled() const { return is_canceled_; }
bool DelayedConnector::is_ok() const { return !is_canceled() && connector_->is_ok(); }
bool DelayedConnector::is_critical_error() const {
return !is_canceled() && connector_->is_critical_error();
}
bool DelayedConnector::is_keyspace_error() const {
return !is_canceled() && connector_->is_keyspace_error();
}
Connector::ConnectionError DelayedConnector::error_code() const {
if (is_canceled()) {
return Connector::CONNECTION_CANCELED;
}
return connector_->error_code();
}
void DelayedConnector::internal_connect(uv_loop_t* loop) { connector_->connect(loop); }
void DelayedConnector::on_connect(Connector* connector) {
callback_(this);
dec_ref();
}
void DelayedConnector::on_delayed_connect(Timer* timer) { internal_connect(timer->loop()); }