Skip to content

Commit

Permalink
Merge branch 'master' into os-add-docstring-to-tsmadd
Browse files Browse the repository at this point in the history
  • Loading branch information
ofekshenawa authored Dec 17, 2023
2 parents 9d4ac15 + 631deaf commit 19f330f
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:

steps:
- name: Set up ${{ matrix.go-version }}
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/doctests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:

steps:
- name: Set up ${{ matrix.go-version }}
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/spellcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v4
- name: Check Spelling
uses: rojopolis/spellcheck-github-actions@0.34.0
uses: rojopolis/spellcheck-github-actions@0.35.0
with:
config_path: .github/spellcheck-settings.yml
task_name: Markdown
2 changes: 1 addition & 1 deletion .github/workflows/stale-issues.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

runs-on: ubuntu-latest
steps:
- uses: actions/stale@v8
- uses: actions/stale@v9
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-issue-message: 'This issue is marked stale. It will be closed in 30 days if it is not updated.'
Expand Down
7 changes: 4 additions & 3 deletions internal/proto/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ var _ = Describe("WriteArg", func() {
})

args := map[any]string{
"hello": "$1\r\nhello\r\n",
"hello": "$5\r\nhello\r\n",
int(10): "$2\r\n10\r\n",
util.ToPtr(int(10)): "$2\r\n10\r\n",
int8(10): "$2\r\n10\r\n",
Expand All @@ -133,8 +133,8 @@ var _ = Describe("WriteArg", func() {
util.ToPtr(uint32(10)): "$2\r\n10\r\n",
uint64(10): "$2\r\n10\r\n",
util.ToPtr(uint64(10)): "$2\r\n10\r\n",
float32(10.3): "$4\r\n10.3\r\n",
util.ToPtr(float32(10.3)): "$4\r\n10.3\r\n",
float32(10.3): "$18\r\n10.300000190734863\r\n",
util.ToPtr(float32(10.3)): "$18\r\n10.300000190734863\r\n",
float64(10.3): "$4\r\n10.3\r\n",
util.ToPtr(float64(10.3)): "$4\r\n10.3\r\n",
bool(true): "$1\r\n1\r\n",
Expand All @@ -144,6 +144,7 @@ var _ = Describe("WriteArg", func() {
}

for arg, expect := range args {
arg, expect := arg, expect
It(fmt.Sprintf("should write arg of type %T", arg), func() {
err := wr.WriteArg(arg)
Expect(err).NotTo(HaveOccurred())
Expand Down
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 19f330f

Please sign in to comment.