Skip to content

Commit 10603b7

Browse files
committed
Sends SELECT right after HELLO after a connection.
1 parent ad3c291 commit 10603b7

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,15 @@ https://lists.boost.org/Archives/boost/2023/01/253944.php.
676676

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

679+
* Adds `boost::redis::config::database_index` to make it possible to
680+
choose a database before starting running commands e.g. after an
681+
automatic reconnection.
682+
683+
* Massive performance improvement. One of my tests went from
684+
140k req/s to 390k/s. This was possible after a parser
685+
simplification that reduced the number of reschedules and buffer
686+
rotations.
687+
679688
* Adds Redis stream example.
680689

681690
* Renames the project to Boost.Redis and moves the code into namespace

include/boost/redis/config.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <string>
1111
#include <chrono>
12+
#include <optional>
1213

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

52+
/// Database that will be passed to the [SELECT](https://redis.io/commands/hello/) command.
53+
std::optional<int> database_index = 0;
54+
5155
/// Message used by the health-checker in `boost::redis::connection::async_run`.
5256
std::string health_check_id = "Boost.Redis";
5357

include/boost/redis/detail/runner.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ class runner {
231231
hello_req_.push("HELLO", "3", "AUTH", cfg_.username, cfg_.password);
232232
else
233233
hello_req_.push("HELLO", "3", "SETNAME", cfg_.clientname);
234+
235+
if (cfg_.database_index)
236+
hello_req_.push("SELECT", cfg_.database_index.value());
234237
}
235238

236239
resolver_type resv_;

tests/test_conn_exec.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ namespace net = boost::asio;
2020
using boost::redis::connection;
2121
using boost::redis::request;
2222
using boost::redis::response;
23+
using boost::redis::generic_response;
2324
using boost::redis::ignore;
2425
using boost::redis::operation;
26+
using boost::redis::config;
2527

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

118120
ioc.run();
119121
}
122+
123+
BOOST_AUTO_TEST_CASE(correct_database)
124+
{
125+
config cfg;
126+
cfg.database_index = 2;
127+
128+
net::io_context ioc;
129+
130+
auto conn = std::make_shared<connection>(ioc);
131+
132+
request req;
133+
req.push("CLIENT", "LIST");
134+
135+
generic_response resp;
136+
137+
conn->async_exec(req, resp, [&](auto ec, auto n){
138+
BOOST_TEST(!ec);
139+
std::clog << "async_exec has completed: " << n << std::endl;
140+
conn->cancel();
141+
});
142+
143+
conn->async_run(cfg, {}, [](auto){
144+
std::clog << "async_run has exited." << std::endl;
145+
});
146+
147+
ioc.run();
148+
149+
assert(!resp.value().empty());
150+
auto const& value = resp.value().front().value;
151+
auto const pos = value.find("db=");
152+
auto const index_str = value.substr(pos + 3, 1);
153+
auto const index = std::stoi(index_str);
154+
BOOST_CHECK_EQUAL(cfg.database_index.value(), index);
155+
}
156+

0 commit comments

Comments
 (0)