Skip to content

Commit 6ce793e

Browse files
authored
Merge pull request #78 from boostorg/69-move-health-check-functionality-from-examples-to-the-library
Implements a function that checks Redis health.
2 parents 16b5c8d + a83c0e7 commit 6ce793e

25 files changed

+371
-68
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ jobs:
120120
- name: Install Redis
121121
run: sudo apt-get install -y redis-server
122122
- name: Install boost
123-
uses: MarkusJx/install-boost@v2.3.0
123+
uses: MarkusJx/install-boost@v2.4.1
124124
id: install-boost
125125
with:
126126
boost_version: 1.81.0

.github/workflows/coverage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
- name: Install Redis
2828
run: sudo apt-get -y install redis-server
2929
- name: Install boost
30-
uses: MarkusJx/install-boost@v2.3.0
30+
uses: MarkusJx/install-boost@v2.4.1
3131
id: install-boost
3232
with:
3333
boost_version: 1.81.0

CMakeLists.txt

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,10 @@ if (MSVC)
100100
target_compile_definitions(cpp17_intro_sync PRIVATE _WIN32_WINNT=0x0601)
101101
endif()
102102

103+
if (NOT MSVC)
103104
add_executable(cpp20_chat_room examples/cpp20_chat_room.cpp)
104105
target_compile_features(cpp20_chat_room PUBLIC cxx_std_20)
105106
target_link_libraries(cpp20_chat_room common)
106-
if (MSVC)
107-
target_compile_options(cpp20_chat_room PRIVATE /bigobj)
108-
target_compile_definitions(cpp20_chat_room PRIVATE _WIN32_WINNT=0x0601)
109107
endif()
110108

111109
add_executable(cpp20_containers examples/cpp20_containers.cpp)
@@ -117,12 +115,10 @@ if (MSVC)
117115
target_compile_definitions(cpp20_containers PRIVATE _WIN32_WINNT=0x0601)
118116
endif()
119117

118+
if (NOT MSVC)
120119
add_executable(cpp20_echo_server examples/cpp20_echo_server.cpp)
121120
target_compile_features(cpp20_echo_server PUBLIC cxx_std_20)
122121
target_link_libraries(cpp20_echo_server common)
123-
if (MSVC)
124-
target_compile_options(cpp20_echo_server PRIVATE /bigobj)
125-
target_compile_definitions(cpp20_echo_server PRIVATE _WIN32_WINNT=0x0601)
126122
endif()
127123

128124
add_executable(cpp20_resolve_with_sentinel examples/cpp20_resolve_with_sentinel.cpp)
@@ -143,12 +139,10 @@ if (MSVC)
143139
target_compile_definitions(cpp20_json_serialization PRIVATE _WIN32_WINNT=0x0601)
144140
endif()
145141

142+
if (NOT MSVC)
146143
add_executable(cpp20_subscriber examples/cpp20_subscriber.cpp)
147144
target_compile_features(cpp20_subscriber PUBLIC cxx_std_20)
148145
target_link_libraries(cpp20_subscriber common)
149-
if (MSVC)
150-
target_compile_options(cpp20_subscriber PRIVATE /bigobj)
151-
target_compile_definitions(cpp20_subscriber PRIVATE _WIN32_WINNT=0x0601)
152146
endif()
153147

154148
add_executable(cpp20_intro_tls examples/cpp20_intro_tls.cpp)
@@ -293,13 +287,18 @@ if (MSVC)
293287
target_compile_definitions(test_request PRIVATE _WIN32_WINNT=0x0601)
294288
endif()
295289

290+
if (NOT MSVC)
296291
add_executable(test_issue_50 tests/issue_50.cpp)
297292
target_compile_features(test_issue_50 PUBLIC cxx_std_20)
298293
target_link_libraries(test_issue_50 common)
299294
add_test(test_issue_50 test_issue_50)
300-
if (MSVC)
301-
target_compile_options(test_issue_50 PRIVATE /bigobj)
302-
target_compile_definitions(test_issue_50 PRIVATE _WIN32_WINNT=0x0601)
295+
endif()
296+
297+
if (NOT MSVC)
298+
add_executable(test_conn_check_health tests/conn_check_health.cpp)
299+
target_compile_features(test_conn_check_health PUBLIC cxx_std_17)
300+
target_link_libraries(test_conn_check_health common)
301+
add_test(test_conn_check_health test_conn_check_health)
303302
endif()
304303

305304
# Install

CMakePresets.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,23 @@
5656
"DOXYGEN_OUTPUT_DIRECTORY": "${sourceDir}/build/g++-11-cpp17/doc/"
5757
}
5858
},
59+
{
60+
"name": "g++-11-cpp20",
61+
"generator": "Unix Makefiles",
62+
"hidden": false,
63+
"inherits": ["cmake-pedantic"],
64+
"binaryDir": "${sourceDir}/build/g++-11-cpp20",
65+
"cacheVariables": {
66+
"CMAKE_BUILD_TYPE": "Debug",
67+
"CMAKE_CXX_EXTENSIONS": "OFF",
68+
"CMAKE_CXX_FLAGS": "-Wall -Wextra -fsanitize=address",
69+
"CMAKE_CXX_COMPILER": "g++-11",
70+
"CMAKE_SHARED_LINKER_FLAGS": "-fsanitize=address",
71+
"CMAKE_CXX_STANDARD_REQUIRED": "ON",
72+
"PROJECT_BINARY_DIR": "${sourceDir}/build/g++-11-cpp20",
73+
"DOXYGEN_OUTPUT_DIRECTORY": "${sourceDir}/build/g++-11-cpp20/doc/"
74+
}
75+
},
5976
{
6077
"name": "libc++-14-cpp17",
6178
"generator": "Unix Makefiles",
@@ -107,6 +124,7 @@
107124
"buildPresets": [
108125
{ "name": "coverage", "configurePreset": "coverage" },
109126
{ "name": "g++-11-cpp17", "configurePreset": "g++-11-cpp17" },
127+
{ "name": "g++-11-cpp20", "configurePreset": "g++-11-cpp20" },
110128
{ "name": "libc++-14-cpp17", "configurePreset": "libc++-14-cpp17" },
111129
{ "name": "libc++-14-cpp20", "configurePreset": "libc++-14-cpp20" },
112130
{ "name": "clang-tidy", "configurePreset": "clang-tidy" }

README.md

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -798,23 +798,70 @@ Acknowledgement to people that helped shape Boost.Redis
798798
* Klemens Morgenstern ([klemens-morgenstern](https://github.com/klemens-morgenstern)): For useful discussion about timeouts, cancellation, synchronous interfaces and general help with Asio.
799799
* Vinnie Falco ([vinniefalco](https://github.com/vinniefalco)): For general suggestions about how to improve the code and the documentation.
800800

801+
Also many thanks to all individuals that participated in the Boost
802+
review
803+
804+
* Zach Laine: https://lists.boost.org/Archives/boost/2023/01/253883.php
805+
* Vinnie Falco: https://lists.boost.org/Archives/boost/2023/01/253886.php
806+
* Christian Mazakas: https://lists.boost.org/Archives/boost/2023/01/253900.php
807+
* Ruben Perez: https://lists.boost.org/Archives/boost/2023/01/253915.php
808+
* Dmitry Arkhipov: https://lists.boost.org/Archives/boost/2023/01/253925.php
809+
* Alan de Freitas: https://lists.boost.org/Archives/boost/2023/01/253927.php
810+
* Mohammad Nejati: https://lists.boost.org/Archives/boost/2023/01/253929.php
811+
* Sam Hartsfield: https://lists.boost.org/Archives/boost/2023/01/253931.php
812+
* Miguel Portilla: https://lists.boost.org/Archives/boost/2023/01/253935.php
813+
* Robert A.H. Leahy: https://lists.boost.org/Archives/boost/2023/01/253928.php
814+
815+
The Reviews can be found at:
816+
https://lists.boost.org/Archives/boost/2023/01/date.php. The thread
817+
with the ACCEPT from the review manager can be found here:
818+
https://lists.boost.org/Archives/boost/2023/01/253944.php.
819+
801820
## Changelog
802821

803-
### master (incorporates many suggestions from the boost review)
822+
### master (incorporates changes to conform the boost review and more)
823+
824+
* Renames the project to Boost.Redis and moves the code into namespace
825+
`boost::redis`.
826+
827+
* As pointed out in the reviews the `to_buld` and `from_buld` names were too
828+
generic for ADL customization points. They gained the prefix `boost_redis_`.
804829

805-
* Renames the project to Boost.Redis and moves the code into namespace `boost::redis`.
806-
* As pointed out in the reviews the `to_buld` and `from_buld` names were too generic for ADL customization points. They gained the prefix `boost_redis_`.
807830
* Moves `boost::redis::resp3::request` to `boost::redis::request`.
808-
* Adds new typedef `boost::redis::response` that should be used instead of `std::tuple`.
809-
* Adds new typedef `boost::redis::generic_response` that should be used instead of `std::vector<resp3::node<std::string>>`.
831+
832+
* Adds new typedef `boost::redis::response` that should be used instead of
833+
`std::tuple`.
834+
835+
* Adds new typedef `boost::redis::generic_response` that should be used instead
836+
of `std::vector<resp3::node<std::string>>`.
837+
810838
* Renames `redis::ignore` to `redis::ignore_t`.
811-
* Changes the signature from `async_exec` to receive a `redis::response` instead of an adapter.
812-
* Adds `boost::redis::adapter::result` to store responses to commands including possible resp3 errors without losing the error diagnostic part. Basicaly instead of accessing values as `std::get<N>(resp)` users have to type `std::get<N>(resp).value()`
813-
* Implements full-duplex communication. Before these changes the connection would wait for a response to arrive before sending the next one. Now requests are continuously coalesced and written to the socket. This made the request::coalesce unnecessary and threfore it was removed.
814-
* Adds native json support for Boost.Describe'd classes, see cpp20_json_serialization.cpp for how to use it.
839+
840+
* Changes `async_exec` to receive a `redis::response` instead of an adapter,
841+
namely, instead of passing `adapt(resp)` users should pass `resp` directly.
842+
843+
* Introduces `boost::redis::adapter::result` to store responses to commands
844+
including possible resp3 errors without losing the error diagnostic part. To
845+
access values now use `std::get<N>(resp).value()` instead of
846+
`std::get<N>(resp)`.
847+
848+
* Implements full-duplex communication. Before these changes the connection
849+
would wait for a response to arrive before sending the next one. Now requests
850+
are continuously coalesced and written to the socket. `request::coalesce`
851+
became unnecessary and was removed. I could measure significative performance
852+
gains with theses changes.
853+
854+
* Adds native json support for Boost.Describe'd classes. To use it include
855+
`<boost/redis/json.hpp>` and decribe you class as of Boost.Describe, see
856+
cpp20_json_serialization.cpp for more details.
857+
815858
* Upgrades to Boost 1.81.0.
859+
816860
* Fixes build with libc++.
817861

862+
* Adds a function that performs health checks, see
863+
`boost::redis::experimental::async_check_health`.
864+
818865
### v1.4.0-1
819866

820867
* Renames `retry_on_connection_lost` to `cancel_if_unresponded`. (v1.4.1)

examples/common/common.cpp

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,6 @@ auto redir(boost::system::error_code& ec)
2424
{ return net::redirect_error(net::use_awaitable, ec); }
2525
}
2626

27-
auto health_check(std::shared_ptr<connection> conn) -> net::awaitable<void>
28-
{
29-
try {
30-
request req;
31-
req.push("PING");
32-
33-
timer_type timer{co_await net::this_coro::executor};
34-
35-
for (boost::system::error_code ec;;) {
36-
timer.expires_after(std::chrono::seconds{1});
37-
co_await (conn->async_exec(req) || timer.async_wait(redir(ec)));
38-
39-
if (!ec) {
40-
co_return;
41-
}
42-
43-
// Waits some time before trying the next ping.
44-
timer.expires_after(std::chrono::seconds{1});
45-
co_await timer.async_wait();
46-
}
47-
} catch (...) {
48-
}
49-
}
50-
5127
auto
5228
connect(
5329
std::shared_ptr<connection> conn,

examples/common/common.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
* accompanying file LICENSE.txt)
55
*/
66

7-
#ifndef AEDIS_EXAMPLES_COMMON_HPP
8-
#define AEDIS_EXAMPLES_COMMON_HPP
7+
#ifndef BOOST_REDIS_EXAMPLES_COMMON_HPP
8+
#define BOOST_REDIS_EXAMPLES_COMMON_HPP
99

10+
#include <iostream>
1011
#include <boost/asio.hpp>
1112
#include <boost/redis.hpp>
1213
#include <memory>
@@ -26,9 +27,7 @@ connect(
2627
std::string const& host,
2728
std::string const& port) -> boost::asio::awaitable<void>;
2829

29-
auto health_check(std::shared_ptr<connection> conn) -> boost::asio::awaitable<void>;
30-
3130
auto run(boost::asio::awaitable<void> op) -> int;
3231

3332
#endif // defined(BOOST_ASIO_HAS_CO_AWAIT)
34-
#endif // AEDIS_EXAMPLES_COMMON_HPP
33+
#endif // BOOST_REDIS_EXAMPLES_COMMON_HPP

examples/cpp20_chat_room.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace net = boost::asio;
1111
#if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
1212
#include <boost/asio/experimental/awaitable_operators.hpp>
1313
#include <boost/redis.hpp>
14+
#include <boost/redis/experimental/run.hpp>
1415
#include <unistd.h>
1516

1617
#include "common/common.hpp"
@@ -20,6 +21,7 @@ using stream_descriptor = net::use_awaitable_t<>::as_default_on_t<net::posix::st
2021
using signal_set = net::use_awaitable_t<>::as_default_on_t<net::signal_set>;
2122
using boost::redis::request;
2223
using boost::redis::generic_response;
24+
using boost::redis::experimental::async_check_health;
2325

2426
// Chat over Redis pubsub. To test, run this program from multiple
2527
// terminals and type messages to stdin.
@@ -60,7 +62,8 @@ auto co_main(std::string host, std::string port) -> net::awaitable<void>
6062

6163
co_await connect(conn, host, port);
6264
co_await ((conn->async_run() || publisher(stream, conn) || receiver(conn) ||
63-
health_check(conn) || sig.async_wait()) && conn->async_exec(req));
65+
async_check_health(*conn) || sig.async_wait()) &&
66+
conn->async_exec(req));
6467
}
6568

6669
#else // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)

examples/cpp20_echo_server.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#if defined(BOOST_ASIO_HAS_CO_AWAIT)
99
#include <boost/asio/experimental/awaitable_operators.hpp>
1010
#include <boost/redis.hpp>
11+
#include <boost/redis/experimental/run.hpp>
1112
#include "common/common.hpp"
1213

1314
namespace net = boost::asio;
@@ -17,6 +18,7 @@ using tcp_acceptor = net::use_awaitable_t<>::as_default_on_t<net::ip::tcp::accep
1718
using signal_set = net::use_awaitable_t<>::as_default_on_t<net::signal_set>;
1819
using boost::redis::request;
1920
using boost::redis::response;
21+
using boost::redis::experimental::async_check_health;
2022

2123
auto echo_server_session(tcp_socket socket, std::shared_ptr<connection> conn) -> net::awaitable<void>
2224
{
@@ -54,7 +56,7 @@ auto co_main(std::string host, std::string port) -> net::awaitable<void>
5456
req.push("HELLO", 3);
5557

5658
co_await connect(conn, host, port);
57-
co_await ((conn->async_run() || listener(conn) || health_check(conn) ||
59+
co_await ((conn->async_run() || listener(conn) || async_check_health(*conn) ||
5860
sig.async_wait()) && conn->async_exec(req));
5961
}
6062

examples/cpp20_subscriber.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#if defined(BOOST_ASIO_HAS_CO_AWAIT)
99
#include <boost/asio/experimental/awaitable_operators.hpp>
1010
#include <boost/redis.hpp>
11+
#include <boost/redis/experimental/run.hpp>
1112

1213
#include "common/common.hpp"
1314

@@ -16,6 +17,7 @@ using namespace net::experimental::awaitable_operators;
1617
using steady_timer = net::use_awaitable_t<>::as_default_on_t<net::steady_timer>;
1718
using boost::redis::request;
1819
using boost::redis::generic_response;
20+
using boost::redis::experimental::async_check_health;
1921

2022
/* This example will subscribe and read pushes indefinitely.
2123
*
@@ -56,7 +58,7 @@ auto co_main(std::string host, std::string port) -> net::awaitable<void>
5658
// The loop will reconnect on connection lost. To exit type Ctrl-C twice.
5759
for (;;) {
5860
co_await connect(conn, host, port);
59-
co_await ((conn->async_run() || health_check(conn) || receiver(conn)) && conn->async_exec(req));
61+
co_await ((conn->async_run() || async_check_health(*conn) || receiver(conn)) && conn->async_exec(req));
6062

6163
conn->reset_stream();
6264
timer.expires_after(std::chrono::seconds{1});

0 commit comments

Comments
 (0)