depager.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  5. */
  6. package depager
  7. import (
  8. "context"
  9. "fmt"
  10. )
  11. /*
  12. The `Page` interface must wrap server responses. This
  13. allows pagers to calculate page sizes and iterate over
  14. response aggregates.
  15. If the underlying value of this interface is `nil` (e.g. a
  16. nil pointer to a struct or a nil slice), `Elems()` will
  17. panic.
  18. */
  19. type Page[T any] interface {
  20. // Elems must return the items from this page
  21. Elems() []T
  22. // URI must return the URI associated with this page
  23. URI() string
  24. // Count must return the total number of items being paged
  25. Count() uint64
  26. }
  27. // Exposes the part of the client that depager understands.
  28. type Client[T any] interface {
  29. // NextPage returns the next page or it returns an error
  30. NextPage(
  31. page Page[T],
  32. offset uint64, // item offset at which to start page
  33. ) (err error)
  34. }
  35. type Pager[T any] interface {
  36. // Iter is intended to be used in a for-range loop
  37. Iter() <-chan T
  38. // IterPages iterates over whole pages rather than items
  39. IterPages() <-chan Page[T]
  40. // LastErr must return the first error encountered, if any
  41. LastErr() error
  42. // Abort causes the pager to relinquish all pages back to
  43. // the page pool and stop all running goroutines.
  44. Abort() error
  45. }
  46. func NewPager[T any](
  47. ctx context.Context,
  48. c Client[T],
  49. pagePool chan Page[T],
  50. ) Pager[T] {
  51. if len(pagePool) == 0 {
  52. panic("new pager: provided page pool is empty")
  53. }
  54. var pageSize uint64
  55. pg := <-pagePool
  56. pageSize = uint64(cap(pg.Elems()))
  57. pagePool <- pg
  58. ctx2, cancel := context.WithCancel(ctx)
  59. done := make(chan struct{})
  60. return &pager[T]{
  61. ctx: ctx2,
  62. cancel: cancel,
  63. done: done,
  64. client: c,
  65. n: pageSize,
  66. pagePool: pagePool,
  67. poolSize: len(pagePool),
  68. }
  69. }
  70. /*
  71. Retrieve n items in the range [m*n, m*n + n - 1], inclusive.
  72. We keep len(pagePool) pages buffered.
  73. */
  74. type pager[T any] struct {
  75. ctx context.Context
  76. cancel context.CancelFunc
  77. done chan struct{} // Notify Abort when finished.
  78. client Client[T]
  79. m uint64
  80. n uint64
  81. err error
  82. pagePool chan Page[T]
  83. poolSize int
  84. cnt uint64
  85. }
  86. func (p *pager[T]) iteratePages() <-chan Page[T] {
  87. ch := make(chan Page[T], len(p.pagePool))
  88. go func() {
  89. defer close(ch)
  90. var page Page[T]
  91. for {
  92. if p.ctx.Err() != nil {
  93. break
  94. }
  95. page = <-p.pagePool
  96. err := p.client.NextPage(page, p.m*p.n)
  97. if err != nil {
  98. p.err = err
  99. p.pagePool <- page
  100. return
  101. }
  102. if p.cnt == 0 {
  103. p.cnt = page.Count()
  104. }
  105. ch <- page
  106. if (p.m*p.n + p.n) >= p.cnt {
  107. return
  108. }
  109. p.m++
  110. }
  111. }()
  112. return ch
  113. }
  114. func (p *pager[T]) IterPages() <-chan Page[T] {
  115. ch := make(chan Page[T], p.n)
  116. go func() {
  117. defer close(p.done)
  118. defer close(ch)
  119. for page := range p.iteratePages() {
  120. if p.ctx.Err() != nil {
  121. p.pagePool <- page
  122. break
  123. }
  124. if p.err != nil {
  125. p.err = fmt.Errorf("pager: iterate pages: %s", p.err)
  126. p.pagePool <- page
  127. return
  128. }
  129. ch <- page
  130. }
  131. }()
  132. return ch
  133. }
  134. func (p *pager[T]) Iter() <-chan T {
  135. ch := make(chan T, p.n)
  136. go func() {
  137. defer close(p.done)
  138. defer close(ch)
  139. for page := range p.iteratePages() {
  140. if p.ctx.Err() != nil {
  141. p.pagePool <- page
  142. break
  143. }
  144. for _, i := range page.Elems() {
  145. ch <- i
  146. }
  147. p.pagePool <- page
  148. if p.err != nil {
  149. p.err = fmt.Errorf("pager: iterate items: %s", p.err)
  150. return
  151. }
  152. }
  153. }()
  154. return ch
  155. }
  156. func (p *pager[T]) LastErr() error {
  157. return p.err
  158. }
  159. func (p *pager[T]) Abort() error {
  160. p.cancel()
  161. <-p.done
  162. return p.ctx.Err()
  163. }