File: cpp20_streams.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (97 lines) | stat: -rw-r--r-- 3,082 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* Copyright (c) 2018-2022 Marcelo Zimbres Silva (mzimbres@gmail.com)
 *
 * Distributed under the Boost Software License, Version 1.0. (See
 * accompanying file LICENSE.txt)
 */

#include <boost/redis/connection.hpp>

#include <boost/asio/awaitable.hpp>
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/consign.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/signal_set.hpp>

#include <iostream>

#if defined(BOOST_ASIO_HAS_CO_AWAIT)

#include <memory>
#include <string>
#include <thread>
#include <vector>

namespace net = boost::asio;
using boost::redis::config;
using boost::redis::generic_response;
using boost::redis::operation;
using boost::redis::request;
using boost::redis::connection;
using net::signal_set;

auto stream_reader(std::shared_ptr<connection> conn) -> net::awaitable<void>
{
   std::string redisStreamKey_;
   request req;
   generic_response resp;

   std::string stream_id{"$"};
   std::string const field = "myfield";

   for (;;) {
      req.push("XREAD", "BLOCK", "0", "STREAMS", "test-topic", stream_id);
      co_await conn->async_exec(req, resp);

      //std::cout << "Response: ";
      //for (auto i = 0UL; i < resp->size(); ++i) {
      //    std::cout << resp->at(i).value << ", ";
      //}
      //std::cout << std::endl;

      // The following approach was taken in order to be able to
      // deal with the responses, as generated by redis in the case
      // that there are multiple stream 'records' within a single
      // generic_response.  The nesting and number of values in
      // resp.value() are different, depending on the contents
      // of the stream in redis.  Uncomment the above commented-out
      // code for examples while running the XADD command.

      std::size_t item_index = 0;
      while (item_index < std::size(resp.value())) {
         auto const& val = resp.value().at(item_index).value;

         if (field.compare(val) == 0) {
            // We've hit a myfield field.
            // The streamId is located at item_index - 2
            // The payload is located at item_index + 1
            stream_id = resp.value().at(item_index - 2).value;
            std::cout << "StreamId: " << stream_id << ", "
                      << "MyField: " << resp.value().at(item_index + 1).value << std::endl;
            ++item_index;  // We can increase so we don't read this again
         }

         ++item_index;
      }

      req.clear();
      resp.value().clear();
   }
}

// Run this in another terminal:
// redis-cli -r 100000 -i 0.0001 XADD "test-topic" "*" "myfield" "myfieldvalue1"
auto co_main(config cfg) -> net::awaitable<void>
{
   auto ex = co_await net::this_coro::executor;
   auto conn = std::make_shared<connection>(ex);
   net::co_spawn(ex, stream_reader(conn), net::detached);

   // Disable health checks.
   cfg.health_check_interval = std::chrono::seconds::zero();
   conn->async_run(cfg, net::consign(net::detached, conn));

   signal_set sig_set(ex, SIGINT, SIGTERM);
   co_await sig_set.async_wait();
   conn->cancel();
}
#endif  // defined(BOOST_ASIO_HAS_CO_AWAIT)