Skip to content

Commit

Permalink
Context methods only for go1.7+
Browse files Browse the repository at this point in the history
smacker committed Jan 16, 2017
1 parent 2f247eb commit b9ab636
Showing 3 changed files with 51 additions and 28 deletions.
29 changes: 1 addition & 28 deletions redis.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package redis // import "gopkg.in/redis.v5"

import (
"context"
"fmt"
"log"
"time"
@@ -18,16 +17,6 @@ func SetLogger(logger *log.Logger) {
internal.Logger = logger
}

type baseClient struct {
connPool pool.Pooler
opt *Options

process func(Cmder) error
onClose func() error // hook called when client is closed

ctx context.Context
}

func (c *baseClient) String() string {
return fmt.Sprintf("Redis<%s db:%d>", c.getAddr(), c.opt.DB)
}
@@ -312,29 +301,13 @@ func NewClient(opt *Options) *Client {
return newClient(opt, newConnPool(opt))
}

func (c *Client) Clone() *Client {
func (c *Client) copy() *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()
35 changes: 35 additions & 0 deletions redis_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// +build go1.7

package redis

import (
"context"

"gopkg.in/redis.v5/internal/pool"
)

type baseClient struct {
connPool pool.Pooler
opt *Options

process 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
}
15 changes: 15 additions & 0 deletions redis_no_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// +build !go1.7

package redis

import (
"gopkg.in/redis.v5/internal/pool"
)

type baseClient struct {
connPool pool.Pooler
opt *Options

process func(Cmder) error
onClose func() error // hook called when client is closed
}

0 comments on commit b9ab636

Please sign in to comment.