Skip to content

Commit bd964e5

Browse files
authored
fix: open connection count (gomodule#527)
This commit releases back the connection in the special case, so that correct count is always maintained.
1 parent a7bb38d commit bd964e5

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

redis/pool.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ func (p *Pool) waitVacantConn(ctx context.Context) (waited time.Duration, err er
377377
// because `select` picks a random `case` if several of them are "ready".
378378
select {
379379
case <-ctx.Done():
380+
p.ch <- struct{}{}
380381
return 0, ctx.Err()
381382
default:
382383
}

redis/pool_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,32 @@ func TestWaitPoolGetContext(t *testing.T) {
825825
defer c.Close()
826826
}
827827

828+
func TestWaitPoolGetContextIssue520(t *testing.T) {
829+
d := poolDialer{t: t}
830+
p := &redis.Pool{
831+
MaxIdle: 1,
832+
MaxActive: 1,
833+
Dial: d.dial,
834+
Wait: true,
835+
}
836+
defer p.Close()
837+
ctx1, cancel1 := context.WithTimeout(context.Background(), 1*time.Nanosecond)
838+
defer cancel1()
839+
c, err := p.GetContext(ctx1)
840+
if err != context.DeadlineExceeded {
841+
t.Fatalf("GetContext returned %v", err)
842+
}
843+
defer c.Close()
844+
845+
ctx2, cancel2 := context.WithCancel(context.Background())
846+
defer cancel2()
847+
c2, err := p.GetContext(ctx2)
848+
if err != nil {
849+
t.Fatalf("Get context returned %v", err)
850+
}
851+
defer c2.Close()
852+
}
853+
828854
func TestWaitPoolGetContextWithDialContext(t *testing.T) {
829855
d := poolDialer{t: t}
830856
p := &redis.Pool{

0 commit comments

Comments
 (0)