Require Go 1.7+ and use context.Context

This commit is contained in:
Vladimir Mihailenco 2018-03-07 13:42:20 +02:00
parent a64d3e1ef1
commit 0e33c36bd1
3 changed files with 30 additions and 56 deletions

View File

@ -1,6 +1,7 @@
package redis package redis
import ( import (
"context"
"fmt" "fmt"
"log" "log"
"os" "os"
@ -22,6 +23,19 @@ func SetLogger(logger *log.Logger) {
internal.Logger = 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() { func (c *baseClient) init() {
c.process = c.defaultProcess c.process = c.defaultProcess
c.processPipeline = c.defaultProcessPipeline c.processPipeline = c.defaultProcessPipeline
@ -355,6 +369,22 @@ func NewClient(opt *Options) *Client {
return newClient(opt, newConnPool(opt)) 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 { func (c *Client) copy() *Client {
c2 := new(Client) c2 := new(Client)
*c2 = *c *c2 = *c

View File

@ -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
}

View File

@ -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
}