-
-
Notifications
You must be signed in to change notification settings - Fork 481
/
session.go
185 lines (152 loc) · 4.13 KB
/
session.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package core
import (
"context"
"encoding/csv"
"fmt"
"math/rand"
"os"
"runtime"
"sync"
"time"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
type Session struct {
sync.Mutex
Version string
Log *Logger
Options *Options
Config *Config
Signatures []Signature
Repositories chan GitResource
Gists chan string
Comments chan string
Context context.Context
Clients chan *GitHubClientWrapper
ExhaustedClients chan *GitHubClientWrapper
CsvWriter *csv.Writer
}
var (
session *Session
sessionSync sync.Once
err error
)
func (s *Session) Start() {
rand.Seed(time.Now().Unix())
s.InitLogger()
s.InitThreads()
s.InitSignatures()
s.InitGitHubClients()
s.InitCsvWriter()
}
func (s *Session) InitLogger() {
s.Log = &Logger{}
s.Log.SetDebug(*s.Options.Debug)
s.Log.SetSilent(*s.Options.Silent)
}
func (s *Session) InitSignatures() {
s.Signatures = GetSignatures(s)
}
func (s *Session) InitGitHubClients() {
if len(*s.Options.Local) <= 0 {
chanSize := *s.Options.Threads * (len(s.Config.GitHubAccessTokens) + 1)
s.Clients = make(chan *GitHubClientWrapper, chanSize)
s.ExhaustedClients = make(chan *GitHubClientWrapper, chanSize)
for _, token := range s.Config.GitHubAccessTokens {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
tc := oauth2.NewClient(s.Context, ts)
client := github.NewClient(tc)
client.UserAgent = fmt.Sprintf("%s v%s", Name, Version)
_, _, err := client.Users.Get(s.Context, "")
if err != nil {
if _, ok := err.(*github.ErrorResponse); ok {
s.Log.Warn("Failed to validate token %s[..]: %s", token[:10], err)
continue
}
}
for i := 0; i <= *s.Options.Threads; i++ {
s.Clients <- &GitHubClientWrapper{client, token, time.Now().Add(-1 * time.Second)}
}
}
if len(s.Clients) < 1 {
s.Log.Fatal("No valid GitHub tokens provided. Quitting!")
}
}
}
func (s *Session) GetClient() *GitHubClientWrapper {
for {
select {
case client := <-s.Clients:
s.Log.Debug("Using client with token: %s", client.Token[:10])
return client
case client := <-s.ExhaustedClients:
sleepTime := time.Until(client.RateLimitedUntil)
s.Log.Warn("All GitHub tokens exhausted/rate limited. Sleeping for %s", sleepTime.String())
time.Sleep(sleepTime)
s.Log.Debug("Returning client %s to pool", client.Token[:10])
s.FreeClient(client)
default:
s.Log.Debug("Available Clients: %d", len(s.Clients))
s.Log.Debug("Exhausted Clients: %d", len(s.ExhaustedClients))
time.Sleep(time.Millisecond * 1000)
}
}
}
// FreeClient returns the GitHub Client to the pool of available,
// non-rate-limited channel of clients in the session
func (s *Session) FreeClient(client *GitHubClientWrapper) {
if client.RateLimitedUntil.After(time.Now()) {
s.ExhaustedClients <- client
} else {
s.Clients <- client
}
}
func (s *Session) InitThreads() {
if *s.Options.Threads == 0 {
numCPUs := runtime.NumCPU()
s.Options.Threads = &numCPUs
}
runtime.GOMAXPROCS(*s.Options.Threads + 1)
}
func (s *Session) InitCsvWriter() {
if *s.Options.CsvPath == "" {
return
}
writeHeader := false
if !PathExists(*s.Options.CsvPath) {
writeHeader = true
}
file, err := os.OpenFile(*s.Options.CsvPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
LogIfError("Could not create/open CSV file", err)
s.CsvWriter = csv.NewWriter(file)
if writeHeader {
s.WriteToCsv([]string{"Repository name", "Signature name", "Matching file", "Matches"})
}
}
func (s *Session) WriteToCsv(line []string) {
if *s.Options.CsvPath == "" {
return
}
s.CsvWriter.Write(line)
s.CsvWriter.Flush()
}
func GetSession() *Session {
sessionSync.Do(func() {
session = &Session{
Context: context.Background(),
Repositories: make(chan GitResource, 1000),
Gists: make(chan string, 100),
Comments: make(chan string, 1000),
}
if session.Options, err = ParseOptions(); err != nil {
fmt.Println(err)
os.Exit(1)
}
if session.Config, err = ParseConfig(session.Options); err != nil {
fmt.Println(err)
os.Exit(1)
}
session.Start()
})
return session
}