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.

138 lines
3.1 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. Do(ctx context.Context, args ...interface{}) *Cmd
  24. Process(ctx context.Context, cmd Cmder) error
  25. Close() error
  26. Discard() error
  27. Exec(ctx context.Context) ([]Cmder, error)
  28. }
  29. var _ Pipeliner = (*Pipeline)(nil)
  30. // Pipeline implements pipelining as described in
  31. // http://redis.io/topics/pipelining. It's safe for concurrent use
  32. // by multiple goroutines.
  33. type Pipeline struct {
  34. cmdable
  35. statefulCmdable
  36. ctx context.Context
  37. exec pipelineExecer
  38. mu sync.Mutex
  39. cmds []Cmder
  40. closed bool
  41. }
  42. func (c *Pipeline) init() {
  43. c.cmdable = c.Process
  44. c.statefulCmdable = c.Process
  45. }
  46. func (c *Pipeline) Do(ctx context.Context, args ...interface{}) *Cmd {
  47. cmd := NewCmd(ctx, args...)
  48. _ = c.Process(ctx, cmd)
  49. return cmd
  50. }
  51. // Process queues the cmd for later execution.
  52. func (c *Pipeline) Process(ctx context.Context, cmd Cmder) error {
  53. c.mu.Lock()
  54. c.cmds = append(c.cmds, cmd)
  55. c.mu.Unlock()
  56. return nil
  57. }
  58. // Close closes the pipeline, releasing any open resources.
  59. func (c *Pipeline) Close() error {
  60. c.mu.Lock()
  61. _ = c.discard()
  62. c.closed = true
  63. c.mu.Unlock()
  64. return nil
  65. }
  66. // Discard resets the pipeline and discards queued commands.
  67. func (c *Pipeline) Discard() error {
  68. c.mu.Lock()
  69. err := c.discard()
  70. c.mu.Unlock()
  71. return err
  72. }
  73. func (c *Pipeline) discard() error {
  74. if c.closed {
  75. return pool.ErrClosed
  76. }
  77. c.cmds = c.cmds[:0]
  78. return nil
  79. }
  80. // Exec executes all previously queued commands using one
  81. // client-server roundtrip.
  82. //
  83. // Exec always returns list of commands and error of the first failed
  84. // command if any.
  85. func (c *Pipeline) Exec(ctx context.Context) ([]Cmder, error) {
  86. c.mu.Lock()
  87. defer c.mu.Unlock()
  88. if c.closed {
  89. return nil, pool.ErrClosed
  90. }
  91. if len(c.cmds) == 0 {
  92. return nil, nil
  93. }
  94. cmds := c.cmds
  95. c.cmds = nil
  96. return cmds, c.exec(ctx, cmds)
  97. }
  98. func (c *Pipeline) Pipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error) {
  99. if err := fn(c); err != nil {
  100. return nil, err
  101. }
  102. cmds, err := c.Exec(ctx)
  103. _ = c.Close()
  104. return cmds, err
  105. }
  106. func (c *Pipeline) Pipeline() Pipeliner {
  107. return c
  108. }
  109. func (c *Pipeline) TxPipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error) {
  110. return c.Pipelined(ctx, fn)
  111. }
  112. func (c *Pipeline) TxPipeline() Pipeliner {
  113. return c
  114. }