/
pagenation.go
104 lines (86 loc) · 1.75 KB
/
pagenation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package api
import (
"context"
"math"
"sync"
"github.com/google/go-github/v28/github"
)
const (
PerPage = 100
)
type Counter struct {
n int
m sync.Mutex
}
func (c *Counter) Increment() {
c.m.Lock()
c.n++
c.m.Unlock()
}
func (c *Counter) Num() int {
return c.n
}
type PagenationResult struct {
i interface{}
r *github.Response
e error
}
func (pr *PagenationResult) Interface() interface{} {
return pr.i
}
func (pr *PagenationResult) Response() *github.Response {
return pr.r
}
func (pr *PagenationResult) Error() error {
return pr.e
}
type Pagenation struct {
ch chan *PagenationResult
wg *sync.WaitGroup
cnt Counter
}
func (p *Pagenation) Request(ctx context.Context, callback func(opt *github.ListOptions) (interface{}, *github.Response, error)) {
p.RequestWithLimit(ctx, math.MaxInt64, callback)
}
func (p *Pagenation) RequestWithLimit(ctx context.Context, maxPage int, callback func(opt *github.ListOptions) (interface{}, *github.Response, error)) {
p.cnt.Increment()
go func() {
o := &github.ListOptions{
Page: 0,
PerPage: PerPage,
}
data, resp, err := callback(o)
p.ch <- &PagenationResult{data, resp, err}
if err != nil {
return
}
lastPage := resp.LastPage
if lastPage > maxPage {
lastPage = maxPage
}
for i := 1; i < lastPage; i++ {
p.cnt.Increment()
go func(page int) {
o.Page = page
data, resp, err := callback(o)
p.ch <- &PagenationResult{data, resp, err}
}(i)
}
}()
}
func (p *Pagenation) Done() <-chan *PagenationResult {
return p.ch
}
func (p *Pagenation) RequestedNum() int {
return p.cnt.Num()
}
func NewPagenation() *Pagenation {
return &Pagenation{
ch: make(chan *PagenationResult),
wg: new(sync.WaitGroup),
cnt: Counter{
n: 0,
m: sync.Mutex{},
},
}
}