http urls monitor.

pipeline.go 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package redis
  2. import (
  3. "sync"
  4. "github.com/go-redis/redis/internal/pool"
  5. )
  6. type pipelineExecer func([]Cmder) error
  7. type Pipeliner interface {
  8. StatefulCmdable
  9. Process(cmd Cmder) error
  10. Close() error
  11. Discard() error
  12. Exec() ([]Cmder, error)
  13. }
  14. var _ Pipeliner = (*Pipeline)(nil)
  15. // Pipeline implements pipelining as described in
  16. // http://redis.io/topics/pipelining. It's safe for concurrent use
  17. // by multiple goroutines.
  18. type Pipeline struct {
  19. statefulCmdable
  20. exec pipelineExecer
  21. mu sync.Mutex
  22. cmds []Cmder
  23. closed bool
  24. }
  25. // Process queues the cmd for later execution.
  26. func (c *Pipeline) Process(cmd Cmder) error {
  27. c.mu.Lock()
  28. c.cmds = append(c.cmds, cmd)
  29. c.mu.Unlock()
  30. return nil
  31. }
  32. // Close closes the pipeline, releasing any open resources.
  33. func (c *Pipeline) Close() error {
  34. c.mu.Lock()
  35. c.discard()
  36. c.closed = true
  37. c.mu.Unlock()
  38. return nil
  39. }
  40. // Discard resets the pipeline and discards queued commands.
  41. func (c *Pipeline) Discard() error {
  42. c.mu.Lock()
  43. err := c.discard()
  44. c.mu.Unlock()
  45. return err
  46. }
  47. func (c *Pipeline) discard() error {
  48. if c.closed {
  49. return pool.ErrClosed
  50. }
  51. c.cmds = c.cmds[:0]
  52. return nil
  53. }
  54. // Exec executes all previously queued commands using one
  55. // client-server roundtrip.
  56. //
  57. // Exec always returns list of commands and error of the first failed
  58. // command if any.
  59. func (c *Pipeline) Exec() ([]Cmder, error) {
  60. c.mu.Lock()
  61. defer c.mu.Unlock()
  62. if c.closed {
  63. return nil, pool.ErrClosed
  64. }
  65. if len(c.cmds) == 0 {
  66. return nil, nil
  67. }
  68. cmds := c.cmds
  69. c.cmds = nil
  70. return cmds, c.exec(cmds)
  71. }
  72. func (c *Pipeline) pipelined(fn func(Pipeliner) error) ([]Cmder, error) {
  73. if err := fn(c); err != nil {
  74. return nil, err
  75. }
  76. cmds, err := c.Exec()
  77. _ = c.Close()
  78. return cmds, err
  79. }
  80. func (c *Pipeline) Pipelined(fn func(Pipeliner) error) ([]Cmder, error) {
  81. return c.pipelined(fn)
  82. }
  83. func (c *Pipeline) Pipeline() Pipeliner {
  84. return c
  85. }
  86. func (c *Pipeline) TxPipelined(fn func(Pipeliner) error) ([]Cmder, error) {
  87. return c.pipelined(fn)
  88. }
  89. func (c *Pipeline) TxPipeline() Pipeliner {
  90. return c
  91. }