2017-02-18 17:42:34 +03:00
|
|
|
package redis
|
2012-07-25 17:00:50 +04:00
|
|
|
|
|
|
|
import (
|
2018-03-07 14:42:20 +03:00
|
|
|
"context"
|
2015-05-15 15:21:28 +03:00
|
|
|
"fmt"
|
2014-07-31 17:01:54 +04:00
|
|
|
"log"
|
2017-09-11 09:10:17 +03:00
|
|
|
"os"
|
2016-12-03 18:30:13 +03:00
|
|
|
"time"
|
2016-03-12 11:52:13 +03:00
|
|
|
|
2017-02-18 17:42:34 +03:00
|
|
|
"github.com/go-redis/redis/internal"
|
|
|
|
"github.com/go-redis/redis/internal/pool"
|
|
|
|
"github.com/go-redis/redis/internal/proto"
|
2012-08-09 14:12:41 +04:00
|
|
|
)
|
2012-07-25 17:00:50 +04:00
|
|
|
|
2018-03-06 15:50:48 +03:00
|
|
|
// Nil reply Redis returns when key does not exist.
|
2018-02-22 15:14:30 +03:00
|
|
|
const Nil = proto.Nil
|
2016-07-02 15:52:10 +03:00
|
|
|
|
2017-09-11 09:10:17 +03:00
|
|
|
func init() {
|
|
|
|
SetLogger(log.New(os.Stderr, "redis: ", log.LstdFlags|log.Lshortfile))
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func SetLogger(logger *log.Logger) {
|
2016-04-09 14:52:01 +03:00
|
|
|
internal.Logger = logger
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
2016-02-03 20:30:39 +03:00
|
|
|
|
2018-03-07 14:42:20 +03:00
|
|
|
type baseClient struct {
|
|
|
|
opt *Options
|
2018-03-07 14:50:14 +03:00
|
|
|
connPool pool.Pooler
|
2018-03-07 14:42:20 +03:00
|
|
|
|
|
|
|
process func(Cmder) error
|
|
|
|
processPipeline func([]Cmder) error
|
|
|
|
processTxPipeline func([]Cmder) error
|
|
|
|
|
|
|
|
onClose func() error // hook called when client is closed
|
|
|
|
}
|
|
|
|
|
2018-01-20 13:26:33 +03:00
|
|
|
func (c *baseClient) init() {
|
|
|
|
c.process = c.defaultProcess
|
|
|
|
c.processPipeline = c.defaultProcessPipeline
|
|
|
|
c.processTxPipeline = c.defaultProcessTxPipeline
|
|
|
|
}
|
|
|
|
|
2015-05-15 15:21:28 +03:00
|
|
|
func (c *baseClient) String() string {
|
2016-10-05 23:20:05 +03:00
|
|
|
return fmt.Sprintf("Redis<%s db:%d>", c.getAddr(), c.opt.DB)
|
2015-05-15 15:21:28 +03:00
|
|
|
}
|
|
|
|
|
2017-07-09 10:07:20 +03:00
|
|
|
func (c *baseClient) newConn() (*pool.Conn, error) {
|
|
|
|
cn, err := c.connPool.NewConn()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cn.Inited {
|
|
|
|
if err := c.initConn(cn); err != nil {
|
|
|
|
_ = c.connPool.CloseConn(cn)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *baseClient) getConn() (*pool.Conn, bool, error) {
|
2016-09-29 15:07:04 +03:00
|
|
|
cn, isNew, err := c.connPool.Get()
|
2016-03-15 15:04:35 +03:00
|
|
|
if err != nil {
|
2016-09-29 15:07:04 +03:00
|
|
|
return nil, false, err
|
2016-03-15 15:04:35 +03:00
|
|
|
}
|
2017-04-17 15:43:58 +03:00
|
|
|
|
2016-03-15 15:04:35 +03:00
|
|
|
if !cn.Inited {
|
|
|
|
if err := c.initConn(cn); err != nil {
|
2017-04-17 15:43:58 +03:00
|
|
|
_ = c.connPool.Remove(cn)
|
2016-09-29 15:07:04 +03:00
|
|
|
return nil, false, err
|
2016-03-12 13:41:02 +03:00
|
|
|
}
|
|
|
|
}
|
2017-04-18 13:12:38 +03:00
|
|
|
|
2016-09-29 15:07:04 +03:00
|
|
|
return cn, isNew, nil
|
2012-07-26 19:16:17 +04:00
|
|
|
}
|
|
|
|
|
2017-07-09 10:07:20 +03:00
|
|
|
func (c *baseClient) releaseConn(cn *pool.Conn, err error) bool {
|
2017-04-24 12:43:15 +03:00
|
|
|
if internal.IsBadConn(err, false) {
|
2017-04-17 15:43:58 +03:00
|
|
|
_ = c.connPool.Remove(cn)
|
2016-03-01 13:31:06 +03:00
|
|
|
return false
|
2012-08-06 12:33:49 +04:00
|
|
|
}
|
2016-03-01 13:31:06 +03:00
|
|
|
|
2016-03-14 14:17:33 +03:00
|
|
|
_ = c.connPool.Put(cn)
|
2016-03-01 13:31:06 +03:00
|
|
|
return true
|
2012-08-06 12:33:49 +04:00
|
|
|
}
|
|
|
|
|
2016-03-12 13:41:02 +03:00
|
|
|
func (c *baseClient) initConn(cn *pool.Conn) error {
|
2016-03-15 15:04:35 +03:00
|
|
|
cn.Inited = true
|
|
|
|
|
2017-05-25 14:16:39 +03:00
|
|
|
if c.opt.Password == "" &&
|
|
|
|
c.opt.DB == 0 &&
|
2017-07-25 04:35:41 +03:00
|
|
|
!c.opt.readOnly &&
|
2017-05-25 14:16:39 +03:00
|
|
|
c.opt.OnConnect == nil {
|
2016-03-12 13:41:02 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-25 14:16:39 +03:00
|
|
|
// Temp client to initialize connection.
|
|
|
|
conn := &Conn{
|
|
|
|
baseClient: baseClient{
|
|
|
|
opt: c.opt,
|
|
|
|
connPool: pool.NewSingleConnPool(cn),
|
|
|
|
},
|
|
|
|
}
|
2018-01-20 13:26:33 +03:00
|
|
|
conn.baseClient.init()
|
|
|
|
conn.statefulCmdable.setProcessor(conn.Process)
|
2017-05-25 14:16:39 +03:00
|
|
|
|
|
|
|
_, err := conn.Pipelined(func(pipe Pipeliner) error {
|
2016-06-05 12:45:39 +03:00
|
|
|
if c.opt.Password != "" {
|
|
|
|
pipe.Auth(c.opt.Password)
|
2016-03-12 13:41:02 +03:00
|
|
|
}
|
|
|
|
|
2016-06-05 12:45:39 +03:00
|
|
|
if c.opt.DB > 0 {
|
|
|
|
pipe.Select(c.opt.DB)
|
2016-03-12 13:41:02 +03:00
|
|
|
}
|
|
|
|
|
2017-07-25 04:35:41 +03:00
|
|
|
if c.opt.readOnly {
|
2016-06-05 12:45:39 +03:00
|
|
|
pipe.ReadOnly()
|
2016-05-06 21:12:31 +03:00
|
|
|
}
|
|
|
|
|
2016-06-05 12:45:39 +03:00
|
|
|
return nil
|
|
|
|
})
|
2017-05-25 14:16:39 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.opt.OnConnect != nil {
|
|
|
|
return c.opt.OnConnect(conn)
|
|
|
|
}
|
|
|
|
return nil
|
2016-03-12 13:41:02 +03:00
|
|
|
}
|
|
|
|
|
2018-03-06 15:50:48 +03:00
|
|
|
// WrapProcess wraps function that processes Redis commands.
|
2016-11-30 13:39:14 +03:00
|
|
|
func (c *baseClient) WrapProcess(fn func(oldProcess func(cmd Cmder) error) func(cmd Cmder) error) {
|
2018-01-20 13:26:33 +03:00
|
|
|
c.process = fn(c.process)
|
2016-11-30 13:39:14 +03:00
|
|
|
}
|
|
|
|
|
2017-07-09 13:10:07 +03:00
|
|
|
func (c *baseClient) Process(cmd Cmder) error {
|
2018-01-20 13:26:33 +03:00
|
|
|
return c.process(cmd)
|
2017-07-09 13:10:07 +03:00
|
|
|
}
|
|
|
|
|
2016-11-30 13:39:14 +03:00
|
|
|
func (c *baseClient) defaultProcess(cmd Cmder) error {
|
2017-07-09 13:10:07 +03:00
|
|
|
for attempt := 0; attempt <= c.opt.MaxRetries; attempt++ {
|
|
|
|
if attempt > 0 {
|
|
|
|
time.Sleep(c.retryBackoff(attempt))
|
2017-05-25 08:08:44 +03:00
|
|
|
}
|
|
|
|
|
2017-07-09 10:07:20 +03:00
|
|
|
cn, _, err := c.getConn()
|
2015-05-10 15:33:04 +03:00
|
|
|
if err != nil {
|
|
|
|
cmd.setErr(err)
|
2017-09-27 10:58:06 +03:00
|
|
|
if internal.IsRetryableError(err, true) {
|
2017-05-25 08:08:44 +03:00
|
|
|
continue
|
|
|
|
}
|
2016-06-17 15:09:38 +03:00
|
|
|
return err
|
2015-05-10 15:33:04 +03:00
|
|
|
}
|
2012-07-26 22:43:21 +04:00
|
|
|
|
2016-12-03 18:30:13 +03:00
|
|
|
cn.SetWriteTimeout(c.opt.WriteTimeout)
|
2016-03-12 11:52:13 +03:00
|
|
|
if err := writeCmd(cn, cmd); err != nil {
|
2017-07-09 10:07:20 +03:00
|
|
|
c.releaseConn(cn, err)
|
2015-05-10 15:33:04 +03:00
|
|
|
cmd.setErr(err)
|
2017-09-27 10:58:06 +03:00
|
|
|
if internal.IsRetryableError(err, true) {
|
2015-05-10 15:33:04 +03:00
|
|
|
continue
|
|
|
|
}
|
2016-06-17 15:09:38 +03:00
|
|
|
return err
|
2015-05-10 15:33:04 +03:00
|
|
|
}
|
|
|
|
|
2016-12-03 18:30:13 +03:00
|
|
|
cn.SetReadTimeout(c.cmdTimeout(cmd))
|
2015-10-07 17:09:20 +03:00
|
|
|
err = cmd.readReply(cn)
|
2017-07-09 10:07:20 +03:00
|
|
|
c.releaseConn(cn, err)
|
2017-09-27 10:58:06 +03:00
|
|
|
if err != nil && internal.IsRetryableError(err, cmd.readTimeout() == nil) {
|
2015-05-10 15:33:04 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-06-17 15:09:38 +03:00
|
|
|
return err
|
2012-07-26 19:16:17 +04:00
|
|
|
}
|
2016-06-17 15:09:38 +03:00
|
|
|
|
|
|
|
return cmd.Err()
|
2014-05-11 11:42:40 +04:00
|
|
|
}
|
|
|
|
|
2017-07-09 13:10:07 +03:00
|
|
|
func (c *baseClient) retryBackoff(attempt int) time.Duration {
|
|
|
|
return internal.RetryBackoff(attempt, c.opt.MinRetryBackoff, c.opt.MaxRetryBackoff)
|
|
|
|
}
|
|
|
|
|
2016-12-03 18:30:13 +03:00
|
|
|
func (c *baseClient) cmdTimeout(cmd Cmder) time.Duration {
|
|
|
|
if timeout := cmd.readTimeout(); timeout != nil {
|
|
|
|
return *timeout
|
|
|
|
}
|
2018-01-24 21:38:47 +03:00
|
|
|
|
|
|
|
return c.opt.ReadTimeout
|
2016-12-03 18:30:13 +03:00
|
|
|
}
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
// Close closes the client, releasing any open resources.
|
2015-09-12 09:36:03 +03:00
|
|
|
//
|
|
|
|
// It is rare to Close a Client, as the Client is meant to be
|
|
|
|
// long-lived and shared between many goroutines.
|
2014-05-11 11:42:40 +04:00
|
|
|
func (c *baseClient) Close() error {
|
2016-12-03 18:30:13 +03:00
|
|
|
var firstErr error
|
2016-03-09 16:14:01 +03:00
|
|
|
if c.onClose != nil {
|
2016-12-03 18:30:13 +03:00
|
|
|
if err := c.onClose(); err != nil && firstErr == nil {
|
|
|
|
firstErr = err
|
2016-03-09 16:14:01 +03:00
|
|
|
}
|
|
|
|
}
|
2016-12-03 18:30:13 +03:00
|
|
|
if err := c.connPool.Close(); err != nil && firstErr == nil {
|
|
|
|
firstErr = err
|
2016-03-09 16:14:01 +03:00
|
|
|
}
|
2016-12-03 18:30:13 +03:00
|
|
|
return firstErr
|
2012-07-26 19:16:17 +04:00
|
|
|
}
|
|
|
|
|
2016-10-05 23:20:05 +03:00
|
|
|
func (c *baseClient) getAddr() string {
|
|
|
|
return c.opt.Addr
|
|
|
|
}
|
|
|
|
|
2018-01-20 13:26:33 +03:00
|
|
|
func (c *baseClient) WrapProcessPipeline(
|
|
|
|
fn func(oldProcess func([]Cmder) error) func([]Cmder) error,
|
|
|
|
) {
|
|
|
|
c.processPipeline = fn(c.processPipeline)
|
|
|
|
c.processTxPipeline = fn(c.processTxPipeline)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *baseClient) defaultProcessPipeline(cmds []Cmder) error {
|
|
|
|
return c.generalProcessPipeline(cmds, c.pipelineProcessCmds)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *baseClient) defaultProcessTxPipeline(cmds []Cmder) error {
|
|
|
|
return c.generalProcessPipeline(cmds, c.txPipelineProcessCmds)
|
|
|
|
}
|
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
type pipelineProcessor func(*pool.Conn, []Cmder) (bool, error)
|
|
|
|
|
2018-01-20 13:26:33 +03:00
|
|
|
func (c *baseClient) generalProcessPipeline(cmds []Cmder, p pipelineProcessor) error {
|
|
|
|
for attempt := 0; attempt <= c.opt.MaxRetries; attempt++ {
|
|
|
|
if attempt > 0 {
|
|
|
|
time.Sleep(c.retryBackoff(attempt))
|
|
|
|
}
|
2017-08-31 15:22:47 +03:00
|
|
|
|
2018-01-20 13:26:33 +03:00
|
|
|
cn, _, err := c.getConn()
|
|
|
|
if err != nil {
|
|
|
|
setCmdsErr(cmds, err)
|
|
|
|
return err
|
|
|
|
}
|
2016-12-13 18:28:39 +03:00
|
|
|
|
2018-01-20 13:26:33 +03:00
|
|
|
canRetry, err := p(cn, cmds)
|
2017-08-31 15:22:47 +03:00
|
|
|
|
2018-01-20 13:26:33 +03:00
|
|
|
if err == nil || internal.IsRedisError(err) {
|
|
|
|
_ = c.connPool.Put(cn)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
_ = c.connPool.Remove(cn)
|
2017-08-31 15:22:47 +03:00
|
|
|
|
2018-01-20 13:26:33 +03:00
|
|
|
if !canRetry || !internal.IsRetryableError(err, true) {
|
|
|
|
break
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
|
|
|
}
|
2018-01-20 13:26:33 +03:00
|
|
|
return firstCmdsErr(cmds)
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
|
|
|
|
2017-05-25 14:16:39 +03:00
|
|
|
func (c *baseClient) pipelineProcessCmds(cn *pool.Conn, cmds []Cmder) (bool, error) {
|
2016-12-13 18:28:39 +03:00
|
|
|
cn.SetWriteTimeout(c.opt.WriteTimeout)
|
|
|
|
if err := writeCmd(cn, cmds...); err != nil {
|
|
|
|
setCmdsErr(cmds, err)
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set read timeout for all commands.
|
|
|
|
cn.SetReadTimeout(c.opt.ReadTimeout)
|
2017-08-31 15:22:47 +03:00
|
|
|
return true, pipelineReadCmds(cn, cmds)
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
|
|
|
|
2017-08-31 15:22:47 +03:00
|
|
|
func pipelineReadCmds(cn *pool.Conn, cmds []Cmder) error {
|
|
|
|
for _, cmd := range cmds {
|
2016-12-13 18:28:39 +03:00
|
|
|
err := cmd.readReply(cn)
|
2017-08-31 15:22:47 +03:00
|
|
|
if err != nil && !internal.IsRedisError(err) {
|
|
|
|
return err
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
|
|
|
}
|
2017-08-31 15:22:47 +03:00
|
|
|
return nil
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *baseClient) txPipelineProcessCmds(cn *pool.Conn, cmds []Cmder) (bool, error) {
|
|
|
|
cn.SetWriteTimeout(c.opt.WriteTimeout)
|
|
|
|
if err := txPipelineWriteMulti(cn, cmds); err != nil {
|
|
|
|
setCmdsErr(cmds, err)
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set read timeout for all commands.
|
|
|
|
cn.SetReadTimeout(c.opt.ReadTimeout)
|
|
|
|
|
|
|
|
if err := c.txPipelineReadQueued(cn, cmds); err != nil {
|
2017-08-31 15:22:47 +03:00
|
|
|
setCmdsErr(cmds, err)
|
2016-12-13 18:28:39 +03:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2017-08-31 15:22:47 +03:00
|
|
|
return false, pipelineReadCmds(cn, cmds)
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func txPipelineWriteMulti(cn *pool.Conn, cmds []Cmder) error {
|
|
|
|
multiExec := make([]Cmder, 0, len(cmds)+2)
|
|
|
|
multiExec = append(multiExec, NewStatusCmd("MULTI"))
|
|
|
|
multiExec = append(multiExec, cmds...)
|
|
|
|
multiExec = append(multiExec, NewSliceCmd("EXEC"))
|
|
|
|
return writeCmd(cn, multiExec...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *baseClient) txPipelineReadQueued(cn *pool.Conn, cmds []Cmder) error {
|
|
|
|
// Parse queued replies.
|
|
|
|
var statusCmd StatusCmd
|
2017-08-31 15:22:47 +03:00
|
|
|
if err := statusCmd.readReply(cn); err != nil {
|
|
|
|
return err
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
|
|
|
|
2017-08-31 15:22:47 +03:00
|
|
|
for _ = range cmds {
|
2016-12-13 18:28:39 +03:00
|
|
|
err := statusCmd.readReply(cn)
|
2017-08-31 15:22:47 +03:00
|
|
|
if err != nil && !internal.IsRedisError(err) {
|
|
|
|
return err
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse number of replies.
|
|
|
|
line, err := cn.Rd.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
if err == Nil {
|
|
|
|
err = TxFailedErr
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch line[0] {
|
|
|
|
case proto.ErrorReply:
|
|
|
|
return proto.ParseErrorReply(line)
|
|
|
|
case proto.ArrayReply:
|
|
|
|
// ok
|
|
|
|
default:
|
|
|
|
err := fmt.Errorf("redis: expected '*', but got line %q", line)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2015-09-12 09:36:03 +03:00
|
|
|
// Client is a Redis client representing a pool of zero or more
|
|
|
|
// underlying connections. It's safe for concurrent use by multiple
|
|
|
|
// goroutines.
|
2012-08-11 18:42:10 +04:00
|
|
|
type Client struct {
|
2016-03-09 12:49:05 +03:00
|
|
|
baseClient
|
2016-06-05 12:45:39 +03:00
|
|
|
cmdable
|
2018-03-07 14:50:14 +03:00
|
|
|
|
|
|
|
ctx context.Context
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func newClient(opt *Options, pool pool.Pooler) *Client {
|
2018-01-20 13:26:33 +03:00
|
|
|
c := Client{
|
2016-12-30 13:58:04 +03:00
|
|
|
baseClient: baseClient{
|
|
|
|
opt: opt,
|
|
|
|
connPool: pool,
|
|
|
|
},
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
2018-01-20 13:26:33 +03:00
|
|
|
c.baseClient.init()
|
|
|
|
c.cmdable.setProcessor(c.Process)
|
|
|
|
return &c
|
2012-08-05 16:09:43 +04:00
|
|
|
}
|
|
|
|
|
2015-09-12 09:36:03 +03:00
|
|
|
// NewClient returns a client to the Redis Server specified by Options.
|
2015-05-23 17:55:08 +03:00
|
|
|
func NewClient(opt *Options) *Client {
|
2016-06-05 14:10:30 +03:00
|
|
|
opt.init()
|
2016-03-12 11:52:13 +03:00
|
|
|
return newClient(opt, newConnPool(opt))
|
2012-08-11 18:42:10 +04:00
|
|
|
}
|
2016-01-19 19:36:40 +03:00
|
|
|
|
2018-03-07 14:42:20 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-01-13 07:11:07 +03:00
|
|
|
func (c *Client) copy() *Client {
|
2018-03-07 14:50:14 +03:00
|
|
|
cp := *c
|
|
|
|
return &cp
|
2017-01-11 06:32:10 +03:00
|
|
|
}
|
|
|
|
|
2017-05-25 14:16:39 +03:00
|
|
|
// Options returns read-only Options that were used to create the client.
|
|
|
|
func (c *Client) Options() *Options {
|
|
|
|
return c.opt
|
|
|
|
}
|
|
|
|
|
2017-09-11 10:12:00 +03:00
|
|
|
type PoolStats pool.Stats
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
// PoolStats returns connection pool stats.
|
2016-01-19 19:36:40 +03:00
|
|
|
func (c *Client) PoolStats() *PoolStats {
|
2017-09-11 10:12:00 +03:00
|
|
|
stats := c.connPool.Stats()
|
|
|
|
return (*PoolStats)(stats)
|
2016-01-19 19:36:40 +03:00
|
|
|
}
|
2016-04-09 10:47:15 +03:00
|
|
|
|
2017-05-02 18:00:53 +03:00
|
|
|
func (c *Client) Pipelined(fn func(Pipeliner) error) ([]Cmder, error) {
|
2017-09-25 11:48:44 +03:00
|
|
|
return c.Pipeline().Pipelined(fn)
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
|
|
|
|
2017-05-02 18:00:53 +03:00
|
|
|
func (c *Client) Pipeline() Pipeliner {
|
2016-06-05 12:45:39 +03:00
|
|
|
pipe := Pipeline{
|
2018-01-20 13:26:33 +03:00
|
|
|
exec: c.processPipeline,
|
2016-04-09 10:47:15 +03:00
|
|
|
}
|
2018-01-20 13:26:33 +03:00
|
|
|
pipe.statefulCmdable.setProcessor(pipe.Process)
|
2016-06-05 12:45:39 +03:00
|
|
|
return &pipe
|
2016-04-09 10:47:15 +03:00
|
|
|
}
|
|
|
|
|
2017-05-02 18:00:53 +03:00
|
|
|
func (c *Client) TxPipelined(fn func(Pipeliner) error) ([]Cmder, error) {
|
2017-09-25 11:48:44 +03:00
|
|
|
return c.TxPipeline().Pipelined(fn)
|
2016-04-09 10:47:15 +03:00
|
|
|
}
|
2016-07-21 16:04:40 +03:00
|
|
|
|
2016-12-16 15:19:53 +03:00
|
|
|
// TxPipeline acts like Pipeline, but wraps queued commands with MULTI/EXEC.
|
2017-05-03 16:48:12 +03:00
|
|
|
func (c *Client) TxPipeline() Pipeliner {
|
2016-12-13 18:28:39 +03:00
|
|
|
pipe := Pipeline{
|
2018-01-20 13:26:33 +03:00
|
|
|
exec: c.processTxPipeline,
|
2016-12-03 18:30:13 +03:00
|
|
|
}
|
2018-01-20 13:26:33 +03:00
|
|
|
pipe.statefulCmdable.setProcessor(pipe.Process)
|
2016-12-13 18:28:39 +03:00
|
|
|
return &pipe
|
2016-12-03 18:30:13 +03:00
|
|
|
}
|
|
|
|
|
2016-07-21 16:04:40 +03:00
|
|
|
func (c *Client) pubSub() *PubSub {
|
|
|
|
return &PubSub{
|
2017-07-09 10:07:20 +03:00
|
|
|
opt: c.opt,
|
|
|
|
|
|
|
|
newConn: func(channels []string) (*pool.Conn, error) {
|
|
|
|
return c.newConn()
|
2016-07-21 16:04:40 +03:00
|
|
|
},
|
2017-07-09 10:07:20 +03:00
|
|
|
closeConn: c.connPool.CloseConn,
|
2016-07-21 16:04:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subscribe subscribes the client to the specified channels.
|
2017-06-17 12:43:19 +03:00
|
|
|
// Channels can be omitted to create empty subscription.
|
2017-04-11 16:53:55 +03:00
|
|
|
func (c *Client) Subscribe(channels ...string) *PubSub {
|
2016-07-21 16:04:40 +03:00
|
|
|
pubsub := c.pubSub()
|
2016-12-22 14:26:00 +03:00
|
|
|
if len(channels) > 0 {
|
2017-04-11 16:53:55 +03:00
|
|
|
_ = pubsub.Subscribe(channels...)
|
2016-12-22 14:26:00 +03:00
|
|
|
}
|
2017-04-11 16:53:55 +03:00
|
|
|
return pubsub
|
2016-07-21 16:04:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// PSubscribe subscribes the client to the given patterns.
|
2017-06-17 12:43:19 +03:00
|
|
|
// Patterns can be omitted to create empty subscription.
|
2017-04-11 16:53:55 +03:00
|
|
|
func (c *Client) PSubscribe(channels ...string) *PubSub {
|
2016-07-21 16:04:40 +03:00
|
|
|
pubsub := c.pubSub()
|
2016-12-22 14:26:00 +03:00
|
|
|
if len(channels) > 0 {
|
2017-04-11 16:53:55 +03:00
|
|
|
_ = pubsub.PSubscribe(channels...)
|
2016-12-22 14:26:00 +03:00
|
|
|
}
|
2017-04-11 16:53:55 +03:00
|
|
|
return pubsub
|
2016-07-21 16:04:40 +03:00
|
|
|
}
|
2017-05-25 14:16:39 +03:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Conn is like Client, but its pool contains single connection.
|
|
|
|
type Conn struct {
|
|
|
|
baseClient
|
|
|
|
statefulCmdable
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) Pipelined(fn func(Pipeliner) error) ([]Cmder, error) {
|
2017-09-25 11:48:44 +03:00
|
|
|
return c.Pipeline().Pipelined(fn)
|
2017-05-25 14:16:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) Pipeline() Pipeliner {
|
|
|
|
pipe := Pipeline{
|
2018-01-20 13:26:33 +03:00
|
|
|
exec: c.processPipeline,
|
2017-05-25 14:16:39 +03:00
|
|
|
}
|
2018-01-20 13:26:33 +03:00
|
|
|
pipe.statefulCmdable.setProcessor(pipe.Process)
|
2017-05-25 14:16:39 +03:00
|
|
|
return &pipe
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) TxPipelined(fn func(Pipeliner) error) ([]Cmder, error) {
|
2017-09-25 11:48:44 +03:00
|
|
|
return c.TxPipeline().Pipelined(fn)
|
2017-05-25 14:16:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TxPipeline acts like Pipeline, but wraps queued commands with MULTI/EXEC.
|
|
|
|
func (c *Conn) TxPipeline() Pipeliner {
|
|
|
|
pipe := Pipeline{
|
2018-01-20 13:26:33 +03:00
|
|
|
exec: c.processTxPipeline,
|
2017-05-25 14:16:39 +03:00
|
|
|
}
|
2018-01-20 13:26:33 +03:00
|
|
|
pipe.statefulCmdable.setProcessor(pipe.Process)
|
2017-05-25 14:16:39 +03:00
|
|
|
return &pipe
|
|
|
|
}
|