mirror of https://github.com/go-redis/redis.git
Clone and WithContext #471
This commit is contained in:
parent
b9cc17bae0
commit
2f247ebe83
26
redis.go
26
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()
|
||||
|
|
Loading…
Reference in New Issue