From 2f247ebe83446fe246462259a33dfda68d06c8bf Mon Sep 17 00:00:00 2001 From: Sukharev Maxim Date: Wed, 11 Jan 2017 10:32:10 +0700 Subject: [PATCH 1/2] Clone and WithContext #471 --- redis.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/redis.go b/redis.go index 894294d8..dc779f47 100644 --- a/redis.go +++ b/redis.go @@ -1,6 +1,7 @@ package redis // import "gopkg.in/redis.v5" import ( + "context" "fmt" "log" "time" @@ -23,6 +24,8 @@ type baseClient struct { process func(Cmder) error onClose func() error // hook called when client is closed + + ctx context.Context } func (c *baseClient) String() string { @@ -309,6 +312,29 @@ func NewClient(opt *Options) *Client { return newClient(opt, newConnPool(opt)) } +func (c *Client) Clone() *Client { + c2 := new(Client) + *c2 = *c + c2.cmdable.process = c2.Process + 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. func (c *Client) PoolStats() *PoolStats { s := c.connPool.Stats() From b9ab636be44bb604f10ba411fea670e043dc7f43 Mon Sep 17 00:00:00 2001 From: Sukharev Maxim Date: Fri, 13 Jan 2017 11:11:07 +0700 Subject: [PATCH 2/2] Context methods only for go1.7+ --- redis.go | 29 +---------------------------- redis_context.go | 35 +++++++++++++++++++++++++++++++++++ redis_no_context.go | 15 +++++++++++++++ 3 files changed, 51 insertions(+), 28 deletions(-) create mode 100644 redis_context.go create mode 100644 redis_no_context.go diff --git a/redis.go b/redis.go index dc779f47..32b83d5a 100644 --- a/redis.go +++ b/redis.go @@ -1,7 +1,6 @@ package redis // import "gopkg.in/redis.v5" import ( - "context" "fmt" "log" "time" @@ -18,16 +17,6 @@ func SetLogger(logger *log.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 { 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)) } -func (c *Client) Clone() *Client { +func (c *Client) copy() *Client { c2 := new(Client) *c2 = *c c2.cmdable.process = c2.Process 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. func (c *Client) PoolStats() *PoolStats { s := c.connPool.Stats() diff --git a/redis_context.go b/redis_context.go new file mode 100644 index 00000000..bfa62bc5 --- /dev/null +++ b/redis_context.go @@ -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 +} diff --git a/redis_no_context.go b/redis_no_context.go new file mode 100644 index 00000000..78a5a637 --- /dev/null +++ b/redis_no_context.go @@ -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 +}