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

Commit 9a68fc1

Browse files
committed
change the way to set disconnection handlers: remove the setter so that the handler can only be set when calling the connect() member functions. This remove the need of a mutex, increasing the performance and reducing code complexity. The same has been done for the receive handlers of internal classes, introducing possible better performance.
1 parent 9aa7bf1 commit 9a68fc1

File tree

11 files changed

+83
-175
lines changed

11 files changed

+83
-175
lines changed

README.md

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,20 @@ To build the tests, it is necessary to install `google_tests`. Just run the `ins
3838

3939
### Methods
4040

41-
#### void connect(const std::string& host = "127.0.0.1", unsigned int port = 6379)
41+
#### void connect(const std::string& host = "127.0.0.1", unsigned int port = 6379, const disconnection_handler& handler = nullptr)
4242
Connect to the Redis Server. Connection is done synchronously.
4343
Throws redis_error in case of failure or if client if already connected.
4444

45+
Also set the disconnection handler which is called whenever a disconnection has occurred.
46+
Disconnection handler is an `std::function<void(redis_client&)>`.
47+
4548
#### void disconnect(void)
4649
Disconnect client from remote host.
4750
Throws redis_error if client is not connected to any server.
4851

4952
#### bool is_connected(void)
5053
Returns whether the client is connected or not.
5154

52-
#### void set_disconnection_handler(const disconnection_handler& handler)
53-
Set the disconnection handler which is called whenever a disconnection has occurred.
54-
Disconnection handler is an `std::function<void(redis_client&)>`.
55-
5655
#### void send(const std::vector<std::string>& redis_cmd, const reply_callback& callback = nullptr)
5756
Send a command and set the callback which has to be called when the reply has been received.
5857
If `nullptr` is passed as callback, command is executed and no callback will be called.
@@ -78,13 +77,11 @@ sigint_handler(int) {
7877

7978
int
8079
main(void) {
81-
client.set_disconnection_handler([] (cpp_redis::redis_client&) {
80+
client.connect("127.0.0.1", 6379, [] (cpp_redis::redis_client&) {
8281
std::cout << "client disconnected (disconnection handler)" << std::endl;
8382
should_exit = true;
8483
});
8584

86-
client.connect();
87-
8885
client.send({"SET", "hello", "world"}, [] (cpp_redis::reply& reply) {
8986
std::cout << reply.as_string() << std::endl;
9087
});
@@ -103,21 +100,20 @@ main(void) {
103100
104101
### Methods
105102
106-
#### void connect(const std::string& host = "127.0.0.1", unsigned int port = 6379)
103+
#### void connect(const std::string& host = "127.0.0.1", unsigned int port = 6379, const disconnection_handler& handler = nullptr)
107104
Connect to the Redis Server. Connection is done synchronously.
108105
Throws redis_error in case of failure or if client if already connected.
109106
107+
Also set the disconnection handler which is called whenever a disconnection has occurred.
108+
Disconnection handler is an `std::function<void(redis_subscriber&)>`.
109+
110110
#### void disconnect(void)
111111
Disconnect client from remote host.
112112
Throws redis_error if client is not connected to any server.
113113
114114
#### bool is_connected(void)
115115
Returns whether the client is connected or not.
116116
117-
#### void set_disconnection_handler(const disconnection_handler& handler)
118-
Set the disconnection handler which is called whenever a disconnection has occurred.
119-
Disconnection handler is an `std::function<void(redis_subscriber&)>`.
120-
121117
#### void subscribe(const std::string& channel, const subscribe_callback& callback)
122118
Subscribe to the given channel and call subscribe_callback each time a message is published in this channel.
123119
subscribe_callback is an `std::function<void(const std::string&, const std::string&)>`.
@@ -145,31 +141,29 @@ cpp_redis::redis_subscriber sub;
145141
146142
void
147143
sigint_handler(int) {
148-
std::cout << "disconnected (sigint handler)" << std::endl;
149-
sub.disconnect();
150-
should_exit = true;
144+
std::cout << "disconnected (sigint handler)" << std::endl;
145+
sub.disconnect();
146+
should_exit = true;
151147
}
152148
153149
int
154150
main(void) {
155-
sub.set_disconnection_handler([] (cpp_redis::redis_subscriber&) {
156-
std::cout << "sub disconnected (disconnection handler)" << std::endl;
157-
should_exit = true;
158-
});
159-
160-
sub.connect();
151+
sub.connect("127.0.0.1", 6379, [](cpp_redis::redis_subscriber&) {
152+
std::cout << "sub disconnected (disconnection handler)" << std::endl;
153+
should_exit = true;
154+
});
161155
162-
sub.subscribe("some_chan", [] (const std::string& chan, const std::string& msg) {
163-
std::cout << "MESSAGE " << chan << ": " << msg << std::endl;
164-
});
165-
sub.psubscribe("*", [] (const std::string& chan, const std::string& msg) {
166-
std::cout << "PMESSAGE " << chan << ": " << msg << std::endl;
167-
});
156+
sub.subscribe("some_chan", [] (const std::string& chan, const std::string& msg) {
157+
std::cout << "MESSAGE " << chan << ": " << msg << std::endl;
158+
});
159+
sub.psubscribe("*", [] (const std::string& chan, const std::string& msg) {
160+
std::cout << "PMESSAGE " << chan << ": " << msg << std::endl;
161+
});
168162
169-
signal(SIGINT, &sigint_handler);
170-
while (not should_exit);
163+
signal(SIGINT, &sigint_handler);
164+
while (not should_exit);
171165
172-
return 0;
166+
return 0;
173167
}
174168
```
175169

examples/redis_client.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ sigint_handler(int) {
1515

1616
int
1717
main(void) {
18-
client.set_disconnection_handler([] (cpp_redis::redis_client&) {
18+
client.connect("127.0.0.1", 6379, [] (cpp_redis::redis_client&) {
1919
std::cout << "client disconnected (disconnection handler)" << std::endl;
2020
should_exit = true;
2121
});
2222

23-
client.connect();
24-
2523
client.send({"SET", "hello", "world"}, [] (cpp_redis::reply& reply) {
2624
std::cout << reply.as_string() << std::endl;
2725
});

examples/redis_subscriber.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ sigint_handler(int) {
1515

1616
int
1717
main(void) {
18-
sub.set_disconnection_handler([] (cpp_redis::redis_subscriber&) {
18+
sub.connect("127.0.0.1", 6379, [](cpp_redis::redis_subscriber&) {
1919
std::cout << "sub disconnected (disconnection handler)" << std::endl;
2020
should_exit = true;
2121
});
2222

23-
sub.connect();
24-
2523
sub.subscribe("some_chan", [] (const std::string& chan, const std::string& msg) {
2624
std::cout << "MESSAGE " << chan << ": " << msg << std::endl;
2725
});

includes/cpp_redis/network/redis_connection.hpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,17 @@ class redis_connection {
2424

2525
public:
2626
//! handle connection
27-
void connect(const std::string& host = "127.0.0.1", unsigned int port = 6379);
27+
typedef std::function<void(redis_connection&)> disconnection_handler_t;
28+
typedef std::function<void(redis_connection&, reply&)> reply_callback_t;
29+
void connect(const std::string& host = "127.0.0.1", unsigned int port = 6379,
30+
const disconnection_handler_t& disconnection_handler = nullptr,
31+
const reply_callback_t& reply_callback = nullptr);
2832
void disconnect(void);
2933
bool is_connected(void);
3034

31-
//! disconnection handler
32-
typedef std::function<void(redis_connection&)> disconnection_handler_t;
33-
void set_disconnection_handler(const disconnection_handler_t& handler);
34-
3535
//! send cmd
3636
void send(const std::vector<std::string>& redis_cmd);
3737

38-
//! receive handler
39-
typedef std::function<void(redis_connection&, reply&)> reply_callback_t;
40-
void set_reply_callback(const reply_callback_t& handler);
41-
4238
private:
4339
//! receive & disconnection handlers
4440
bool tcp_client_receive_handler(network::tcp_client&, const std::vector<char>& buffer);
@@ -58,10 +54,6 @@ class redis_connection {
5854

5955
//! reply builder
6056
builders::reply_builder m_builder;
61-
62-
//! thread safety
63-
std::mutex m_disconnection_handler_mutex;
64-
std::mutex m_reply_callback_mutex;
6557
};
6658

6759
} //! network

includes/cpp_redis/network/tcp_client.hpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,17 @@ class tcp_client {
3030
bool is_connected(void);
3131

3232
//! handle connection & disconnection
33-
void connect(const std::string& host, unsigned int port);
33+
typedef std::function<void(tcp_client&)> disconnection_handler_t;
34+
typedef std::function<bool(tcp_client&, const std::vector<char>& buffer)> receive_handler_t;
35+
void connect(const std::string& host, unsigned int port,
36+
const disconnection_handler_t& disconnection_handler = nullptr,
37+
const receive_handler_t& receive_handler = nullptr);
3438
void disconnect(void);
3539

3640
//! send data
3741
void send(const std::string& buffer);
3842
void send(const std::vector<char>& buffer);
3943

40-
//! set receive callback
41-
//! called each time some data has been received
42-
//! Returns true if everything is ok, false otherwise (trigger disconnection)
43-
typedef std::function<bool(tcp_client&, const std::vector<char>& buffer)> receive_handler_t;
44-
void set_receive_handler(const receive_handler_t& handler);
45-
46-
//! set disconnection callback
47-
//! called each time a disconnection has been detected
48-
typedef std::function<void(tcp_client&)> disconnection_handler_t;
49-
void set_disconnection_handler(const disconnection_handler_t& handler);
50-
5144
private:
5245
//! make boost asio async read and write operations
5346
void async_read(void);
@@ -77,8 +70,6 @@ class tcp_client {
7770

7871
//! thread safety
7972
std::mutex m_write_buffer_mutex;
80-
std::mutex m_receive_handler_mutex;
81-
std::mutex m_disconnection_handler_mutex;
8273
};
8374

8475
} //! network

includes/cpp_redis/redis_client.hpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace cpp_redis {
1313
class redis_client {
1414
public:
1515
//! ctor & dtor
16-
redis_client(void);
16+
redis_client(void) = default;
1717
~redis_client(void);
1818

1919
//! copy ctor & assignment operator
@@ -22,14 +22,12 @@ class redis_client {
2222

2323
public:
2424
//! handle connection
25-
void connect(const std::string& host = "127.0.0.1", unsigned int port = 6379);
25+
typedef std::function<void(redis_client&)> disconnection_handler_t;
26+
void connect(const std::string& host = "127.0.0.1", unsigned int port = 6379,
27+
const disconnection_handler_t& disconnection_handler = nullptr);
2628
void disconnect(void);
2729
bool is_connected(void);
2830

29-
//! disconnection handler
30-
typedef std::function<void(redis_client&)> disconnection_handler_t;
31-
void set_disconnection_handler(const disconnection_handler_t& handler);
32-
3331
//! send cmd
3432
typedef std::function<void(reply&)> reply_callback_t;
3533
void send(const std::vector<std::string>& redis_cmd, const reply_callback_t& callback = nullptr);
@@ -53,7 +51,6 @@ class redis_client {
5351
disconnection_handler_t m_disconnection_handler;
5452

5553
//! thread safety
56-
std::mutex m_disconnection_handler_mutex;
5754
std::mutex m_callbacks_mutex;
5855
};
5956

includes/cpp_redis/redis_subscriber.hpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace cpp_redis {
1313
class redis_subscriber {
1414
public:
1515
//! ctor & dtor
16-
redis_subscriber(void);
16+
redis_subscriber(void) = default;
1717
~redis_subscriber(void);
1818

1919
//! copy ctor & assignment operator
@@ -22,14 +22,12 @@ class redis_subscriber {
2222

2323
public:
2424
//! handle connection
25-
void connect(const std::string& host = "127.0.0.1", unsigned int port = 6379);
25+
typedef std::function<void(redis_subscriber&)> disconnection_handler_t;
26+
void connect(const std::string& host = "127.0.0.1", unsigned int port = 6379,
27+
const disconnection_handler_t& disconnection_handler = nullptr);
2628
void disconnect(void);
2729
bool is_connected(void);
2830

29-
//! disconnection handler
30-
typedef std::function<void(redis_subscriber&)> disconnection_handler_t;
31-
void set_disconnection_handler(const disconnection_handler_t& handler);
32-
3331
//! subscribe - unsubscribe
3432
typedef std::function<void(const std::string&, const std::string&)> subscribe_callback_t;
3533
void subscribe(const std::string& channel, const subscribe_callback_t& callback);
@@ -56,7 +54,6 @@ class redis_subscriber {
5654
disconnection_handler_t m_disconnection_handler;
5755

5856
//! thread safety
59-
std::mutex m_disconnection_handler_mutex;
6057
std::mutex m_psubscribed_channels_mutex;
6158
std::mutex m_subscribed_channels_mutex;
6259
};

sources/network/redis_connection.cpp

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,24 @@ namespace network {
77
redis_connection::redis_connection(void)
88
: m_reply_callback(nullptr)
99
, m_disconnection_handler(nullptr)
10-
{
11-
auto disconnection_handler = std::bind(&redis_connection::tcp_client_disconnection_handler, this, std::placeholders::_1);
12-
m_client.set_disconnection_handler(disconnection_handler);
13-
14-
auto receive_handler = std::bind(&redis_connection::tcp_client_receive_handler, this, std::placeholders::_1, std::placeholders::_2);
15-
m_client.set_receive_handler(receive_handler);
16-
}
10+
{}
1711

1812
redis_connection::~redis_connection(void) {
1913
if (is_connected())
2014
disconnect();
2115
}
2216

2317
void
24-
redis_connection::connect(const std::string& host, unsigned int port) {
25-
m_client.connect(host, port);
18+
redis_connection::connect(const std::string& host, unsigned int port,
19+
const disconnection_handler_t& client_disconnection_handler,
20+
const reply_callback_t& client_reply_callback)
21+
{
22+
m_reply_callback = client_reply_callback;
23+
m_disconnection_handler = client_disconnection_handler;
24+
25+
auto disconnection_handler = std::bind(&redis_connection::tcp_client_disconnection_handler, this, std::placeholders::_1);
26+
auto receive_handler = std::bind(&redis_connection::tcp_client_receive_handler, this, std::placeholders::_1, std::placeholders::_2);
27+
m_client.connect(host, port, disconnection_handler, receive_handler);
2628
}
2729

2830
void
@@ -50,20 +52,6 @@ redis_connection::send(const std::vector<std::string>& redis_cmd) {
5052
m_client.send(build_command(redis_cmd));
5153
}
5254

53-
void
54-
redis_connection::set_disconnection_handler(const disconnection_handler_t& handler) {
55-
std::lock_guard<std::mutex> lock(m_disconnection_handler_mutex);
56-
57-
m_disconnection_handler = handler;
58-
}
59-
60-
void
61-
redis_connection::set_reply_callback(const reply_callback_t& handler) {
62-
std::lock_guard<std::mutex> lock(m_reply_callback_mutex);
63-
64-
m_reply_callback = handler;
65-
}
66-
6755
bool
6856
redis_connection::tcp_client_receive_handler(network::tcp_client&, const std::vector<char>& buffer) {
6957
try {
@@ -74,8 +62,6 @@ redis_connection::tcp_client_receive_handler(network::tcp_client&, const std::ve
7462
}
7563

7664
while (m_builder.reply_available()) {
77-
std::lock_guard<std::mutex> lock(m_reply_callback_mutex);
78-
7965
auto reply = m_builder.get_front();
8066
m_builder.pop_front();
8167

@@ -88,8 +74,6 @@ redis_connection::tcp_client_receive_handler(network::tcp_client&, const std::ve
8874

8975
void
9076
redis_connection::tcp_client_disconnection_handler(network::tcp_client&) {
91-
std::lock_guard<std::mutex> lock(m_disconnection_handler_mutex);
92-
9377
if (m_disconnection_handler)
9478
m_disconnection_handler(*this);
9579
}

0 commit comments

Comments
 (0)