Context methods only for go1.7+

This commit is contained in:
Sukharev Maxim 2017-01-13 11:11:07 +07:00
parent 2f247ebe83
commit b9ab636be4
3 changed files with 51 additions and 28 deletions

View File

@ -1,7 +1,6 @@
package redis // import "gopkg.in/redis.v5" package redis // import "gopkg.in/redis.v5"
import ( import (
"context"
"fmt" "fmt"
"log" "log"
"time" "time"
@ -18,16 +17,6 @@ func SetLogger(logger *log.Logger) {
internal.Logger = logger internal.Logger = logger
} }
type baseClient struct {
connPool pool.Pooler
opt *Options
process func(Cmder) error
onClose func() error // hook called when client is closed
ctx context.Context
}
func (c *baseClient) String() string { func (c *baseClient) String() string {
return fmt.Sprintf("Redis<%s db:%d>", c.getAddr(), c.opt.DB) return fmt.Sprintf("Redis<%s db:%d>", c.getAddr(), c.opt.DB)
} }
@ -312,29 +301,13 @@ func NewClient(opt *Options) *Client {
return newClient(opt, newConnPool(opt)) return newClient(opt, newConnPool(opt))
} }
func (c *Client) Clone() *Client { func (c *Client) copy() *Client {
c2 := new(Client) c2 := new(Client)
*c2 = *c *c2 = *c
c2.cmdable.process = c2.Process c2.cmdable.process = c2.Process
return c2 return c2
} }
func (c *Client) Context() context.Context {
if c.ctx != nil {
return c.ctx
}
return context.Background()
}
func (c *Client) WithContext(ctx context.Context) *Client {
if ctx == nil {
panic("nil context")
}
c2 := c.Clone()
c2.ctx = ctx
return c2
}
// PoolStats returns connection pool stats. // PoolStats returns connection pool stats.
func (c *Client) PoolStats() *PoolStats { func (c *Client) PoolStats() *PoolStats {
s := c.connPool.Stats() s := c.connPool.Stats()

35
redis_context.go Normal file
View File

@ -0,0 +1,35 @@
// +build go1.7
package redis
import (
"context"
"gopkg.in/redis.v5/internal/pool"
)
type baseClient struct {
connPool pool.Pooler
opt *Options
process func(Cmder) error
onClose func() error // hook called when client is closed
ctx context.Context
}
func (c *Client) Context() context.Context {
if c.ctx != nil {
return c.ctx
}
return context.Background()
}
func (c *Client) WithContext(ctx context.Context) *Client {
if ctx == nil {
panic("nil context")
}
c2 := c.copy()
c2.ctx = ctx
return c2
}

15
redis_no_context.go Normal file
View File

@ -0,0 +1,15 @@
// +build !go1.7
package redis
import (
"gopkg.in/redis.v5/internal/pool"
)
type baseClient struct {
connPool pool.Pooler
opt *Options
process func(Cmder) error
onClose func() error // hook called when client is closed
}