-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathrest.go
More file actions
313 lines (263 loc) · 8.74 KB
/
Copy pathrest.go
File metadata and controls
313 lines (263 loc) · 8.74 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package tempest
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
"net"
"net/http"
"net/textproto"
"strings"
"sync/atomic"
"time"
)
var (
errRetryable = errors.New("a retryable error occurred")
errGlobalRateLimit = errors.New("hit global rate limit")
errTooManyRetries = errors.New("internal retry threshold exceeded - your code logic is probably unsafe to use at scale")
)
// Represents file you can attach to message on Discord.
type File struct {
Name string // File's display name
Reader io.Reader
}
type rateLimitError struct {
Message string `json:"message"`
RetryAfter float64 `json:"retry_after"`
Global bool `json:"global"`
}
type Rest struct {
HTTPClient http.Client
maxRetries uint8
maxWaitTime time.Duration
token string
limiter *RateLimiter
retryCounter atomic.Int64
retryThreshold int64
trippedUntil atomic.Int64 // UnixNano
}
type RestOptions struct {
Token string
MaxRetries uint8 // By default: 3
MaxWaitTime time.Duration // Max duration it can take for each request.
RetryThreshold uint32 // Max number of concurrent retries allowed before failing all ongoing requests (emergency breaks). By default: 60.
RateLimiterOptions RateLimiterOptions
}
func NewRest(opt RestOptions) *Rest {
_, err := extractUserIDFromToken(opt.Token)
if err != nil {
panic("failed to extract bot user ID from bot token: " + err.Error())
}
prefixedToken := opt.Token
if !strings.HasPrefix(prefixedToken, "Bot ") {
prefixedToken = "Bot " + prefixedToken
}
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
ForceAttemptHTTP2: false,
// MaxConnsPerHost: 256,
MaxIdleConns: 256,
IdleConnTimeout: 90 * time.Second,
}
maxTimeout := 10 * time.Second
if opt.MaxWaitTime != 0 {
maxTimeout = opt.MaxWaitTime
}
maxRetries := opt.MaxRetries
if opt.MaxRetries == 0 {
maxRetries = 3
}
retryThreshold := opt.RetryThreshold
if retryThreshold == 0 {
retryThreshold = 60
}
return &Rest{
HTTPClient: http.Client{
Transport: &rateLimitTransport{
limiter: NewRateLimiter(opt.RateLimiterOptions),
innerTransport: transport,
},
Timeout: maxTimeout,
},
token: prefixedToken,
maxRetries: maxRetries,
maxWaitTime: maxTimeout,
retryThreshold: int64(retryThreshold),
}
}
func (rest *Rest) Request(method, route string, jsonPayload any) ([]byte, error) {
var body io.ReadSeeker
if jsonPayload != nil {
var buf bytes.Buffer
encoder := json.NewEncoder(&buf)
encoder.SetEscapeHTML(false)
if err := encoder.Encode(jsonPayload); err != nil {
return nil, fmt.Errorf("failed to encode JSON payload: %w", err)
}
body = bytes.NewReader(buf.Bytes())
}
return rest.request(method, route, body, CONTENT_TYPE_JSON)
}
func (rest *Rest) isTripped() bool {
return rest.trippedUntil.Load() > time.Now().UnixNano()
}
// Internal handler for buffered requests. It's used by Request and RequestWithFiles (for PATCH only).
func (rest *Rest) request(method, route string, body io.ReadSeeker, contentType string) ([]byte, error) {
var (
responseBody []byte
lastErr error
retrying bool
)
defer func() {
if retrying {
rest.retryCounter.Add(-1)
}
}()
for i := uint8(0); i < rest.maxRetries; i++ {
if body != nil {
if _, err := body.Seek(0, io.SeekStart); err != nil {
return nil, fmt.Errorf("failed to seek request body: %w", err)
}
}
if rest.isTripped() {
return nil, errTooManyRetries
}
// #nosec G704
req, err := http.NewRequest(method, DiscordAPIBaseURL()+route, body)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", contentType)
responseBody, err = rest.executeOnce(req)
if err != nil {
lastErr = err
if errors.Is(err, errGlobalRateLimit) || errors.Is(err, errRetryable) {
if !retrying {
retrying = true
if rest.retryCounter.Add(1) > rest.retryThreshold {
rest.trippedUntil.Store(time.Now().Add(5 * time.Second).UnixNano())
return nil, fmt.Errorf("%w: global retry threshold (%d) exceeded", errTooManyRetries, rest.retryThreshold)
}
}
if errors.Is(err, errGlobalRateLimit) {
continue
}
time.Sleep(time.Millisecond * time.Duration(250*int64(i+1)))
continue
}
return nil, err // Not a retryable error.
}
return responseBody, nil
}
return nil, fmt.Errorf("request failed after %d retries on %s %s: %w", rest.maxRetries, method, route, lastErr)
}
func (rest *Rest) RequestWithFiles(method string, route string, jsonPayload any, files []File) ([]byte, error) {
if len(files) == 0 {
return rest.Request(method, route, jsonPayload)
}
// To avoid issues with retrying requests that have streaming bodies (which can't be re-read),
// we pre-buffer the multipart request into memory. This ensures that the request body can be
// reliably sent multiple times in case of retries.
var requestBody bytes.Buffer
writer := multipart.NewWriter(&requestBody)
if err := writeMultipart(writer, jsonPayload, files); err != nil {
return nil, err
}
return rest.request(method, route, bytes.NewReader(requestBody.Bytes()), writer.FormDataContentType())
}
// Writes the JSON payload and files to a multipart writer.
func writeMultipart(writer *multipart.Writer, jsonPayload any, files []File) error {
defer writer.Close() //nolint:errcheck
// Write JSON payload part.
jsonPart, err := writer.CreatePart(textproto.MIMEHeader{
"Content-Disposition": []string{`form-data; name="payload_json"`},
"Content-Type": []string{CONTENT_TYPE_JSON},
})
if err != nil {
return fmt.Errorf("failed to create payload_json part: %w", err)
}
encoder := json.NewEncoder(jsonPart)
encoder.SetEscapeHTML(false)
if err := encoder.Encode(jsonPayload); err != nil {
return fmt.Errorf("failed to encode payload_json: %w", err)
}
// Write file parts.
for i, file := range files {
if seeker, ok := file.Reader.(io.ReadSeeker); ok {
if _, err := seeker.Seek(0, io.SeekStart); err != nil {
return fmt.Errorf("failed to seek file [%s]: %w", file.Name, err)
}
}
filePart, err := writer.CreatePart(textproto.MIMEHeader{
"Content-Disposition": []string{fmt.Sprintf(`form-data; name="files[%d]"; filename="%s"`, i, file.Name)},
"Content-Type": []string{CONTENT_TYPE_OCTET_STREAM},
})
if err != nil {
return fmt.Errorf("failed to create file part [%d]: %w", i, err)
}
lr := io.LimitReader(file.Reader, MAX_FILE_UPLOAD_SIZE+1)
n, err := io.Copy(filePart, lr)
if err != nil {
return fmt.Errorf("failed to stream file [%s]: %w", file.Name, err)
}
if n > MAX_FILE_UPLOAD_SIZE {
return fmt.Errorf("file [%s] exceeds maximum upload size of 10MB", file.Name)
}
}
if err := writer.Close(); err != nil {
return fmt.Errorf("failed to close multipart writer: %w", err)
}
return nil
}
// executeOnce handles the lifecycle of a single request attempt.
func (rest *Rest) executeOnce(req *http.Request) ([]byte, error) {
req.Header.Set("User-Agent", USER_AGENT)
req.Header.Set("Authorization", rest.token)
res, err := rest.HTTPClient.Do(req)
if err != nil {
return nil, fmt.Errorf("%w: http request execution failed: %s", errRetryable, err.Error())
}
defer res.Body.Close() //nolint:errcheck
responseBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
if res.StatusCode >= http.StatusOK && res.StatusCode < http.StatusMultipleChoices {
return responseBody, nil
}
if res.StatusCode == http.StatusTooManyRequests {
var rateErr rateLimitError
err = json.Unmarshal(responseBody, &rateErr)
if err != nil {
return nil, fmt.Errorf("failed to parse rate limit details: %w", err)
}
if rateErr.Global {
retryAfter := time.Duration(rateErr.RetryAfter * float64(time.Second))
return nil, fmt.Errorf("%w: retrying after %s", errGlobalRateLimit, retryAfter.String())
}
return nil, fmt.Errorf("%w: hit per-route rate limit (429) on %s %s: %s", errRetryable, req.Method, req.URL.Path, string(responseBody))
}
if res.StatusCode >= http.StatusInternalServerError {
return nil, fmt.Errorf("%w: discord API internal server error: %s", errRetryable, res.Status)
}
return nil, fmt.Errorf("request failed with status %s: %s", res.Status, string(responseBody))
}
func extractUserIDFromToken(token string) (Snowflake, error) {
strs := strings.Split(token, ".")
if len(strs) == 0 {
return 0, errors.New("token is not in a valid format")
}
hexID := strings.Replace(strs[0], "Bot ", "", 1)
byteID, err := base64.RawStdEncoding.DecodeString(hexID)
if err != nil {
return 0, err
}
return StringToSnowflake(string(byteID))
}