Skip to content

Commit

Permalink
Merge pull request #134 from boostorg/123-automatically-select-databa…
Browse files Browse the repository at this point in the history
…se-after-hello

123 automatically select database after hello
  • Loading branch information
mzimbres authored Aug 6, 2023
2 parents ad3c291 + 34ff1ce commit 81927de
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 5 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,15 @@ https://lists.boost.org/Archives/boost/2023/01/253944.php.

### develop (incorporates changes to conform the boost review and more)

* Adds `boost::redis::config::database_index` to make it possible to
choose a database before starting running commands e.g. after an
automatic reconnection.

* Massive performance improvement. One of my tests went from
140k req/s to 390k/s. This was possible after a parser
simplification that reduced the number of reschedules and buffer
rotations.

* Adds Redis stream example.

* Renames the project to Boost.Redis and moves the code into namespace
Expand Down
10 changes: 5 additions & 5 deletions examples/cpp20_streams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ auto stream_reader(std::shared_ptr<connection> conn) -> net::awaitable<void>
req.push("XREAD", "BLOCK", "0", "STREAMS", "test-topic", stream_id);
co_await conn->async_exec(req, resp, net::deferred);

// std::cout << "Response: ";
// for (int i = 0; i < resp->value().size(); ++i) {
// std::cout << resp->value().at(i).value << ", ";
// }
// std::cout << std::endl;
//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
Expand Down
4 changes: 4 additions & 0 deletions include/boost/redis/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <string>
#include <chrono>
#include <optional>

namespace boost::redis
{
Expand Down Expand Up @@ -48,6 +49,9 @@ struct config {
/// Client name parameter of the [HELLO](https://redis.io/commands/hello/) command.
std::string clientname = "Boost.Redis";

/// Database that will be passed to the [SELECT](https://redis.io/commands/hello/) command.
std::optional<int> database_index = 0;

/// Message used by the health-checker in `boost::redis::connection::async_run`.
std::string health_check_id = "Boost.Redis";

Expand Down
3 changes: 3 additions & 0 deletions include/boost/redis/detail/runner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ class runner {
hello_req_.push("HELLO", "3", "AUTH", cfg_.username, cfg_.password);
else
hello_req_.push("HELLO", "3", "SETNAME", cfg_.clientname);

if (cfg_.database_index)
hello_req_.push("SELECT", cfg_.database_index.value());
}

resolver_type resv_;
Expand Down
37 changes: 37 additions & 0 deletions tests/test_conn_exec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ namespace net = boost::asio;
using boost::redis::connection;
using boost::redis::request;
using boost::redis::response;
using boost::redis::generic_response;
using boost::redis::ignore;
using boost::redis::operation;
using boost::redis::config;

// Sends three requests where one of them has a hello with a priority
// set, which means it should be executed first.
Expand Down Expand Up @@ -117,3 +119,38 @@ BOOST_AUTO_TEST_CASE(cancel_request_if_not_connected)

ioc.run();
}

BOOST_AUTO_TEST_CASE(correct_database)
{
config cfg;
cfg.database_index = 2;

net::io_context ioc;

auto conn = std::make_shared<connection>(ioc);

request req;
req.push("CLIENT", "LIST");

generic_response resp;

conn->async_exec(req, resp, [&](auto ec, auto n){
BOOST_TEST(!ec);
std::clog << "async_exec has completed: " << n << std::endl;
conn->cancel();
});

conn->async_run(cfg, {}, [](auto){
std::clog << "async_run has exited." << std::endl;
});

ioc.run();

assert(!resp.value().empty());
auto const& value = resp.value().front().value;
auto const pos = value.find("db=");
auto const index_str = value.substr(pos + 3, 1);
auto const index = std::stoi(index_str);
BOOST_CHECK_EQUAL(cfg.database_index.value(), index);
}

0 comments on commit 81927de

Please sign in to comment.