fix race detector checking failed about hook

This commit is contained in:
skyjiang 2023-08-22 17:09:42 +08:00
parent c3098d5f7e
commit a0e3d7e82f
1 changed files with 13 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,6 +41,7 @@ type (
) )
type hooksMixin struct { type hooksMixin struct {
mu sync.RWMutex
slice []Hook slice []Hook
initial hooks initial hooks
current hooks current hooks
@ -116,6 +118,9 @@ func (hs *hooksMixin) AddHook(hook Hook) {
func (hs *hooksMixin) chain() { func (hs *hooksMixin) chain() {
hs.initial.setDefaults() hs.initial.setDefaults()
hs.mu.Lock()
defer hs.mu.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
@ -165,18 +170,26 @@ 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.mu.RLock()
defer hs.mu.RUnlock()
return hs.current.dial(ctx, network, addr) return hs.current.dial(ctx, network, addr)
} }
func (hs *hooksMixin) processHook(ctx context.Context, cmd Cmder) error { func (hs *hooksMixin) processHook(ctx context.Context, cmd Cmder) error {
hs.mu.RLock()
defer hs.mu.RUnlock()
return hs.current.process(ctx, cmd) return hs.current.process(ctx, cmd)
} }
func (hs *hooksMixin) processPipelineHook(ctx context.Context, cmds []Cmder) error { func (hs *hooksMixin) processPipelineHook(ctx context.Context, cmds []Cmder) error {
hs.mu.RLock()
defer hs.mu.RUnlock()
return hs.current.pipeline(ctx, cmds) return hs.current.pipeline(ctx, cmds)
} }
func (hs *hooksMixin) processTxPipelineHook(ctx context.Context, cmds []Cmder) error { func (hs *hooksMixin) processTxPipelineHook(ctx context.Context, cmds []Cmder) error {
hs.mu.RLock()
defer hs.mu.RUnlock()
return hs.current.txPipeline(ctx, cmds) return hs.current.txPipeline(ctx, cmds)
} }