forked from inominate/eve-api-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workers.go
256 lines (216 loc) · 6.55 KB
/
workers.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package main
import (
"fmt"
"io"
"log"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/inominate/apicache"
)
var apiClient apicache.Client
// worker tracking
var activeWorkerCount, workerCount int32
var workCount []int32
type apiReq struct {
apiReq *apicache.Request
apiResp *apicache.Response
apiErr apicache.APIError
expires time.Time
worker int
httpCode int
err error
respChan chan apiReq
}
// Channel for sending jobs to workers
var workChan chan apiReq
func APIReq(url string, params map[string]string) (*apicache.Response, error) {
var errorStr string
if atomic.LoadInt32(&workerCount) <= 0 {
panic("No workers!")
}
// Build the request
apireq := apicache.NewRequest(url)
for k, v := range params {
switch k {
case "force":
if v != "" {
apireq.Force = true
}
default:
apireq.Set(k, v)
}
}
workerID := "C"
// Don't send it to a worker if we can just yank it fromm the cache
apiResp, err := apireq.GetCached()
if err != nil || apireq.Force {
for i := 0; i < conf.Retries; i++ {
respChan := make(chan apiReq)
req := apiReq{apiReq: apireq, respChan: respChan}
workChan <- req
resp := <-respChan
close(respChan)
apiResp = resp.apiResp
err = resp.err
workerID = fmt.Sprintf("%d", resp.worker)
// Attempt to recover from server issues, invalidate flag means we
// believe this is not a server failure.
// 418 is the tempban code
// 500/900 are panic codes
if err == nil || apiResp.Invalidate || apiResp.HTTPCode == 418 || apiResp.HTTPCode == 500 || apiResp.HTTPCode == 900 {
break
}
time.Sleep(2 * time.Second)
apireq.Force = true
}
}
// I HATE 221 HATE HATE HAAAAAAAATE
var singleDebug bool
if apiResp.Error.ErrorCode == 221 {
singleDebug = true
}
// This is similar to the request log, but knows more about where it came from.
if debug || singleDebug {
if apiResp.Error.ErrorCode != 0 {
errorStr = fmt.Sprintf(" Error %d: %s", apiResp.Error.ErrorCode, apiResp.Error.ErrorText)
}
logParams := ""
var paramVal string
for k, _ := range params {
if conf.Logging.CensorLog && strings.ToLower(k) == "vcode" && len(params[k]) > 8 {
paramVal = params[k][0:8] + "..."
} else {
paramVal = params[k]
}
logParams = fmt.Sprintf("%s&%s=%s", logParams, k, paramVal)
}
if logParams != "" {
logParams = "?" + logParams[1:]
}
debugLog.Printf("w%s: %s%s HTTP: %d Expires: %s%s", workerID, url, logParams, apiResp.HTTPCode, apiResp.Expires.Format("2006-01-02 15:04:05"), errorStr)
}
return apiResp, err
}
func worker(reqChan chan apiReq, workerID int) {
atomic.AddInt32(&workerCount, 1)
for req := range reqChan {
var err, eErr, rErr error
var errStr string
atomic.AddInt32(&activeWorkerCount, 1)
// Run both of the error limiters simultaneously rather than in
// sequence. Still need both before we continue.
errorLimiter := make(chan error)
rpsLimiter := make(chan error)
go func() {
err := errorRateLimiter.Start(30 * time.Second)
errorLimiter <- err
}()
go func() {
err := rateLimiter.Start(30 * time.Second)
rpsLimiter <- err
}()
eErr = <-errorLimiter
rErr = <-rpsLimiter
// Check the error limiter for timeouts
if eErr != nil {
err = eErr
errStr = "error throttling"
// If the rate limiter didn't timeout be sure to signal it that we
// didn't do anything.
if rErr == nil {
rateLimiter.Finish(true)
}
}
if rErr != nil {
err = rErr
if errStr == "" {
errStr = "rate limiting"
} else {
errStr += " and rate limiting"
}
// If the error limiter didn't also timeout be sure to signal it that we
// didn't do anything.
if eErr == nil {
errorRateLimiter.Finish(true)
}
}
// We're left with a single err and errStr for returning an error to the client.
if err != nil {
log.Printf("Rate Limit Error: %s - %s", errStr, err)
log.Printf("RPS Events: %d Outstanding: %d", rateLimiter.Count(), rateLimiter.Outstanding())
log.Printf("Errors Events: %d Outstanding: %d", errorRateLimiter.Count(), errorRateLimiter.Outstanding())
req.apiResp = &apicache.Response{
Data: apicache.SynthesizeAPIError(500,
fmt.Sprintf("APIProxy Error: Proxy timeout due to %s.", errStr),
5*time.Minute),
Expires: time.Now().Add(5 * time.Minute),
Error: apicache.APIError{500,
fmt.Sprintf("APIProxy Error: Proxy timeout due to %s.", errStr)},
HTTPCode: 504,
}
req.err = err
} else {
resp, err := req.apiReq.Do()
req.apiResp = resp
req.err = err
if resp.Error.ErrorCode == 0 || resp.HTTPCode == 504 || resp.HTTPCode == 418 {
// 418 means we are currently tempbanned from the API.
// 504 means the API proxy had some kind of internal or network error.
//
// We do not treat these as an error for rate limiting because
// the apicache library handles it for us, these requests are
// not actually making it to the CCP API.
// Finish, but skip recording the event in the rate limiter
// when there is no error.
errorRateLimiter.Finish(true)
} else {
errorRateLimiter.Finish(false)
}
rateLimiter.Finish(false)
}
req.worker = workerID
req.respChan <- req
atomic.AddInt32(&workCount[workerID], 1)
atomic.AddInt32(&activeWorkerCount, -1)
}
atomic.AddInt32(&workerCount, -1)
}
var startWorkersOnce = &sync.Once{}
func startWorkers() {
startWorkersOnce.Do(realStartWorkers)
}
func realStartWorkers() {
log.Printf("Starting %d Workers...", conf.Workers)
workChan = make(chan apiReq)
workCount = make([]int32, conf.Workers+1)
for i := 1; i <= conf.Workers; i++ {
debugLog.Printf("Starting worker #%d.", i)
go worker(workChan, i)
}
}
func stopWorkers() {
close(workChan)
for atomic.LoadInt32(&workerCount) > 0 {
time.Sleep(10 * time.Millisecond)
}
startWorkersOnce = &sync.Once{}
}
func PrintWorkerStats(w io.Writer) {
active, loaded := GetWorkerStats()
fmt.Fprintf(w, "%d workers idle, %d workers active.\n", loaded-active, active)
rateCount := rateLimiter.Count()
rateOutstanding := rateLimiter.Outstanding()
errorCount := errorRateLimiter.Count()
errorOutstanding := errorRateLimiter.Outstanding()
fmt.Fprintf(w, "%d requests in the last second. %d requests outstanding.\n", rateCount, rateOutstanding)
fmt.Fprintf(w, "%d errors over last %d seconds. %d errors outstanding.\n", errorCount, conf.ErrorPeriod, errorOutstanding)
for i := int32(1); i <= atomic.LoadInt32(&workerCount); i++ {
count := atomic.LoadInt32(&workCount[i])
fmt.Fprintf(w, " %d: %d\n", i, count)
}
}
func GetWorkerStats() (int32, int32) {
return atomic.LoadInt32(&activeWorkerCount), atomic.LoadInt32(&workerCount)
}