Merge pull request #265 from go-redis/fix/cn-used-at

Set conn.UsedAt when connection is created. Fixes #263.
This commit is contained in:
Vladimir Mihailenco 2016-03-04 14:56:54 +02:00
commit f7d4933032
3 changed files with 30 additions and 9 deletions

13
conn.go
View File

@ -8,9 +8,10 @@ import (
const defaultBufSize = 4096 const defaultBufSize = 4096
var ( var noTimeout = time.Time{}
noTimeout = time.Time{}
) // Stubbed in tests.
var now = time.Now
type conn struct { type conn struct {
netcn net.Conn netcn net.Conn
@ -32,6 +33,8 @@ func newConnDialer(opt *Options) func() (*conn, error) {
cn := &conn{ cn := &conn{
netcn: netcn, netcn: netcn,
buf: make([]byte, defaultBufSize), buf: make([]byte, defaultBufSize),
UsedAt: now(),
} }
cn.rd = bufio.NewReader(cn) cn.rd = bufio.NewReader(cn)
return cn, cn.init(opt) return cn, cn.init(opt)
@ -76,7 +79,7 @@ func (cn *conn) writeCmds(cmds ...Cmder) error {
} }
func (cn *conn) Read(b []byte) (int, error) { func (cn *conn) Read(b []byte) (int, error) {
cn.UsedAt = time.Now() cn.UsedAt = now()
if cn.ReadTimeout != 0 { if cn.ReadTimeout != 0 {
cn.netcn.SetReadDeadline(cn.UsedAt.Add(cn.ReadTimeout)) cn.netcn.SetReadDeadline(cn.UsedAt.Add(cn.ReadTimeout))
} else { } else {
@ -86,7 +89,7 @@ func (cn *conn) Read(b []byte) (int, error) {
} }
func (cn *conn) Write(b []byte) (int, error) { func (cn *conn) Write(b []byte) (int, error) {
cn.UsedAt = time.Now() cn.UsedAt = now()
if cn.WriteTimeout != 0 { if cn.WriteTimeout != 0 {
cn.netcn.SetWriteDeadline(cn.UsedAt.Add(cn.WriteTimeout)) cn.netcn.SetWriteDeadline(cn.UsedAt.Add(cn.WriteTimeout))
} else { } else {

View File

@ -1,6 +1,9 @@
package redis package redis
import "net" import (
"net"
"time"
)
func (c *baseClient) Pool() pool { func (c *baseClient) Pool() pool {
return c.connPool return c.connPool
@ -15,3 +18,13 @@ var NewConnDialer = newConnDialer
func (cn *conn) SetNetConn(netcn net.Conn) { func (cn *conn) SetNetConn(netcn net.Conn) {
cn.netcn = netcn cn.netcn = netcn
} }
func SetTime(tm time.Time) {
now = func() time.Time {
return tm
}
}
func RestoreTime() {
now = time.Now
}

View File

@ -173,18 +173,23 @@ var _ = Describe("Client", func() {
It("should maintain conn.UsedAt", func() { It("should maintain conn.UsedAt", func() {
cn, _, err := client.Pool().Get() cn, _, err := client.Pool().Get()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(cn.UsedAt).To(BeZero()) Expect(cn.UsedAt).NotTo(BeZero())
createdAt := cn.UsedAt
future := time.Now().Add(time.Hour)
redis.SetTime(future)
defer redis.RestoreTime()
err = client.Pool().Put(cn) err = client.Pool().Put(cn)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(cn.UsedAt).To(BeZero()) Expect(cn.UsedAt.Equal(createdAt)).To(BeTrue())
err = client.Ping().Err() err = client.Ping().Err()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
cn = client.Pool().First() cn = client.Pool().First()
Expect(cn).NotTo(BeNil()) Expect(cn).NotTo(BeNil())
Expect(cn.UsedAt).To(BeTemporally("~", time.Now())) Expect(cn.UsedAt.Equal(future)).To(BeTrue())
}) })
}) })