Skip to content
This repository was archived by the owner on Apr 6, 2019. It is now read-only.

Commit 4da8ea1

Browse files
committed
provide ability to configure sentinel timeout #132
1 parent e738071 commit 4da8ea1

File tree

6 files changed

+61
-25
lines changed

6 files changed

+61
-25
lines changed

includes/cpp_redis/core/client.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ class client {
300300
//! \param host sentinel host
301301
//! \param port sentinel port
302302
//!
303-
void add_sentinel(const std::string& host, std::size_t port);
303+
void add_sentinel(const std::string& host, std::size_t port, std::uint32_t timeout_msecs = 0);
304304

305305
//!
306306
//! retrieve sentinel for current client

includes/cpp_redis/core/sentinel.hpp

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class sentinel {
124124
//! \param port sentinel port
125125
//! \return current instance
126126
//!
127-
sentinel& add_sentinel(const std::string& host, std::size_t port);
127+
sentinel& add_sentinel(const std::string& host, std::size_t port, std::uint32_t timeout_msecs = 0);
128128

129129
//!
130130
//! clear all existing sentinels.
@@ -152,13 +152,11 @@ class sentinel {
152152

153153
//!
154154
//! Connect to 1st active sentinel we find. Requires add_sentinel() to be called first
155+
//! will use timeout set for each added sentinel independently
155156
//!
156-
//! \param timeout_msecs maximum time to connect
157157
//! \param disconnect_handler handler to be called whenever disconnection occurs
158158
//!
159-
void connect_sentinel(
160-
std::uint32_t timeout_msecs = 0,
161-
const sentinel_disconnect_handler_t& disconnect_handler = nullptr);
159+
void connect_sentinel(const sentinel_disconnect_handler_t& disconnect_handler = nullptr);
162160

163161
//!
164162
//! Connect to named sentinel
@@ -207,12 +205,12 @@ class sentinel {
207205
sentinel& set(const std::string& name, const std::string& option, const std::string& value, const reply_callback_t& reply_callback = nullptr);
208206
sentinel& slaves(const std::string& name, const reply_callback_t& reply_callback = nullptr);
209207

210-
private:
208+
public:
211209
class sentinel_def {
212210
public:
213211
//! ctor
214-
sentinel_def(const std::string& host, std::size_t port)
215-
: m_host(host), m_port(port) {}
212+
sentinel_def(const std::string& host, std::size_t port, std::uint32_t timeout_msecs)
213+
: m_host(host), m_port(port), m_timeout_msecs(timeout_msecs) {}
216214

217215
//! dtor
218216
~sentinel_def(void) = default;
@@ -230,6 +228,19 @@ class sentinel {
230228
size_t
231229
get_port(void) const { return m_port; }
232230

231+
//!
232+
//! \return timeout for sentinel
233+
//!
234+
std::uint32_t
235+
get_timeout_msecs(void) const { return m_timeout_msecs; }
236+
237+
//!
238+
//! set connect timeout for sentinel in msecs
239+
//! \param timeout_msecs new value
240+
//!
241+
void
242+
set_timeout_msecs(std::uint32_t timeout_msecs) { m_timeout_msecs = timeout_msecs; }
243+
233244
private:
234245
//!
235246
//! sentinel host
@@ -240,8 +251,24 @@ class sentinel {
240251
//! sentinel port
241252
//!
242253
std::size_t m_port;
254+
255+
//!
256+
//! connect timeout config
257+
//!
258+
std::uint32_t m_timeout_msecs;
243259
};
244260

261+
public:
262+
//!
263+
//! \return sentinels
264+
//!
265+
const std::vector<sentinel_def>& get_sentinels(void) const;
266+
267+
//!
268+
//! \return sentinels (non-const version)
269+
//!
270+
std::vector<sentinel_def>& get_sentinels(void);
271+
245272
private:
246273
//!
247274
//! redis connection receive handler, triggered whenever a reply has been read by the redis connection

includes/cpp_redis/core/subscriber.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ class subscriber {
233233
//! \param host sentinel host
234234
//! \param port sentinel port
235235
//!
236-
void add_sentinel(const std::string& host, std::size_t port);
236+
void add_sentinel(const std::string& host, std::size_t port, std::uint32_t timeout_msecs = 0);
237237

238238
//!
239239
//! retrieve sentinel for current client

sources/core/client.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ client::is_reconnecting(void) const {
144144
}
145145

146146
void
147-
client::add_sentinel(const std::string& host, std::size_t port) {
148-
m_sentinel.add_sentinel(host, port);
147+
client::add_sentinel(const std::string& host, std::size_t port, std::uint32_t timeout_msecs) {
148+
m_sentinel.add_sentinel(host, port, timeout_msecs);
149149
}
150150

151151
const sentinel&

sources/core/sentinel.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ sentinel::~sentinel(void) {
4949
}
5050

5151
sentinel&
52-
sentinel::add_sentinel(const std::string& host, std::size_t port) {
53-
m_sentinels.push_back({host, port});
52+
sentinel::add_sentinel(const std::string& host, std::size_t port, std::uint32_t timeout_msecs) {
53+
m_sentinels.push_back({host, port, timeout_msecs});
5454
return *this;
5555
}
5656

@@ -62,9 +62,8 @@ sentinel::clear_sentinels() {
6262
bool
6363
sentinel::get_master_addr_by_name(const std::string& name, std::string& host, std::size_t& port, bool autoconnect) {
6464
//! reset connection settings
65-
host.clear();
66-
port = 0;
67-
std::uint32_t connect_timeout = 2000;
65+
host = "";
66+
port = 0;
6867

6968
//! we must have some sentinels to connect to if we are in autoconnect mode
7069
if (autoconnect && m_sentinels.size() == 0) {
@@ -79,7 +78,7 @@ sentinel::get_master_addr_by_name(const std::string& name, std::string& host, st
7978
if (autoconnect) {
8079
try {
8180
//! Will round robin all attached sentinels until it finds one that is online.
82-
connect_sentinel(connect_timeout, nullptr);
81+
connect_sentinel(nullptr);
8382
}
8483
catch (const redis_error&) {
8584
}
@@ -95,8 +94,8 @@ sentinel::get_master_addr_by_name(const std::string& name, std::string& host, st
9594
send({"SENTINEL", "get-master-addr-by-name", name}, [&](cpp_redis::reply& reply) {
9695
if (reply.is_array()) {
9796
auto arr = reply.as_array();
98-
host = arr[0].as_string(); //host
99-
port = std::stoi(arr[1].as_string(), nullptr, 10); //port
97+
host = arr[0].as_string();
98+
port = std::stoi(arr[1].as_string(), nullptr, 10);
10099
}
101100
});
102101
sync_commit();
@@ -111,7 +110,7 @@ sentinel::get_master_addr_by_name(const std::string& name, std::string& host, st
111110
}
112111

113112
void
114-
sentinel::connect_sentinel(std::uint32_t timeout_msecs, const sentinel_disconnect_handler_t& sentinel_disconnect_handler) {
113+
sentinel::connect_sentinel(const sentinel_disconnect_handler_t& sentinel_disconnect_handler) {
115114
if (m_sentinels.size() == 0) {
116115
throw redis_error("No sentinels available. Call add_sentinel() before connect_sentinel()");
117116
}
@@ -126,10 +125,10 @@ sentinel::connect_sentinel(std::uint32_t timeout_msecs, const sentinel_disconnec
126125
while (not_connected && it != m_sentinels.end()) {
127126
try {
128127
__CPP_REDIS_LOG(debug, std::string("cpp_redis::sentinel attempting to connect to host ") + it->get_host());
129-
m_client.connect(it->get_host(), it->get_port(), disconnect_handler, receive_handler, timeout_msecs);
128+
m_client.connect(it->get_host(), it->get_port(), disconnect_handler, receive_handler, it->get_timeout_msecs());
130129
}
131130
catch (const redis_error&) {
132-
not_connected = true; //Connection failed.
131+
not_connected = true; //! Connection failed.
133132
__CPP_REDIS_LOG(info, std::string("cpp_redis::sentinel unable to connect to sentinel host ") + it->get_host());
134133
}
135134

@@ -232,6 +231,16 @@ sentinel::is_connected(void) {
232231
return m_client.is_connected();
233232
}
234233

234+
const std::vector<sentinel::sentinel_def>&
235+
sentinel::get_sentinels(void) const {
236+
return m_sentinels;
237+
}
238+
239+
std::vector<sentinel::sentinel_def>&
240+
sentinel::get_sentinels(void) {
241+
return m_sentinels;
242+
}
243+
235244
sentinel&
236245
sentinel::send(const std::vector<std::string>& redis_cmd, const reply_callback_t& callback) {
237246
std::lock_guard<std::mutex> lock_callback(m_callbacks_mutex);

sources/core/subscriber.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ subscriber::connect(
117117
}
118118

119119
void
120-
subscriber::add_sentinel(const std::string& host, std::size_t port) {
121-
m_sentinel.add_sentinel(host, port);
120+
subscriber::add_sentinel(const std::string& host, std::size_t port, std::uint32_t timeout_msecs) {
121+
m_sentinel.add_sentinel(host, port, timeout_msecs);
122122
}
123123

124124
const sentinel&

0 commit comments

Comments
 (0)