You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
3.3 KiB

  1. package redis
  2. import (
  3. "context"
  4. "sync"
  5. "github.com/go-redis/redis/v8/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. Len() int
  24. Do(ctx context.Context, args ...interface{}) *Cmd
  25. Process(ctx context.Context, cmd Cmder) error
  26. Close() error
  27. Discard() error
  28. Exec(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. // Len returns the number of queued commands.
  48. func (c *Pipeline) Len() int {
  49. c.mu.Lock()
  50. ln := len(c.cmds)
  51. c.mu.Unlock()
  52. return ln
  53. }
  54. // Do queues the custom command for later execution.
  55. func (c *Pipeline) Do(ctx context.Context, args ...interface{}) *Cmd {
  56. cmd := NewCmd(ctx, args...)
  57. _ = c.Process(ctx, cmd)
  58. return cmd
  59. }
  60. // Process queues the cmd for later execution.
  61. func (c *Pipeline) Process(ctx context.Context, cmd Cmder) error {
  62. c.mu.Lock()
  63. c.cmds = append(c.cmds, cmd)
  64. c.mu.Unlock()
  65. return nil
  66. }
  67. // Close closes the pipeline, releasing any open resources.
  68. func (c *Pipeline) Close() error {
  69. c.mu.Lock()
  70. _ = c.discard()
  71. c.closed = true
  72. c.mu.Unlock()
  73. return nil
  74. }
  75. // Discard resets the pipeline and discards queued commands.
  76. func (c *Pipeline) Discard() error {
  77. c.mu.Lock()
  78. err := c.discard()
  79. c.mu.Unlock()
  80. return err
  81. }
  82. func (c *Pipeline) discard() error {
  83. if c.closed {
  84. return pool.ErrClosed
  85. }
  86. c.cmds = c.cmds[:0]
  87. return nil
  88. }
  89. // Exec executes all previously queued commands using one
  90. // client-server roundtrip.
  91. //
  92. // Exec always returns list of commands and error of the first failed
  93. // command if any.
  94. func (c *Pipeline) Exec(ctx context.Context) ([]Cmder, error) {
  95. c.mu.Lock()
  96. defer c.mu.Unlock()
  97. if c.closed {
  98. return nil, pool.ErrClosed
  99. }
  100. if len(c.cmds) == 0 {
  101. return nil, nil
  102. }
  103. cmds := c.cmds
  104. c.cmds = nil
  105. return cmds, c.exec(ctx, cmds)
  106. }
  107. func (c *Pipeline) Pipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error) {
  108. if err := fn(c); err != nil {
  109. return nil, err
  110. }
  111. cmds, err := c.Exec(ctx)
  112. _ = c.Close()
  113. return cmds, err
  114. }
  115. func (c *Pipeline) Pipeline() Pipeliner {
  116. return c
  117. }
  118. func (c *Pipeline) TxPipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error) {
  119. return c.Pipelined(ctx, fn)
  120. }
  121. func (c *Pipeline) TxPipeline() Pipeliner {
  122. return c
  123. }