Skip to content

Commit b9a2356

Browse files
committed
Many improvements in the examples.
1 parent 4ac2509 commit b9a2356

18 files changed

+171
-128
lines changed

CMakeLists.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ write_basic_package_version_file(
4343
COMPATIBILITY AnyNewerVersion
4444
)
4545

46-
find_package(Boost 1.79 REQUIRED)
46+
find_package(Boost 1.80 REQUIRED)
4747
include_directories(${Boost_INCLUDE_DIRS})
4848

4949
find_package(OpenSSL REQUIRED)
@@ -54,19 +54,25 @@ include_directories(include)
5454
# Main function for the examples.
5555
#=======================================================================
5656

57-
add_library(common STATIC examples/common.cpp)
57+
add_library(common STATIC
58+
examples/common/common.cpp
59+
examples/common/main.cpp
60+
examples/common/aedis.cpp
61+
)
5862
target_compile_features(common PUBLIC cxx_std_20)
5963

6064
# Executables
6165
#=======================================================================
6266

63-
#add_executable(intro_sync examples/intro_sync.cpp) // Uncomment after update to Boost 1.80
64-
6567
add_executable(intro examples/intro.cpp)
6668
target_link_libraries(intro common)
6769
target_compile_features(intro PUBLIC cxx_std_20)
6870
add_test(intro intro)
6971

72+
add_executable(intro_sync examples/intro_sync.cpp)
73+
target_compile_features(intro_sync PUBLIC cxx_std_20)
74+
add_test(intro_sync intro_sync)
75+
7076
add_executable(chat_room examples/chat_room.cpp)
7177
target_compile_features(chat_room PUBLIC cxx_std_20)
7278
target_link_libraries(chat_room common)

README.md

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,21 @@ Some of its distinctive features are
1616
* Back pressure, cancellation and low latency.
1717

1818
In addition to that, Aedis hides most of the low-level Asio code away
19-
from the user. For example, the coroutine below retrieves Redis hashes
19+
from the user, which in the majority of the case will be concerned
20+
with three library entities
21+
22+
* `aedis::resp3::request`: A container of Redis commands.
23+
* `aedis::adapt()`: A function that adapts data structures to receive Redis responses.
24+
* `aedis::connection`: A connection to the Redis server.
25+
26+
For example, the coroutine below reads Redis [hashes](https://redis.io/docs/data-types/hashes/)
2027
in a `std::map` and quits the connection (see containers.cpp)
2128

2229
```cpp
2330
auto hgetall(std::shared_ptr<connection> conn) -> net::awaitable<void>
2431
{
2532
// A request contains multiple Redis commands.
2633
request req;
27-
req.get_config().cancel_on_connection_lost = true;
2834
req.push("HELLO", 3);
2935
req.push("HGETALL", "hset-key");
3036
req.push("QUIT");
@@ -39,11 +45,10 @@ auto hgetall(std::shared_ptr<connection> conn) -> net::awaitable<void>
3945
}
4046
```
4147
42-
The execution of `connection::async_exec` as shown above is
43-
triggered by the `connection::async_run` member function, which is
44-
required to be running concurrently for as long as the connection
45-
stands. For example, the code below uses a short-lived connection to
46-
execute the coroutine above
48+
The execution of `connection::async_exec` as shown above must
49+
still be triggered by the `connection::async_run` member function. For
50+
example, the code below uses a short-lived connection to execute the
51+
coroutine above
4752
4853
```cpp
4954
net::awaitable<void> async_main()
@@ -53,7 +58,7 @@ net::awaitable<void> async_main()
5358
// Resolves and connects (from examples/common.hpp to avoid vebosity)
5459
co_await connect(conn, "127.0.0.1", "6379");
5560
56-
// Runs and executes the request.
61+
// Runs hgetall (previous example).
5762
co_await (conn->async_run() || hgetall(conn));
5863
}
5964
```
@@ -67,32 +72,25 @@ reading from the socket. The reationale behind this design is
6772
* Support server pushes and requests in the same connection object,
6873
concurrently.
6974

70-
In the following sections we will discuss with more details the main
71-
code entities Aedis users are concerned with, namely
72-
73-
* `aedis::resp3::request`: A container of Redis commands.
74-
* `aedis::adapt()`: A function that adapts data structures to receive Redis responses.
75-
* `aedis::connection`: A connection to the Redis server.
76-
77-
before that however, users might find it helpful to skim over the
78-
examples, to gain a better feeling about the library capabilities.
75+
Before we see with more detail how connections, requests and responses
76+
work, users might find it helpful to skim over the examples, to gain a
77+
better feeling about the library capabilities.
7978

8079
* intro.cpp: The Aedis hello-world program. Sends one command to Redis and quits the connection.
8180
* intro_tls.cpp: Same as intro.cpp but over TLS.
81+
* intro_sync.cpp: Shows how to use the conneciton class synchronously.
8282
* containers.cpp: Shows how to send and receive STL containers and how to use transactions.
8383
* serialization.cpp: Shows how to serialize types using Boost.Json.
8484
* resolve_with_sentinel.cpp: Shows how to resolve a master address using sentinels.
8585
* subscriber.cpp: Shows how to implement pubsub with reconnection re-subscription.
8686
* echo_server.cpp: A simple TCP echo server.
8787
* chat_room.cpp: A command line chat built on Redis pubsub.
88-
89-
The next two examples uses the Aedis low-level API
90-
91-
* low_level_sync.cpp: Sends a ping synchronously.
92-
* low_level_async.cpp: Sends a ping asynchronously
88+
* low_level_sync.cpp: Sends a ping synchronously using the low-level API.
89+
* low_level_async.cpp: Sends a ping asynchronously using the low-level API.
9390

9491
To avoid repetition code that is common to all examples have been
95-
grouped in common.hpp.
92+
grouped in common.hpp. The main function is defined in main.cpp and
93+
used by all examples.
9694

9795
<a name="requests"></a>
9896
### Requests
@@ -498,7 +496,7 @@ auto async_main() -> net::awaitable<void>
498496
It is important to emphasize that Redis servers use the old
499497
communication protocol RESP2 by default, therefore it is necessary to
500498
send a `HELLO 3` command everytime a connection is established.
501-
Another common scenarios for reconnection is, for example, a failover
499+
Another common scenario for reconnection is, for example, a failover
502500
with sentinels, covered in `resolve_with_sentinel.cpp` example.
503501

504502
#### Execute
@@ -613,7 +611,7 @@ co_await (conn.async_exec(...) || time.async_wait(...))
613611
should last.
614612
* The cancellation will be ignored if the request has already
615613
been written to the socket.
616-
* It is usually a better idea to have a healthy checker that adding
614+
* It is usually a better idea to have a healthy checker than adding
617615
per request timeout, see subscriber.cpp for an example.
618616
619617
```cpp

examples/chat_room.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <aedis.hpp>
1111
#include <unistd.h>
1212

13-
#include "common.hpp"
13+
#include "common/common.hpp"
1414

1515
namespace net = boost::asio;
1616
using namespace net::experimental::awaitable_operators;
@@ -55,7 +55,7 @@ auto subscriber(std::shared_ptr<connection> conn) -> net::awaitable<void>
5555
co_await conn->async_exec(req);
5656
}
5757

58-
// Called from the main function (see common.cpp)
58+
// Called from the main function (see main.cpp)
5959
auto async_main() -> net::awaitable<void>
6060
{
6161
auto ex = co_await net::this_coro::executor;

examples/common/aedis.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* Copyright (c) 2018-2022 Marcelo Zimbres Silva ([email protected])
2+
*
3+
* Distributed under the Boost Software License, Version 1.0. (See
4+
* accompanying file LICENSE.txt)
5+
*/
6+
7+
#include <aedis.hpp>
8+
#include <aedis/src.hpp>

examples/common.cpp renamed to examples/common/common.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "common.hpp"
88

9+
#include <boost/asio.hpp>
910
#if defined(BOOST_ASIO_HAS_CO_AWAIT)
1011
#include <boost/asio/experimental/awaitable_operators.hpp>
1112
#include <iostream>
@@ -18,9 +19,6 @@ using aedis::resp3::request;
1819
using aedis::adapt;
1920
using aedis::operation;
2021

21-
// Include this in no more than one .cpp file.
22-
#include <aedis/src.hpp>
23-
2422
namespace
2523
{
2624
auto redir(boost::system::error_code& ec)
@@ -74,21 +72,4 @@ connect(
7472
throw std::runtime_error("Connect timeout");
7573
}
7674

77-
extern net::awaitable<void> async_main();
78-
79-
// Main function used in our examples.
80-
auto main() -> int
81-
{
82-
try {
83-
net::io_context ioc;
84-
net::co_spawn(ioc, async_main(), net::detached);
85-
ioc.run();
86-
} catch (std::exception const& e) {
87-
std::cerr << "Error: " << e.what() << std::endl;
88-
return 1;
89-
}
90-
}
91-
92-
#else // defined(BOOST_ASIO_HAS_CO_AWAIT)
93-
auto main() -> int {std::cout << "Requires coroutine support." << std::endl; return 0;}
9475
#endif // defined(BOOST_ASIO_HAS_CO_AWAIT)
File renamed without changes.

examples/common/main.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* Copyright (c) 2018-2022 Marcelo Zimbres Silva ([email protected])
2+
*
3+
* Distributed under the Boost Software License, Version 1.0. (See
4+
* accompanying file LICENSE.txt)
5+
*/
6+
7+
#include <iostream>
8+
#include <boost/asio.hpp>
9+
#if defined(BOOST_ASIO_HAS_CO_AWAIT)
10+
11+
namespace net = boost::asio;
12+
extern net::awaitable<void> async_main();
13+
14+
// The main function used in our examples.
15+
auto main() -> int
16+
{
17+
try {
18+
net::io_context ioc;
19+
net::co_spawn(ioc, async_main(), net::detached);
20+
ioc.run();
21+
} catch (std::exception const& e) {
22+
std::cerr << "Error: " << e.what() << std::endl;
23+
return 1;
24+
}
25+
}
26+
27+
#else // defined(BOOST_ASIO_HAS_CO_AWAIT)
28+
auto main() -> int {std::cout << "Requires coroutine support." << std::endl; return 0;}
29+
#endif // defined(BOOST_ASIO_HAS_CO_AWAIT)

examples/containers.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <map>
1212
#include <vector>
1313

14-
#include "common.hpp"
14+
#include "common/common.hpp"
1515

1616
namespace net = boost::asio;
1717
using namespace net::experimental::awaitable_operators;
@@ -92,7 +92,7 @@ auto transaction(std::shared_ptr<connection> conn) -> net::awaitable<void>
9292
print(std::get<1>(std::get<4>(resp)).value());
9393
}
9494

95-
// Called from the main function (see common.cpp)
95+
// Called from the main function (see main.cpp)
9696
net::awaitable<void> async_main()
9797
{
9898
auto conn = std::make_shared<connection>(co_await net::this_coro::executor);

examples/echo_server.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#if defined(BOOST_ASIO_HAS_CO_AWAIT)
99
#include <boost/asio/experimental/awaitable_operators.hpp>
1010
#include <aedis.hpp>
11-
#include "common.hpp"
11+
#include "common/common.hpp"
1212

1313
namespace net = boost::asio;
1414
using namespace net::experimental::awaitable_operators;
@@ -44,7 +44,7 @@ auto listener(std::shared_ptr<connection> conn) -> net::awaitable<void>
4444
net::co_spawn(ex, echo_server_session(co_await acc.async_accept(), conn), net::detached);
4545
}
4646

47-
// Called from the main function (see common.cpp)
47+
// Called from the main function (see main.cpp)
4848
auto async_main() -> net::awaitable<void>
4949
{
5050
auto ex = co_await net::this_coro::executor;

examples/intro.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
#if defined(BOOST_ASIO_HAS_CO_AWAIT)
99
#include <boost/asio/experimental/awaitable_operators.hpp>
1010
#include <aedis.hpp>
11-
#include "common.hpp"
11+
#include "common/common.hpp"
1212

1313
namespace net = boost::asio;
1414
using namespace net::experimental::awaitable_operators;
1515
using aedis::adapt;
1616
using aedis::resp3::request;
1717

18-
// Called from the main function (see common.cpp)
19-
net::awaitable<void> async_main()
18+
// Called from the main function (see main.cpp)
19+
auto async_main() -> net::awaitable<void>
2020
{
2121
request req;
2222
req.get_config().cancel_on_connection_lost = true;

0 commit comments

Comments
 (0)