Skip to content

Commit

Permalink
Sends SELECT right after HELLO after a connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
mzimbres committed Aug 6, 2023
1 parent ad3c291 commit 10603b7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 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
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 10603b7

Please sign in to comment.