pipeline.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package redis
  2. import (
  3. "context"
  4. "sync"
  5. "github.com/go-redis/redis/internal/pool"
  6. )
  7. type pipelineExecer func(context.Context, []Cmder) error
  8. // Pipeliner is an mechanism to realise Redis Pipeline technique.
  9. //
  10. // Pipelining is a technique to extremely speed up processing by packing
  11. // operations to batches, send them at once to Redis and read a replies in a
  12. // singe step.
  13. // See https://redis.io/topics/pipelining
  14. //
  15. // Pay attention, that Pipeline is not a transaction, so you can get unexpected
  16. // results in case of big pipelines and small read/write timeouts.
  17. // Redis client has retransmission logic in case of timeouts, pipeline
  18. // can be retransmitted and commands can be executed more then once.
  19. // To avoid this: it is good idea to use reasonable bigger read/write timeouts
  20. // depends of your batch size and/or use TxPipeline.
  21. type Pipeliner interface {
  22. StatefulCmdable
  23. Do(args ...interface{}) *Cmd
  24. Process(cmd Cmder) error
  25. Close() error
  26. Discard() error
  27. Exec() ([]Cmder, error)
  28. ExecContext(ctx context.Context) ([]Cmder, error)
  29. }
  30. var _ Pipeliner = (*Pipeline)(nil)
  31. // Pipeline implements pipelining as described in
  32. // http://redis.io/topics/pipelining. It's safe for concurrent use
  33. // by multiple goroutines.
  34. type Pipeline struct {
  35. cmdable
  36. statefulCmdable
  37. ctx context.Context
  38. exec pipelineExecer
  39. mu sync.Mutex
  40. cmds []Cmder
  41. closed bool
  42. }
  43. func (c *Pipeline) init() {
  44. c.cmdable = c.Process
  45. c.statefulCmdable = c.Process
  46. }
  47. func (c *Pipeline) Do(args ...interface{}) *Cmd {
  48. cmd := NewCmd(args...)
  49. _ = c.Process(cmd)
  50. return cmd
  51. }
  52. // Process queues the cmd for later execution.
  53. func (c *Pipeline) Process(cmd Cmder) error {
  54. c.mu.Lock()
  55. c.cmds = append(c.cmds, cmd)
  56. c.mu.Unlock()
  57. return nil
  58. }
  59. // Close closes the pipeline, releasing any open resources.
  60. func (c *Pipeline) Close() error {
  61. c.mu.Lock()
  62. _ = c.discard()
  63. c.closed = true
  64. c.mu.Unlock()
  65. return nil
  66. }
  67. // Discard resets the pipeline and discards queued commands.
  68. func (c *Pipeline) Discard() error {
  69. c.mu.Lock()
  70. err := c.discard()
  71. c.mu.Unlock()
  72. return err
  73. }
  74. func (c *Pipeline) discard() error {
  75. if c.closed {
  76. return pool.ErrClosed
  77. }
  78. c.cmds = c.cmds[:0]
  79. return nil
  80. }
  81. // Exec executes all previously queued commands using one
  82. // client-server roundtrip.
  83. //
  84. // Exec always returns list of commands and error of the first failed
  85. // command if any.
  86. func (c *Pipeline) Exec() ([]Cmder, error) {
  87. return c.ExecContext(c.ctx)
  88. }
  89. func (c *Pipeline) ExecContext(ctx context.Context) ([]Cmder, error) {
  90. c.mu.Lock()
  91. defer c.mu.Unlock()
  92. if c.closed {
  93. return nil, pool.ErrClosed
  94. }
  95. if len(c.cmds) == 0 {
  96. return nil, nil
  97. }
  98. cmds := c.cmds
  99. c.cmds = nil
  100. return cmds, c.exec(ctx, cmds)
  101. }
  102. func (c *Pipeline) Pipelined(fn func(Pipeliner) error) ([]Cmder, error) {
  103. if err := fn(c); err != nil {
  104. return nil, err
  105. }
  106. cmds, err := c.Exec()
  107. _ = c.Close()
  108. return cmds, err
  109. }
  110. func (c *Pipeline) Pipeline() Pipeliner {
  111. return c
  112. }
  113. func (c *Pipeline) TxPipelined(fn func(Pipeliner) error) ([]Cmder, error) {
  114. return c.Pipelined(fn)
  115. }
  116. func (c *Pipeline) TxPipeline() Pipeliner {
  117. return c
  118. }