From ec47a58ba1246e806ff09f1b25ee3b572680c2d7 Mon Sep 17 00:00:00 2001 From: Chris Rice Date: Thu, 6 Jul 2023 15:13:07 -0700 Subject: [PATCH 1/4] Fix ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / PING / QUIT allowed in this context #694 --- internal/server/pubsub.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/internal/server/pubsub.go b/internal/server/pubsub.go index 4016347f..0e440441 100644 --- a/internal/server/pubsub.go +++ b/internal/server/pubsub.go @@ -205,6 +205,23 @@ func (s *Server) liveSubscription( write([]byte("+OK\r\n")) } } + writePing := func() { + switch msg.OutputType { + case JSON: + if len(msg.Args) > 1 { + write([]byte(`{"ok":true,"ping":` + jsonString(msg.Args[1]) + `,"elapsed":"` + time.Since(start).String() + `"}`)) + } else { + write([]byte(`{"ok":true,"ping":"pong","elapsed":"` + time.Since(start).String() + `"}`)) + } + case RESP: + if len(msg.Args) > 1 { + data := redcon.AppendBulkString(nil, msg.Args[1]) + write(data) + } else { + write([]byte("+PONG\r\n")) + } + } + } writeWrongNumberOfArgsErr := func(command string) { switch outputType { case JSON: @@ -335,6 +352,9 @@ func (s *Server) liveSubscription( case "quit": writeOK() return nil + case "ping": + writePing() + continue case "psubscribe": kind, un = pubsubPattern, false case "punsubscribe": From 564b64a75818a818c3b0674ef33ab083872e0f16 Mon Sep 17 00:00:00 2001 From: Chris Rice Date: Thu, 6 Jul 2023 16:29:40 -0700 Subject: [PATCH 2/4] Syntax matching sugar --- internal/server/pubsub.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/server/pubsub.go b/internal/server/pubsub.go index 0e440441..ce185689 100644 --- a/internal/server/pubsub.go +++ b/internal/server/pubsub.go @@ -206,7 +206,7 @@ func (s *Server) liveSubscription( } } writePing := func() { - switch msg.OutputType { + switch outputType { case JSON: if len(msg.Args) > 1 { write([]byte(`{"ok":true,"ping":` + jsonString(msg.Args[1]) + `,"elapsed":"` + time.Since(start).String() + `"}`)) From 61b5023000611cc2832df6bb84d41d5c69fed670 Mon Sep 17 00:00:00 2001 From: Chris Rice Date: Thu, 6 Jul 2023 19:14:28 -0700 Subject: [PATCH 3/4] Change PING output to match REDIS protocol --- internal/server/pubsub.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/server/pubsub.go b/internal/server/pubsub.go index ce185689..22c973e8 100644 --- a/internal/server/pubsub.go +++ b/internal/server/pubsub.go @@ -215,7 +215,9 @@ func (s *Server) liveSubscription( } case RESP: if len(msg.Args) > 1 { - data := redcon.AppendBulkString(nil, msg.Args[1]) + data := redcon.AppendArray(nil, 2) + data = redcon.AppendBulkString(data, "PONG") + data = redcon.AppendBulkString(data, msg.Args[1]) write(data) } else { write([]byte("+PONG\r\n")) From c45170d211099edfc9a999dcf13e86470cb82efd Mon Sep 17 00:00:00 2001 From: Chris Rice Date: Wed, 26 Jul 2023 11:47:11 -0700 Subject: [PATCH 4/4] Use the right MSG variable? --- internal/server/pubsub.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/server/pubsub.go b/internal/server/pubsub.go index 22c973e8..f6faa9a5 100644 --- a/internal/server/pubsub.go +++ b/internal/server/pubsub.go @@ -205,23 +205,23 @@ func (s *Server) liveSubscription( write([]byte("+OK\r\n")) } } - writePing := func() { + writePing := func(m *Message) { switch outputType { case JSON: - if len(msg.Args) > 1 { - write([]byte(`{"ok":true,"ping":` + jsonString(msg.Args[1]) + `,"elapsed":"` + time.Since(start).String() + `"}`)) + if len(m.Args) > 1 { + write([]byte(`{"ok":true,"ping":` + jsonString(m.Args[1]) + `,"elapsed":"` + time.Since(start).String() + `"}`)) } else { write([]byte(`{"ok":true,"ping":"pong","elapsed":"` + time.Since(start).String() + `"}`)) } case RESP: - if len(msg.Args) > 1 { - data := redcon.AppendArray(nil, 2) - data = redcon.AppendBulkString(data, "PONG") - data = redcon.AppendBulkString(data, msg.Args[1]) - write(data) + data := redcon.AppendArray(nil, 2) + data = redcon.AppendBulkString(data, "PONG") + if len(m.Args) > 1 { + data = redcon.AppendBulkString(data, m.Args[1]) } else { - write([]byte("+PONG\r\n")) + data = redcon.AppendBulkString(data, "") } + write(data) } } writeWrongNumberOfArgsErr := func(command string) { @@ -355,7 +355,7 @@ func (s *Server) liveSubscription( writeOK() return nil case "ping": - writePing() + writePing(msg) continue case "psubscribe": kind, un = pubsubPattern, false