Skip to content

Commit

Permalink
Clone and WithContext redis#471
Browse files Browse the repository at this point in the history
  • Loading branch information
smacker committed Jan 11, 2017
1 parent b9cc17b commit 2f247eb
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions redis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package redis // import "gopkg.in/redis.v5"

import (
"context"
"fmt"
"log"
"time"
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 2f247eb

Please sign in to comment.