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.
 
 
 
 

137 regels
3.6 KiB

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