Loading...
Searching...
No Matches
cpp20_streams.cpp
1/* Copyright (c) 2018-2022 Marcelo Zimbres Silva (mzimbres@gmail.com)
2 *
3 * Distributed under the Boost Software License, Version 1.0. (See
4 * accompanying file LICENSE.txt)
5 */
6
7#include <boost/redis/connection.hpp>
8
9#include <boost/asio/awaitable.hpp>
10#include <boost/asio/co_spawn.hpp>
11#include <boost/asio/consign.hpp>
12#include <boost/asio/detached.hpp>
13#include <boost/asio/signal_set.hpp>
14
15#include <iostream>
16
17#if defined(BOOST_ASIO_HAS_CO_AWAIT)
18
19#include <memory>
20#include <string>
21#include <thread>
22#include <vector>
23
24namespace net = boost::asio;
30using net::signal_set;
31
32auto stream_reader(std::shared_ptr<connection> conn) -> net::awaitable<void>
33{
34 std::string redisStreamKey_;
35 request req;
37
38 std::string stream_id{"$"};
39 std::string const field = "myfield";
40
41 for (;;) {
42 req.push("XREAD", "BLOCK", "0", "STREAMS", "test-topic", stream_id);
43 co_await conn->async_exec(req, resp);
44
45 //std::cout << "Response: ";
46 //for (auto i = 0UL; i < resp->size(); ++i) {
47 // std::cout << resp->at(i).value << ", ";
48 //}
49 //std::cout << std::endl;
50
51 // The following approach was taken in order to be able to
52 // deal with the responses, as generated by redis in the case
53 // that there are multiple stream 'records' within a single
54 // generic_response. The nesting and number of values in
55 // resp.value() are different, depending on the contents
56 // of the stream in redis. Uncomment the above commented-out
57 // code for examples while running the XADD command.
58
59 std::size_t item_index = 0;
60 while (item_index < std::size(resp.value())) {
61 auto const& val = resp.value().at(item_index).value;
62
63 if (field.compare(val) == 0) {
64 // We've hit a myfield field.
65 // The streamId is located at item_index - 2
66 // The payload is located at item_index + 1
67 stream_id = resp.value().at(item_index - 2).value;
68 std::cout << "StreamId: " << stream_id << ", "
69 << "MyField: " << resp.value().at(item_index + 1).value << std::endl;
70 ++item_index; // We can increase so we don't read this again
71 }
72
73 ++item_index;
74 }
75
76 req.clear();
77 resp.value().clear();
78 }
79}
80
81// Run this in another terminal:
82// redis-cli -r 100000 -i 0.0001 XADD "test-topic" "*" "myfield" "myfieldvalue1"
83auto co_main(config cfg) -> net::awaitable<void>
84{
85 auto ex = co_await net::this_coro::executor;
86 auto conn = std::make_shared<connection>(ex);
87 net::co_spawn(ex, stream_reader(conn), net::detached);
88
89 // Disable health checks.
90 cfg.health_check_interval = std::chrono::seconds::zero();
91 conn->async_run(cfg, net::consign(net::detached, conn));
92
93 signal_set sig_set(ex, SIGINT, SIGTERM);
94 co_await sig_set.async_wait();
95 conn->cancel();
96}
97#endif // defined(BOOST_ASIO_HAS_CO_AWAIT)
A basic_connection that type erases the executor.
Creates Redis requests.
Definition request.hpp:46
operation
Connection operations that can be cancelled.
Definition operation.hpp:19
adapter::result< std::vector< resp3::node > > generic_response
A generic response to a request.
Definition response.hpp:35
Configure parameters used by the connection classes.
Definition config.hpp:30