This is an automated email from the ASF dual-hosted git repository.

twice pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/kvrocks.git


The following commit(s) were added to refs/heads/unstable by this push:
     new e2baeb1b Use resp functions instead of manual concatenation (#1717)
e2baeb1b is described below

commit e2baeb1b2cdeb480d02b74d4eff23f2c418bc3e7
Author: Twice <[email protected]>
AuthorDate: Fri Sep 1 10:24:58 2023 +0900

    Use resp functions instead of manual concatenation (#1717)
    
    Co-authored-by: Binbin <[email protected]>
---
 src/cluster/replication.cc | 12 +++++++-----
 src/server/redis_reply.h   |  4 ++--
 src/server/server.cc       |  8 +++-----
 3 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/src/cluster/replication.cc b/src/cluster/replication.cc
index 261c49a5..d6d8d281 100644
--- a/src/cluster/replication.cc
+++ b/src/cluster/replication.cc
@@ -54,7 +54,7 @@ Status FeedSlaveThread::Start() {
     sigaddset(&mask, SIGHUP);
     sigaddset(&mask, SIGPIPE);
     pthread_sigmask(SIG_BLOCK, &mask, &omask);
-    auto s = util::SockSend(conn_->GetFD(), "+OK\r\n");
+    auto s = util::SockSend(conn_->GetFD(), redis::SimpleString("OK"));
     if (!s.IsOK()) {
       LOG(ERROR) << "failed to send OK response to the replica: " << s.Msg();
       return;
@@ -372,11 +372,13 @@ ReplicationThread::CBState 
ReplicationThread::authWriteCB(bufferevent *bev) {
   return CBState::NEXT;
 }
 
+inline bool ResponseLineIsOK(const char *line) { return strncmp(line, "+OK", 
3) == 0; }
+
 ReplicationThread::CBState ReplicationThread::authReadCB(bufferevent *bev) {  
// NOLINT
   auto input = bufferevent_get_input(bev);
   UniqueEvbufReadln line(input, EVBUFFER_EOL_CRLF_STRICT);
   if (!line) return CBState::AGAIN;
-  if (strncmp(line.get(), "+OK", 3) != 0) {
+  if (!ResponseLineIsOK(line.get())) {
     // Auth failed
     LOG(ERROR) << "[replication] Auth failed: " << line.get();
     return CBState::RESTART;
@@ -447,7 +449,7 @@ ReplicationThread::CBState 
ReplicationThread::replConfReadCB(bufferevent *bev) {
     LOG(WARNING) << "The master was restoring the db, retry later";
     return CBState::RESTART;
   }
-  if (strncmp(line.get(), "+OK", 3) != 0) {
+  if (!ResponseLineIsOK(line.get())) {
     LOG(WARNING) << "[replication] Failed to replconf: " << line.get() + 1;
     //  backward compatible with old version that doesn't support replconf cmd
     return CBState::NEXT;
@@ -511,7 +513,7 @@ ReplicationThread::CBState 
ReplicationThread::tryPSyncReadCB(bufferevent *bev) {
     return CBState::PREV;
   }
 
-  if (strncmp(line.get(), "+OK", 3) != 0) {
+  if (!ResponseLineIsOK(line.get())) {
     // PSYNC isn't OK, we should use FullSync
     // Switch to fullsync state machine
     fullsync_steps_.Start();
@@ -802,7 +804,7 @@ Status ReplicationThread::sendAuth(int sock_fd) {
       }
       UniqueEvbufReadln line(evbuf.get(), EVBUFFER_EOL_CRLF_STRICT);
       if (!line) continue;
-      if (strncmp(line.get(), "+OK", 3) != 0) {
+      if (!ResponseLineIsOK(line.get())) {
         return {Status::NotOK, "auth got invalid response"};
       }
       break;
diff --git a/src/server/redis_reply.h b/src/server/redis_reply.h
index 1ddd2931..c3cc1b44 100644
--- a/src/server/redis_reply.h
+++ b/src/server/redis_reply.h
@@ -43,8 +43,8 @@ std::string Integer(T data) {
 std::string BulkString(const std::string &data);
 std::string NilString();
 
-template <typename IntegerType>
-std::string MultiLen(IntegerType len) {
+template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
+std::string MultiLen(T len) {
   return "*" + std::to_string(len) + CRLF;
 }
 
diff --git a/src/server/server.cc b/src/server/server.cc
index 3b583cbe..90e9505b 100644
--- a/src/server/server.cc
+++ b/src/server/server.cc
@@ -339,19 +339,17 @@ void Server::FeedMonitorConns(redis::Connection *conn, 
const std::vector<std::st
   if (monitor_clients_ <= 0) return;
 
   auto now = util::GetTimeStampUS();
-  std::string output = "+";
-  output += std::to_string(now / 1000000) + "." + std::to_string(now % 
1000000);
-  output += " [" + conn->GetNamespace() + " " + conn->GetAddr() + "]";
+  std::string output =
+      fmt::format("{}.{} [{} {}]", now / 1000000, now % 1000000, 
conn->GetNamespace(), conn->GetAddr());
   for (const auto &token : tokens) {
     output += " \"";
     output += util::EscapeString(token);
     output += "\"";
   }
-  output += CRLF;
 
   for (const auto &worker_thread : worker_threads_) {
     auto worker = worker_thread->GetWorker();
-    worker->FeedMonitorConns(conn, output);
+    worker->FeedMonitorConns(conn, redis::SimpleString(output));
   }
 }
 

Reply via email to