jamesge commented on a change in pull request #972: Redis server protocol
URL: https://github.com/apache/incubator-brpc/pull/972#discussion_r359137068
 
 

 ##########
 File path: src/brpc/policy/redis_protocol.cpp
 ##########
 @@ -52,62 +54,202 @@ struct InputResponse : public InputMessageBase {
     }
 };
 
-// "Message" = "Response" as we only implement the client for redis.
-ParseResult ParseRedisMessage(butil::IOBuf* source, Socket* socket,
-                              bool /*read_eof*/, const void* /*arg*/) {
-    if (source->empty()) {
-        return MakeParseError(PARSE_ERROR_NOT_ENOUGH_DATA);
+// This class is as parsing_context in socket.
+class RedisConnContext : public Destroyable  {
+public:
+    RedisConnContext()
+        : redis_service(NULL)
+        , batched_size(0) {}
+
+    ~RedisConnContext();
+    // @Destroyable
+    void Destroy() override;
+
+    SocketId socket_id;
+    RedisService* redis_service;
+    // If user starts a transaction, transaction_handler indicates the
+    // handler pointer that runs the transaction command.
+    std::unique_ptr<RedisCommandHandler> transaction_handler;
+    // >0 if command handler is run in batched mode.
+    int batched_size;
+
+    RedisCommandParser parser;
+};
+
+int ConsumeCommand(RedisConnContext* ctx,
+                   const std::unique_ptr<const char*[]>& commands,
+                   int command_len, butil::Arena* arena,
+                   bool is_last,
+                   butil::IOBuf* sendbuf) {
+    RedisReply output(arena);
+    RedisCommandHandler::Result result = RedisCommandHandler::OK;
+    if (ctx->transaction_handler) {
+        result = ctx->transaction_handler->Run(
+                command_len, commands.get(), &output, is_last);
+        if (result == RedisCommandHandler::OK) {
+            ctx->transaction_handler.reset(NULL);
+        } else if (result == RedisCommandHandler::BATCHED) {
+            LOG(ERROR) << "BATCHED should not be returned by a transaction 
handler.";
+            return -1;
+        }
+    } else {
+        RedisCommandHandler* ch = 
ctx->redis_service->FindCommandHandler(commands[0]);
+        if (!ch) {
+            char buf[64];
+            snprintf(buf, sizeof(buf), "ERR unknown command `%s`", 
commands[0]);
+            output.SetError(buf);
+        } else {
+            result = ch->Run(command_len, commands.get(), &output, is_last);
+            if (result == RedisCommandHandler::CONTINUE) {
+                if (ctx->batched_size) {
 
 Review comment:
   batch应该对用户透明把,这么实现不是限制用户的行为了?这就意味着只要command 
handler看到is_last为false,几乎都无法返回continue,不太合理。

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to