Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

123 automatically select database after hello #134

Merged
merged 2 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This link is wrong.

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);
}