Skip to content

Commit

Permalink
fix: #2730 data race at hooksMixin (#2814)
Browse files Browse the repository at this point in the history
  • Loading branch information
RyoMiyashita committed Dec 10, 2023
1 parent 21bd40a commit a109302
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
13 changes: 13 additions & 0 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net"
"sync"
"sync/atomic"
"time"

Expand Down Expand Up @@ -40,12 +41,15 @@ type (
)

type hooksMixin struct {
hooksMu *sync.Mutex

slice []Hook
initial hooks
current hooks
}

func (hs *hooksMixin) initHooks(hooks hooks) {
hs.hooksMu = new(sync.Mutex)
hs.initial = hooks
hs.chain()
}
Expand Down Expand Up @@ -116,6 +120,9 @@ func (hs *hooksMixin) AddHook(hook Hook) {
func (hs *hooksMixin) chain() {
hs.initial.setDefaults()

hs.hooksMu.Lock()
defer hs.hooksMu.Unlock()

hs.current.dial = hs.initial.dial
hs.current.process = hs.initial.process
hs.current.pipeline = hs.initial.pipeline
Expand All @@ -138,9 +145,13 @@ func (hs *hooksMixin) chain() {
}

func (hs *hooksMixin) clone() hooksMixin {
hs.hooksMu.Lock()
defer hs.hooksMu.Unlock()

clone := *hs
l := len(clone.slice)
clone.slice = clone.slice[:l:l]
clone.hooksMu = new(sync.Mutex)
return clone
}

Expand All @@ -165,6 +176,8 @@ func (hs *hooksMixin) withProcessPipelineHook(
}

func (hs *hooksMixin) dialHook(ctx context.Context, network, addr string) (net.Conn, error) {
hs.hooksMu.Lock()
defer hs.hooksMu.Unlock()
return hs.current.dial(ctx, network, addr)
}

Expand Down
50 changes: 50 additions & 0 deletions redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,3 +579,53 @@ var _ = Describe("Hook", func() {
Expect(cmd.Val()).To(Equal("Script and hook"))
})
})

var _ = Describe("Hook with MinIdleConns", func() {
var client *redis.Client

BeforeEach(func() {
options := redisOptions()
options.MinIdleConns = 1
client = redis.NewClient(options)
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
})

AfterEach(func() {
err := client.Close()
Expect(err).NotTo(HaveOccurred())
})

It("fifo", func() {
var res []string
client.AddHook(&hook{
processHook: func(hook redis.ProcessHook) redis.ProcessHook {
return func(ctx context.Context, cmd redis.Cmder) error {
res = append(res, "hook-1-process-start")
err := hook(ctx, cmd)
res = append(res, "hook-1-process-end")
return err
}
},
})
client.AddHook(&hook{
processHook: func(hook redis.ProcessHook) redis.ProcessHook {
return func(ctx context.Context, cmd redis.Cmder) error {
res = append(res, "hook-2-process-start")
err := hook(ctx, cmd)
res = append(res, "hook-2-process-end")
return err
}
},
})

err := client.Ping(ctx).Err()
Expect(err).NotTo(HaveOccurred())

Expect(res).To(Equal([]string{
"hook-1-process-start",
"hook-2-process-start",
"hook-2-process-end",
"hook-1-process-end",
}))
})
})

0 comments on commit a109302

Please sign in to comment.