iter.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //go:generate mockgen -package mongo -destination iter_mock.go -source iter.go Iter
  2. package mongo
  3. import (
  4. "github.com/globalsign/mgo/bson"
  5. "github.com/tal-tech/go-zero/core/breaker"
  6. )
  7. type (
  8. Iter interface {
  9. All(result interface{}) error
  10. Close() error
  11. Done() bool
  12. Err() error
  13. For(result interface{}, f func() error) error
  14. Next(result interface{}) bool
  15. State() (int64, []bson.Raw)
  16. Timeout() bool
  17. }
  18. ClosableIter struct {
  19. Iter
  20. Cleanup func()
  21. }
  22. promisedIter struct {
  23. Iter
  24. promise keepablePromise
  25. }
  26. rejectedIter struct{}
  27. )
  28. func (i promisedIter) All(result interface{}) error {
  29. return i.promise.keep(i.Iter.All(result))
  30. }
  31. func (i promisedIter) Close() error {
  32. return i.promise.keep(i.Iter.Close())
  33. }
  34. func (i promisedIter) Err() error {
  35. return i.Iter.Err()
  36. }
  37. func (i promisedIter) For(result interface{}, f func() error) error {
  38. var ferr error
  39. err := i.Iter.For(result, func() error {
  40. ferr = f()
  41. return ferr
  42. })
  43. if ferr == err {
  44. return i.promise.accept(err)
  45. }
  46. return i.promise.keep(err)
  47. }
  48. func (it *ClosableIter) Close() error {
  49. err := it.Iter.Close()
  50. it.Cleanup()
  51. return err
  52. }
  53. func (i rejectedIter) All(result interface{}) error {
  54. return breaker.ErrServiceUnavailable
  55. }
  56. func (i rejectedIter) Close() error {
  57. return breaker.ErrServiceUnavailable
  58. }
  59. func (i rejectedIter) Done() bool {
  60. return false
  61. }
  62. func (i rejectedIter) Err() error {
  63. return breaker.ErrServiceUnavailable
  64. }
  65. func (i rejectedIter) For(result interface{}, f func() error) error {
  66. return breaker.ErrServiceUnavailable
  67. }
  68. func (i rejectedIter) Next(result interface{}) bool {
  69. return false
  70. }
  71. func (i rejectedIter) State() (int64, []bson.Raw) {
  72. return 0, nil
  73. }
  74. func (i rejectedIter) Timeout() bool {
  75. return false
  76. }