diff --git a/redis.go b/redis.go index 1f2167aa..7f5ac5ff 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 c00e505f..00000000 --- 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 8555c5c0..00000000 --- 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 -}