redis.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. package redis
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "os"
  7. "time"
  8. "github.com/go-redis/redis/internal"
  9. "github.com/go-redis/redis/internal/pool"
  10. "github.com/go-redis/redis/internal/proto"
  11. )
  12. // Nil reply Redis returns when key does not exist.
  13. const Nil = proto.Nil
  14. func init() {
  15. SetLogger(log.New(os.Stderr, "redis: ", log.LstdFlags|log.Lshortfile))
  16. }
  17. func SetLogger(logger *log.Logger) {
  18. internal.Logger = logger
  19. }
  20. type baseClient struct {
  21. opt *Options
  22. connPool pool.Pooler
  23. limiter Limiter
  24. process func(Cmder) error
  25. processPipeline func([]Cmder) error
  26. processTxPipeline func([]Cmder) error
  27. onClose func() error // hook called when client is closed
  28. }
  29. func (c *baseClient) init() {
  30. c.process = c.defaultProcess
  31. c.processPipeline = c.defaultProcessPipeline
  32. c.processTxPipeline = c.defaultProcessTxPipeline
  33. }
  34. func (c *baseClient) String() string {
  35. return fmt.Sprintf("Redis<%s db:%d>", c.getAddr(), c.opt.DB)
  36. }
  37. func (c *baseClient) newConn() (*pool.Conn, error) {
  38. cn, err := c.connPool.NewConn()
  39. if err != nil {
  40. return nil, err
  41. }
  42. err = c.initConn(cn)
  43. if err != nil {
  44. _ = c.connPool.CloseConn(cn)
  45. return nil, err
  46. }
  47. return cn, nil
  48. }
  49. func (c *baseClient) getConn() (*pool.Conn, error) {
  50. if c.limiter != nil {
  51. err := c.limiter.Allow()
  52. if err != nil {
  53. return nil, err
  54. }
  55. }
  56. cn, err := c._getConn()
  57. if err != nil {
  58. if c.limiter != nil {
  59. c.limiter.ReportResult(err)
  60. }
  61. return nil, err
  62. }
  63. return cn, nil
  64. }
  65. func (c *baseClient) _getConn() (*pool.Conn, error) {
  66. cn, err := c.connPool.Get()
  67. if err != nil {
  68. return nil, err
  69. }
  70. err = c.initConn(cn)
  71. if err != nil {
  72. c.connPool.Remove(cn, err)
  73. if err := internal.Unwrap(err); err != nil {
  74. return nil, err
  75. }
  76. return nil, err
  77. }
  78. return cn, nil
  79. }
  80. func (c *baseClient) releaseConn(cn *pool.Conn, err error) {
  81. if c.limiter != nil {
  82. c.limiter.ReportResult(err)
  83. }
  84. if internal.IsBadConn(err, false) {
  85. c.connPool.Remove(cn, err)
  86. } else {
  87. c.connPool.Put(cn)
  88. }
  89. }
  90. func (c *baseClient) releaseConnStrict(cn *pool.Conn, err error) {
  91. if c.limiter != nil {
  92. c.limiter.ReportResult(err)
  93. }
  94. if err == nil || internal.IsRedisError(err) {
  95. c.connPool.Put(cn)
  96. } else {
  97. c.connPool.Remove(cn, err)
  98. }
  99. }
  100. func (c *baseClient) initConn(cn *pool.Conn) error {
  101. if cn.Inited {
  102. return nil
  103. }
  104. cn.Inited = true
  105. if c.opt.Password == "" &&
  106. c.opt.DB == 0 &&
  107. !c.opt.readOnly &&
  108. c.opt.OnConnect == nil {
  109. return nil
  110. }
  111. conn := newConn(c.opt, cn)
  112. _, err := conn.Pipelined(func(pipe Pipeliner) error {
  113. if c.opt.Password != "" {
  114. pipe.Auth(c.opt.Password)
  115. }
  116. if c.opt.DB > 0 {
  117. pipe.Select(c.opt.DB)
  118. }
  119. if c.opt.readOnly {
  120. pipe.ReadOnly()
  121. }
  122. return nil
  123. })
  124. if err != nil {
  125. return err
  126. }
  127. if c.opt.OnConnect != nil {
  128. return c.opt.OnConnect(conn)
  129. }
  130. return nil
  131. }
  132. // Do creates a Cmd from the args and processes the cmd.
  133. func (c *baseClient) Do(args ...interface{}) *Cmd {
  134. cmd := NewCmd(args...)
  135. _ = c.Process(cmd)
  136. return cmd
  137. }
  138. // WrapProcess wraps function that processes Redis commands.
  139. func (c *baseClient) WrapProcess(
  140. fn func(oldProcess func(cmd Cmder) error) func(cmd Cmder) error,
  141. ) {
  142. c.process = fn(c.process)
  143. }
  144. func (c *baseClient) Process(cmd Cmder) error {
  145. return c.process(cmd)
  146. }
  147. func (c *baseClient) defaultProcess(cmd Cmder) error {
  148. for attempt := 0; attempt <= c.opt.MaxRetries; attempt++ {
  149. if attempt > 0 {
  150. time.Sleep(c.retryBackoff(attempt))
  151. }
  152. cn, err := c.getConn()
  153. if err != nil {
  154. cmd.setErr(err)
  155. if internal.IsRetryableError(err, true) {
  156. continue
  157. }
  158. return err
  159. }
  160. err = cn.WithWriter(c.opt.WriteTimeout, func(wr *proto.Writer) error {
  161. return writeCmd(wr, cmd)
  162. })
  163. if err != nil {
  164. c.releaseConn(cn, err)
  165. cmd.setErr(err)
  166. if internal.IsRetryableError(err, true) {
  167. continue
  168. }
  169. return err
  170. }
  171. err = cn.WithReader(c.cmdTimeout(cmd), cmd.readReply)
  172. c.releaseConn(cn, err)
  173. if err != nil && internal.IsRetryableError(err, cmd.readTimeout() == nil) {
  174. continue
  175. }
  176. return err
  177. }
  178. return cmd.Err()
  179. }
  180. func (c *baseClient) retryBackoff(attempt int) time.Duration {
  181. return internal.RetryBackoff(attempt, c.opt.MinRetryBackoff, c.opt.MaxRetryBackoff)
  182. }
  183. func (c *baseClient) cmdTimeout(cmd Cmder) time.Duration {
  184. if timeout := cmd.readTimeout(); timeout != nil {
  185. t := *timeout
  186. if t == 0 {
  187. return 0
  188. }
  189. return t + 10*time.Second
  190. }
  191. return c.opt.ReadTimeout
  192. }
  193. // Close closes the client, releasing any open resources.
  194. //
  195. // It is rare to Close a Client, as the Client is meant to be
  196. // long-lived and shared between many goroutines.
  197. func (c *baseClient) Close() error {
  198. var firstErr error
  199. if c.onClose != nil {
  200. if err := c.onClose(); err != nil {
  201. firstErr = err
  202. }
  203. }
  204. if err := c.connPool.Close(); err != nil && firstErr == nil {
  205. firstErr = err
  206. }
  207. return firstErr
  208. }
  209. func (c *baseClient) getAddr() string {
  210. return c.opt.Addr
  211. }
  212. func (c *baseClient) WrapProcessPipeline(
  213. fn func(oldProcess func([]Cmder) error) func([]Cmder) error,
  214. ) {
  215. c.processPipeline = fn(c.processPipeline)
  216. c.processTxPipeline = fn(c.processTxPipeline)
  217. }
  218. func (c *baseClient) defaultProcessPipeline(cmds []Cmder) error {
  219. return c.generalProcessPipeline(cmds, c.pipelineProcessCmds)
  220. }
  221. func (c *baseClient) defaultProcessTxPipeline(cmds []Cmder) error {
  222. return c.generalProcessPipeline(cmds, c.txPipelineProcessCmds)
  223. }
  224. type pipelineProcessor func(*pool.Conn, []Cmder) (bool, error)
  225. func (c *baseClient) generalProcessPipeline(cmds []Cmder, p pipelineProcessor) error {
  226. for attempt := 0; attempt <= c.opt.MaxRetries; attempt++ {
  227. if attempt > 0 {
  228. time.Sleep(c.retryBackoff(attempt))
  229. }
  230. cn, err := c.getConn()
  231. if err != nil {
  232. setCmdsErr(cmds, err)
  233. return err
  234. }
  235. canRetry, err := p(cn, cmds)
  236. c.releaseConnStrict(cn, err)
  237. if !canRetry || !internal.IsRetryableError(err, true) {
  238. break
  239. }
  240. }
  241. return cmdsFirstErr(cmds)
  242. }
  243. func (c *baseClient) pipelineProcessCmds(cn *pool.Conn, cmds []Cmder) (bool, error) {
  244. err := cn.WithWriter(c.opt.WriteTimeout, func(wr *proto.Writer) error {
  245. return writeCmd(wr, cmds...)
  246. })
  247. if err != nil {
  248. setCmdsErr(cmds, err)
  249. return true, err
  250. }
  251. err = cn.WithReader(c.opt.ReadTimeout, func(rd *proto.Reader) error {
  252. return pipelineReadCmds(rd, cmds)
  253. })
  254. return true, err
  255. }
  256. func pipelineReadCmds(rd *proto.Reader, cmds []Cmder) error {
  257. for _, cmd := range cmds {
  258. err := cmd.readReply(rd)
  259. if err != nil && !internal.IsRedisError(err) {
  260. return err
  261. }
  262. }
  263. return nil
  264. }
  265. func (c *baseClient) txPipelineProcessCmds(cn *pool.Conn, cmds []Cmder) (bool, error) {
  266. err := cn.WithWriter(c.opt.WriteTimeout, func(wr *proto.Writer) error {
  267. return txPipelineWriteMulti(wr, cmds)
  268. })
  269. if err != nil {
  270. setCmdsErr(cmds, err)
  271. return true, err
  272. }
  273. err = cn.WithReader(c.opt.ReadTimeout, func(rd *proto.Reader) error {
  274. err := txPipelineReadQueued(rd, cmds)
  275. if err != nil {
  276. setCmdsErr(cmds, err)
  277. return err
  278. }
  279. return pipelineReadCmds(rd, cmds)
  280. })
  281. return false, err
  282. }
  283. func txPipelineWriteMulti(wr *proto.Writer, cmds []Cmder) error {
  284. multiExec := make([]Cmder, 0, len(cmds)+2)
  285. multiExec = append(multiExec, NewStatusCmd("MULTI"))
  286. multiExec = append(multiExec, cmds...)
  287. multiExec = append(multiExec, NewSliceCmd("EXEC"))
  288. return writeCmd(wr, multiExec...)
  289. }
  290. func txPipelineReadQueued(rd *proto.Reader, cmds []Cmder) error {
  291. // Parse queued replies.
  292. var statusCmd StatusCmd
  293. err := statusCmd.readReply(rd)
  294. if err != nil {
  295. return err
  296. }
  297. for range cmds {
  298. err = statusCmd.readReply(rd)
  299. if err != nil && !internal.IsRedisError(err) {
  300. return err
  301. }
  302. }
  303. // Parse number of replies.
  304. line, err := rd.ReadLine()
  305. if err != nil {
  306. if err == Nil {
  307. err = TxFailedErr
  308. }
  309. return err
  310. }
  311. switch line[0] {
  312. case proto.ErrorReply:
  313. return proto.ParseErrorReply(line)
  314. case proto.ArrayReply:
  315. // ok
  316. default:
  317. err := fmt.Errorf("redis: expected '*', but got line %q", line)
  318. return err
  319. }
  320. return nil
  321. }
  322. //------------------------------------------------------------------------------
  323. // Client is a Redis client representing a pool of zero or more
  324. // underlying connections. It's safe for concurrent use by multiple
  325. // goroutines.
  326. type Client struct {
  327. baseClient
  328. cmdable
  329. ctx context.Context
  330. }
  331. // NewClient returns a client to the Redis Server specified by Options.
  332. func NewClient(opt *Options) *Client {
  333. opt.init()
  334. c := Client{
  335. baseClient: baseClient{
  336. opt: opt,
  337. connPool: newConnPool(opt),
  338. },
  339. }
  340. c.baseClient.init()
  341. c.init()
  342. return &c
  343. }
  344. func (c *Client) init() {
  345. c.cmdable.setProcessor(c.Process)
  346. }
  347. func (c *Client) Context() context.Context {
  348. if c.ctx != nil {
  349. return c.ctx
  350. }
  351. return context.Background()
  352. }
  353. func (c *Client) WithContext(ctx context.Context) *Client {
  354. if ctx == nil {
  355. panic("nil context")
  356. }
  357. c2 := c.clone()
  358. c2.ctx = ctx
  359. return c2
  360. }
  361. func (c *Client) clone() *Client {
  362. cp := *c
  363. cp.init()
  364. return &cp
  365. }
  366. // Options returns read-only Options that were used to create the client.
  367. func (c *Client) Options() *Options {
  368. return c.opt
  369. }
  370. func (c *Client) SetLimiter(l Limiter) *Client {
  371. c.limiter = l
  372. return c
  373. }
  374. type PoolStats pool.Stats
  375. // PoolStats returns connection pool stats.
  376. func (c *Client) PoolStats() *PoolStats {
  377. stats := c.connPool.Stats()
  378. return (*PoolStats)(stats)
  379. }
  380. func (c *Client) Pipelined(fn func(Pipeliner) error) ([]Cmder, error) {
  381. return c.Pipeline().Pipelined(fn)
  382. }
  383. func (c *Client) Pipeline() Pipeliner {
  384. pipe := Pipeline{
  385. exec: c.processPipeline,
  386. }
  387. pipe.statefulCmdable.setProcessor(pipe.Process)
  388. return &pipe
  389. }
  390. func (c *Client) TxPipelined(fn func(Pipeliner) error) ([]Cmder, error) {
  391. return c.TxPipeline().Pipelined(fn)
  392. }
  393. // TxPipeline acts like Pipeline, but wraps queued commands with MULTI/EXEC.
  394. func (c *Client) TxPipeline() Pipeliner {
  395. pipe := Pipeline{
  396. exec: c.processTxPipeline,
  397. }
  398. pipe.statefulCmdable.setProcessor(pipe.Process)
  399. return &pipe
  400. }
  401. func (c *Client) pubSub() *PubSub {
  402. pubsub := &PubSub{
  403. opt: c.opt,
  404. newConn: func(channels []string) (*pool.Conn, error) {
  405. return c.newConn()
  406. },
  407. closeConn: c.connPool.CloseConn,
  408. }
  409. pubsub.init()
  410. return pubsub
  411. }
  412. // Subscribe subscribes the client to the specified channels.
  413. // Channels can be omitted to create empty subscription.
  414. // Note that this method does not wait on a response from Redis, so the
  415. // subscription may not be active immediately. To force the connection to wait,
  416. // you may call the Receive() method on the returned *PubSub like so:
  417. //
  418. // sub := client.Subscribe(queryResp)
  419. // iface, err := sub.Receive()
  420. // if err != nil {
  421. // // handle error
  422. // }
  423. //
  424. // // Should be *Subscription, but others are possible if other actions have been
  425. // // taken on sub since it was created.
  426. // switch iface.(type) {
  427. // case *Subscription:
  428. // // subscribe succeeded
  429. // case *Message:
  430. // // received first message
  431. // case *Pong:
  432. // // pong received
  433. // default:
  434. // // handle error
  435. // }
  436. //
  437. // ch := sub.Channel()
  438. func (c *Client) Subscribe(channels ...string) *PubSub {
  439. pubsub := c.pubSub()
  440. if len(channels) > 0 {
  441. _ = pubsub.Subscribe(channels...)
  442. }
  443. return pubsub
  444. }
  445. // PSubscribe subscribes the client to the given patterns.
  446. // Patterns can be omitted to create empty subscription.
  447. func (c *Client) PSubscribe(channels ...string) *PubSub {
  448. pubsub := c.pubSub()
  449. if len(channels) > 0 {
  450. _ = pubsub.PSubscribe(channels...)
  451. }
  452. return pubsub
  453. }
  454. //------------------------------------------------------------------------------
  455. // Conn is like Client, but its pool contains single connection.
  456. type Conn struct {
  457. baseClient
  458. statefulCmdable
  459. }
  460. func newConn(opt *Options, cn *pool.Conn) *Conn {
  461. connPool := pool.NewSingleConnPool(nil)
  462. connPool.SetConn(cn)
  463. c := Conn{
  464. baseClient: baseClient{
  465. opt: opt,
  466. connPool: connPool,
  467. },
  468. }
  469. c.baseClient.init()
  470. c.statefulCmdable.setProcessor(c.Process)
  471. return &c
  472. }
  473. func (c *Conn) Pipelined(fn func(Pipeliner) error) ([]Cmder, error) {
  474. return c.Pipeline().Pipelined(fn)
  475. }
  476. func (c *Conn) Pipeline() Pipeliner {
  477. pipe := Pipeline{
  478. exec: c.processPipeline,
  479. }
  480. pipe.statefulCmdable.setProcessor(pipe.Process)
  481. return &pipe
  482. }
  483. func (c *Conn) TxPipelined(fn func(Pipeliner) error) ([]Cmder, error) {
  484. return c.TxPipeline().Pipelined(fn)
  485. }
  486. // TxPipeline acts like Pipeline, but wraps queued commands with MULTI/EXEC.
  487. func (c *Conn) TxPipeline() Pipeliner {
  488. pipe := Pipeline{
  489. exec: c.processTxPipeline,
  490. }
  491. pipe.statefulCmdable.setProcessor(pipe.Process)
  492. return &pipe
  493. }