From 0e33c36bd1dccbe6371cca7ac11f14cdd9d8eda0 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Wed, 7 Mar 2018 13:42:20 +0200 Subject: [PATCH] Require Go 1.7+ and use context.Context --- redis.go | 30 ++++++++++++++++++++++++++++++ redis_context.go | 38 -------------------------------------- redis_no_context.go | 18 ------------------ 3 files changed, 30 insertions(+), 56 deletions(-) delete mode 100644 redis_context.go delete mode 100644 redis_no_context.go diff --git a/redis.go b/redis.go index 1f2167a..7f5ac5f 100644 --- a/redis.go +++ b/redis.go @@ -1,6 +1,7 @@ package redis import ( + "context" "fmt" "log" "os" @@ -22,6 +23,19 @@ func SetLogger(logger *log.Logger) { internal.Logger = logger } +type baseClient struct { + connPool pool.Pooler + opt *Options + + process func(Cmder) error + processPipeline func([]Cmder) error + processTxPipeline func([]Cmder) error + + onClose func() error // hook called when client is closed + + ctx context.Context +} + func (c *baseClient) init() { c.process = c.defaultProcess c.processPipeline = c.defaultProcessPipeline @@ -355,6 +369,22 @@ func NewClient(opt *Options) *Client { return newClient(opt, newConnPool(opt)) } +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 +} + func (c *Client) copy() *Client { c2 := new(Client) *c2 = *c diff --git a/redis_context.go b/redis_context.go deleted file mode 100644 index c00e505..0000000 --- a/redis_context.go +++ /dev/null @@ -1,38 +0,0 @@ -// +build go1.7 - -package redis - -import ( - "context" - - "github.com/go-redis/redis/internal/pool" -) - -type baseClient struct { - connPool pool.Pooler - opt *Options - - process func(Cmder) error - processPipeline func([]Cmder) error - processTxPipeline 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 deleted file mode 100644 index 8555c5c..0000000 --- a/redis_no_context.go +++ /dev/null @@ -1,18 +0,0 @@ -// +build !go1.7 - -package redis - -import ( - "github.com/go-redis/redis/internal/pool" -) - -type baseClient struct { - connPool pool.Pooler - opt *Options - - process func(Cmder) error - processPipeline func([]Cmder) error - processTxPipeline func([]Cmder) error - - onClose func() error // hook called when client is closed -}