-
Notifications
You must be signed in to change notification settings - Fork 1
/
github.go
99 lines (94 loc) · 2.79 KB
/
github.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
package dadsgha
import (
"context"
"io/ioutil"
"strings"
"time"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
// GHClient - get GitHub client
func GHClient(ctx *Ctx) (ghCtx context.Context, clients []*github.Client) {
if ctx.Debug > 0 {
defer func() {
Printf("configured %d GitHub OAuth clients\n", len(clients))
}()
}
// Get GitHub OAuth from env or from file
oAuth := ctx.GitHubOAuth
if strings.Contains(ctx.GitHubOAuth, "/") {
bytes, err := ioutil.ReadFile(ctx.GitHubOAuth)
FatalOnError(err)
oAuth = strings.TrimSpace(string(bytes))
}
// GitHub authentication or use public access
ghCtx = context.Background()
if oAuth == "" {
client := github.NewClient(nil)
clients = append(clients, client)
} else {
oAuths := strings.Split(oAuth, ",")
for _, auth := range oAuths {
ctx.OAuthKeys = append(ctx.OAuthKeys, auth)
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: auth},
)
tc := oauth2.NewClient(ghCtx, ts)
client := github.NewClient(tc)
clients = append(clients, client)
}
}
return
}
// GetRateLimits - returns all and remaining API points and duration to wait for reset
// when core=true - returns Core limits, when core=false returns Search limits
func GetRateLimits(gctx context.Context, ctx *Ctx, gcs []*github.Client, core bool) (int, []int, []int, []time.Duration) {
var (
limits []int
remainings []int
durations []time.Duration
)
display := false
for idx, gc := range gcs {
rl, _, err := gc.RateLimits(gctx)
if err != nil {
rem, ok := PeriodParse(err.Error())
if ok {
Printf("Parsed wait time from error message: %v\n", rem)
limits = append(limits, -1)
remainings = append(remainings, -1)
durations = append(durations, rem)
display = true
continue
}
Printf("GetRateLimit(%d): %v\n", idx, err)
}
if rl == nil {
limits = append(limits, -1)
remainings = append(remainings, -1)
durations = append(durations, time.Duration(5)*time.Second)
continue
}
if core {
limits = append(limits, rl.Core.Limit)
remainings = append(remainings, rl.Core.Remaining)
durations = append(durations, rl.Core.Reset.Time.Sub(time.Now())+time.Duration(1)*time.Second)
continue
}
limits = append(limits, rl.Search.Limit)
remainings = append(remainings, rl.Search.Remaining)
durations = append(durations, rl.Search.Reset.Time.Sub(time.Now())+time.Duration(1)*time.Second)
}
hint := 0
for idx := range limits {
if remainings[idx] > remainings[hint] {
hint = idx
} else if idx != hint && remainings[idx] == remainings[hint] && durations[idx] < durations[hint] {
hint = idx
}
}
if display || ctx.Debug > 0 {
Printf("GetRateLimits: hint: %d, limits: %+v, remaining: %+v, reset: %+v\n", hint, limits, remainings, durations)
}
return hint, limits, remainings, durations
}