pipe.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package mongo
  2. import (
  3. "time"
  4. "git.i2edu.net/i2/go-zero/core/breaker"
  5. "github.com/globalsign/mgo"
  6. )
  7. type (
  8. // Pipe interface represents a mongo pipe.
  9. Pipe interface {
  10. All(result interface{}) error
  11. AllowDiskUse() Pipe
  12. Batch(n int) Pipe
  13. Collation(collation *mgo.Collation) Pipe
  14. Explain(result interface{}) error
  15. Iter() Iter
  16. One(result interface{}) error
  17. SetMaxTime(d time.Duration) Pipe
  18. }
  19. promisedPipe struct {
  20. *mgo.Pipe
  21. promise keepablePromise
  22. }
  23. rejectedPipe struct{}
  24. )
  25. func (p promisedPipe) All(result interface{}) error {
  26. return p.promise.keep(p.Pipe.All(result))
  27. }
  28. func (p promisedPipe) AllowDiskUse() Pipe {
  29. p.Pipe.AllowDiskUse()
  30. return p
  31. }
  32. func (p promisedPipe) Batch(n int) Pipe {
  33. p.Pipe.Batch(n)
  34. return p
  35. }
  36. func (p promisedPipe) Collation(collation *mgo.Collation) Pipe {
  37. p.Pipe.Collation(collation)
  38. return p
  39. }
  40. func (p promisedPipe) Explain(result interface{}) error {
  41. return p.promise.keep(p.Pipe.Explain(result))
  42. }
  43. func (p promisedPipe) Iter() Iter {
  44. return promisedIter{
  45. Iter: p.Pipe.Iter(),
  46. promise: p.promise,
  47. }
  48. }
  49. func (p promisedPipe) One(result interface{}) error {
  50. return p.promise.keep(p.Pipe.One(result))
  51. }
  52. func (p promisedPipe) SetMaxTime(d time.Duration) Pipe {
  53. p.Pipe.SetMaxTime(d)
  54. return p
  55. }
  56. func (p rejectedPipe) All(result interface{}) error {
  57. return breaker.ErrServiceUnavailable
  58. }
  59. func (p rejectedPipe) AllowDiskUse() Pipe {
  60. return p
  61. }
  62. func (p rejectedPipe) Batch(n int) Pipe {
  63. return p
  64. }
  65. func (p rejectedPipe) Collation(collation *mgo.Collation) Pipe {
  66. return p
  67. }
  68. func (p rejectedPipe) Explain(result interface{}) error {
  69. return breaker.ErrServiceUnavailable
  70. }
  71. func (p rejectedPipe) Iter() Iter {
  72. return rejectedIter{}
  73. }
  74. func (p rejectedPipe) One(result interface{}) error {
  75. return breaker.ErrServiceUnavailable
  76. }
  77. func (p rejectedPipe) SetMaxTime(d time.Duration) Pipe {
  78. return p
  79. }