RedisConnection::WriteRESP: Use one asio::async_write instead of many

This commit is contained in:
Noah Hilverling 2021-03-17 13:34:07 +01:00
parent 160d0ea371
commit 5c8b32618c
1 changed files with 6 additions and 29 deletions

View File

@ -139,9 +139,6 @@ namespace icinga
template<class AsyncWriteStream>
static void WriteRESP(AsyncWriteStream& stream, const Query& query, boost::asio::yield_context& yc);
template<class AsyncWriteStream>
static void WriteInt(AsyncWriteStream& stream, intmax_t i, boost::asio::yield_context& yc);
RedisConnection(boost::asio::io_context& io, String host, int port, String path, String password, int db);
void Connect(boost::asio::yield_context& yc);
@ -493,36 +490,16 @@ void RedisConnection::WriteRESP(AsyncWriteStream& stream, const Query& query, bo
{
namespace asio = boost::asio;
asio::async_write(stream, asio::const_buffer("*", 1), yc);
WriteInt(stream, query.size(), yc);
asio::async_write(stream, asio::const_buffer("\r\n", 2), yc);
asio::streambuf writeBuffer;
std::ostream msg(&writeBuffer);
msg << "*" << query.size() << "\r\n";
for (auto& arg : query) {
asio::async_write(stream, asio::const_buffer("$", 1), yc);
WriteInt(stream, arg.GetLength(), yc);
asio::async_write(stream, asio::const_buffer("\r\n", 2), yc);
asio::async_write(stream, asio::const_buffer(arg.CStr(), arg.GetLength()), yc);
asio::async_write(stream, asio::const_buffer("\r\n", 2), yc);
msg << "$" << arg.GetLength() << "\r\n" << arg << "\r\n";
}
}
/**
* Write a Redis protocol int to stream
*
* @param stream Redis server connection
* @param i Redis protocol int
*/
template<class AsyncWriteStream>
void RedisConnection::WriteInt(AsyncWriteStream& stream, intmax_t i, boost::asio::yield_context& yc)
{
namespace asio = boost::asio;
char buf[21] = {};
sprintf(buf, "%jd", i);
asio::async_write(stream, asio::const_buffer(buf, strlen(buf)), yc);
asio::async_write(stream, writeBuffer, yc);
}
}