fix: #2730 data race at hooksMixin (#2814)

This commit is contained in:
RyoMiyashita 2023-12-10 19:04:13 +09:00 committed by GitHub
parent 21bd40a47e
commit a109302230
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"net" "net"
"sync"
"sync/atomic" "sync/atomic"
"time" "time"
@ -40,12 +41,15 @@ type (
) )
type hooksMixin struct { type hooksMixin struct {
hooksMu *sync.Mutex
slice []Hook slice []Hook
initial hooks initial hooks
current hooks current hooks
} }
func (hs *hooksMixin) initHooks(hooks hooks) { func (hs *hooksMixin) initHooks(hooks hooks) {
hs.hooksMu = new(sync.Mutex)
hs.initial = hooks hs.initial = hooks
hs.chain() hs.chain()
} }
@ -116,6 +120,9 @@ func (hs *hooksMixin) AddHook(hook Hook) {
func (hs *hooksMixin) chain() { func (hs *hooksMixin) chain() {
hs.initial.setDefaults() hs.initial.setDefaults()
hs.hooksMu.Lock()
defer hs.hooksMu.Unlock()
hs.current.dial = hs.initial.dial hs.current.dial = hs.initial.dial
hs.current.process = hs.initial.process hs.current.process = hs.initial.process
hs.current.pipeline = hs.initial.pipeline hs.current.pipeline = hs.initial.pipeline
@ -138,9 +145,13 @@ func (hs *hooksMixin) chain() {
} }
func (hs *hooksMixin) clone() hooksMixin { func (hs *hooksMixin) clone() hooksMixin {
hs.hooksMu.Lock()
defer hs.hooksMu.Unlock()
clone := *hs clone := *hs
l := len(clone.slice) l := len(clone.slice)
clone.slice = clone.slice[:l:l] clone.slice = clone.slice[:l:l]
clone.hooksMu = new(sync.Mutex)
return clone return clone
} }
@ -165,6 +176,8 @@ func (hs *hooksMixin) withProcessPipelineHook(
} }
func (hs *hooksMixin) dialHook(ctx context.Context, network, addr string) (net.Conn, error) { 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) return hs.current.dial(ctx, network, addr)
} }

View File

@ -579,3 +579,53 @@ var _ = Describe("Hook", func() {
Expect(cmd.Val()).To(Equal("Script and hook")) 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",
}))
})
})