diff options
Diffstat (limited to 'vendor/github.com/go-redis/redis/redis.go')
-rw-r--r-- | vendor/github.com/go-redis/redis/redis.go | 85 |
1 files changed, 58 insertions, 27 deletions
diff --git a/vendor/github.com/go-redis/redis/redis.go b/vendor/github.com/go-redis/redis/redis.go index cf402986d..7a606b70e 100644 --- a/vendor/github.com/go-redis/redis/redis.go +++ b/vendor/github.com/go-redis/redis/redis.go @@ -1,6 +1,7 @@ package redis import ( + "context" "fmt" "log" "os" @@ -11,8 +12,8 @@ import ( "github.com/go-redis/redis/internal/proto" ) -// Nil reply redis returned when key does not exist. -const Nil = internal.Nil +// Nil reply Redis returns when key does not exist. +const Nil = proto.Nil func init() { SetLogger(log.New(os.Stderr, "redis: ", log.LstdFlags|log.Lshortfile)) @@ -22,6 +23,17 @@ func SetLogger(logger *log.Logger) { internal.Logger = logger } +type baseClient struct { + opt *Options + connPool pool.Pooler + + process func(Cmder) error + processPipeline func([]Cmder) error + processTxPipeline func([]Cmder) error + + onClose func() error // hook called when client is closed +} + func (c *baseClient) init() { c.process = c.defaultProcess c.processPipeline = c.defaultProcessPipeline @@ -84,16 +96,7 @@ func (c *baseClient) initConn(cn *pool.Conn) error { return nil } - // Temp client to initialize connection. - conn := &Conn{ - baseClient: baseClient{ - opt: c.opt, - connPool: pool.NewSingleConnPool(cn), - }, - } - conn.baseClient.init() - conn.statefulCmdable.setProcessor(conn.Process) - + conn := newConn(c.opt, cn) _, err := conn.Pipelined(func(pipe Pipeliner) error { if c.opt.Password != "" { pipe.Auth(c.opt.Password) @@ -119,10 +122,7 @@ func (c *baseClient) initConn(cn *pool.Conn) error { return nil } -// WrapProcess replaces the process func. It takes a function createWrapper -// which is supplied by the user. createWrapper takes the old process func as -// an input and returns the new wrapper process func. createWrapper should -// use call the old process func within the new process func. +// WrapProcess wraps function that processes Redis commands. func (c *baseClient) WrapProcess(fn func(oldProcess func(cmd Cmder) error) func(cmd Cmder) error) { c.process = fn(c.process) } @@ -338,33 +338,52 @@ func (c *baseClient) txPipelineReadQueued(cn *pool.Conn, cmds []Cmder) error { type Client struct { baseClient cmdable + + ctx context.Context } -func newClient(opt *Options, pool pool.Pooler) *Client { +// NewClient returns a client to the Redis Server specified by Options. +func NewClient(opt *Options) *Client { + opt.init() + c := Client{ baseClient: baseClient{ opt: opt, - connPool: pool, + connPool: newConnPool(opt), }, } c.baseClient.init() - c.cmdable.setProcessor(c.Process) + c.init() + return &c } -// NewClient returns a client to the Redis Server specified by Options. -func NewClient(opt *Options) *Client { - opt.init() - return newClient(opt, newConnPool(opt)) +func (c *Client) init() { + c.cmdable.setProcessor(c.Process) } -func (c *Client) copy() *Client { - c2 := new(Client) - *c2 = *c - c2.cmdable.setProcessor(c2.Process) +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 { + cp := *c + cp.init() + return &cp +} + // Options returns read-only Options that were used to create the client. func (c *Client) Options() *Options { return c.opt @@ -442,6 +461,18 @@ type Conn struct { statefulCmdable } +func newConn(opt *Options, cn *pool.Conn) *Conn { + c := Conn{ + baseClient: baseClient{ + opt: opt, + connPool: pool.NewSingleConnPool(cn), + }, + } + c.baseClient.init() + c.statefulCmdable.setProcessor(c.Process) + return &c +} + func (c *Conn) Pipelined(fn func(Pipeliner) error) ([]Cmder, error) { return c.Pipeline().Pipelined(fn) } |