From 03a86486eabb98988220415834dc9da06abb7d6d Mon Sep 17 00:00:00 2001 From: Wesley Powell Date: Fri, 16 Jul 2021 02:41:17 -0500 Subject: [PATCH] Backport wrapMultiExec() cmds copy bug fix from v8 (#1823) --- .gitignore | 1 + pipeline_test.go | 17 +++++++++++++++++ redis.go | 10 +++++----- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index ebfe903bc..b975a7b4c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.rdb testdata/*/ +.idea/ diff --git a/pipeline_test.go b/pipeline_test.go index fb82e4eb4..a00da7a77 100644 --- a/pipeline_test.go +++ b/pipeline_test.go @@ -1,6 +1,8 @@ package redis_test import ( + "strconv" + "github.com/go-redis/redis/v7" . "github.com/onsi/ginkgo" @@ -67,6 +69,21 @@ var _ = Describe("pipelining", func() { Expect(err).NotTo(HaveOccurred()) Expect(cmds).To(HaveLen(1)) }) + + It("handles large pipelines", func() { + for callCount := 1; callCount < 16; callCount++ { + for i := 1; i <= callCount; i++ { + pipe.SetNX(strconv.Itoa(i)+"_key", strconv.Itoa(i)+"_value", 0) + } + + cmds, err := pipe.Exec() + Expect(err).NotTo(HaveOccurred()) + Expect(cmds).To(HaveLen(callCount)) + for _, cmd := range cmds { + Expect(cmd).To(BeAssignableToTypeOf(&redis.BoolCmd{})) + } + } + }) } Describe("Pipeline", func() { diff --git a/redis.go b/redis.go index 3d9dfed7d..ac0569f5a 100644 --- a/redis.go +++ b/redis.go @@ -473,11 +473,11 @@ func wrapMultiExec(cmds []Cmder) []Cmder { if len(cmds) == 0 { panic("not reached") } - cmds = append(cmds, make([]Cmder, 2)...) - copy(cmds[1:], cmds[:len(cmds)-2]) - cmds[0] = NewStatusCmd("multi") - cmds[len(cmds)-1] = NewSliceCmd("exec") - return cmds + cmdCopy := make([]Cmder, len(cmds)+2) + cmdCopy[0] = NewStatusCmd("multi") + copy(cmdCopy[1:], cmds) + cmdCopy[len(cmdCopy)-1] = NewSliceCmd("exec") + return cmdCopy } func txPipelineReadQueued(rd *proto.Reader, statusCmd *StatusCmd, cmds []Cmder) error {