-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'iwanbk-add-get-timeout', fixes #22
- Loading branch information
Showing
6 changed files
with
181 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// +build !go1.7 | ||
|
||
package redisc | ||
|
||
import ( | ||
"github.com/gomodule/redigo/redis" | ||
) | ||
|
||
// get connection from the pool | ||
// pre go1.7, Pool has no GetContext method, so it always | ||
// calls Get. | ||
func (c *Cluster) getFromPool(p *redis.Pool) (redis.Conn, error) { | ||
conn := p.Get() | ||
return conn, conn.Err() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// +build !go1.7 | ||
|
||
package redisc | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gomodule/redigo/redis" | ||
"github.com/mna/redisc/redistest" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// TestGetPool | ||
func TestGetPool(t *testing.T) { | ||
s := redistest.StartMockServer(t, func(cmd string, args ...string) interface{} { | ||
return nil | ||
}) | ||
defer s.Close() | ||
|
||
p := &redis.Pool{ | ||
MaxActive: 1, | ||
Dial: func() (redis.Conn, error) { | ||
return redis.Dial("tcp", s.Addr) | ||
}, | ||
} | ||
c := Cluster{} | ||
|
||
// fist connection is OK | ||
conn, err := c.getFromPool(p) | ||
if assert.NoError(t, err) { | ||
defer conn.Close() | ||
} | ||
|
||
// second connection should be failed because we only have 1 MaxActive | ||
_, err = c.getFromPool(p) | ||
assert.Error(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// +build go1.7 | ||
|
||
package redisc | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/gomodule/redigo/redis" | ||
) | ||
|
||
// get connection from the pool. | ||
// use GetContext if PoolWaitTime > 0 | ||
func (c *Cluster) getFromPool(p *redis.Pool) (redis.Conn, error) { | ||
if c.PoolWaitTime <= 0 { | ||
conn := p.Get() | ||
return conn, conn.Err() | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), c.PoolWaitTime) | ||
defer cancel() | ||
|
||
return p.GetContext(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// +build go1.7 | ||
|
||
package redisc | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/gomodule/redigo/redis" | ||
"github.com/mna/redisc/redistest" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// TestGetPoolTimedOut test case where we can't get the connection because the pool | ||
// is full | ||
func TestGetPoolTimedOut(t *testing.T) { | ||
s := redistest.StartMockServer(t, func(cmd string, args ...string) interface{} { | ||
return nil | ||
}) | ||
defer s.Close() | ||
|
||
p := &redis.Pool{ | ||
MaxActive: 1, | ||
Dial: func() (redis.Conn, error) { | ||
return redis.Dial("tcp", s.Addr) | ||
}, | ||
Wait: true, | ||
} | ||
c := Cluster{ | ||
PoolWaitTime: 100 * time.Millisecond, | ||
} | ||
conn, err := c.getFromPool(p) | ||
if assert.NoError(t, err) { | ||
defer conn.Close() | ||
} | ||
|
||
// second connection should be failed because we only have 1 MaxActive | ||
start := time.Now() | ||
_, err = c.getFromPool(p) | ||
if assert.Error(t, err) { | ||
assert.Equal(t, context.DeadlineExceeded, err) | ||
assert.True(t, time.Since(start) >= 100*time.Millisecond) | ||
} | ||
} | ||
|
||
// TestGetPoolWaitOnFull test that we could get the connection when the pool | ||
// is full and we can wait for it | ||
func TestGetPoolWaitOnFull(t *testing.T) { | ||
s := redistest.StartMockServer(t, func(cmd string, args ...string) interface{} { | ||
return nil | ||
}) | ||
defer s.Close() | ||
|
||
var ( | ||
usageTime = 100 * time.Millisecond // how long the connection will be used | ||
waitTime = 3 * usageTime // how long we want to wait | ||
) | ||
|
||
p := &redis.Pool{ | ||
MaxActive: 1, | ||
Dial: func() (redis.Conn, error) { | ||
return redis.Dial("tcp", s.Addr) | ||
}, | ||
Wait: true, | ||
} | ||
c := Cluster{ | ||
PoolWaitTime: waitTime, | ||
} | ||
|
||
// first connection OK | ||
conn, err := c.getFromPool(p) | ||
assert.NoError(t, err) | ||
|
||
// second connection should be failed because we only have 1 MaxActive | ||
start := time.Now() | ||
_, err = c.getFromPool(p) | ||
if assert.Error(t, err) { | ||
assert.Equal(t, context.DeadlineExceeded, err) | ||
assert.True(t, time.Since(start) >= waitTime) | ||
} | ||
|
||
go func() { | ||
time.Sleep(usageTime) // sleep before close, to simulate waiting for connection | ||
conn.Close() | ||
}() | ||
|
||
start = time.Now() | ||
conn2, err := c.getFromPool(p) | ||
if assert.NoError(t, err) { | ||
assert.True(t, time.Since(start) >= usageTime) | ||
} | ||
conn2.Close() | ||
} |