forked from pothtonswer/discordmuffin
BREAKING - Added RateLimited event
Renamed RateLimit struct to TooManyRequests{} and added new event struct RateLimited{} which can be registerd to with AddHandler() and will be emitted anytime a HTTP 429 is received on the HTTP API.
This commit is contained in:
parent
94770635a9
commit
098d7861a4
3 changed files with 12 additions and 3 deletions
|
@ -50,6 +50,12 @@ type Connect struct{}
|
||||||
// Disconnect is an empty struct for an event.
|
// Disconnect is an empty struct for an event.
|
||||||
type Disconnect struct{}
|
type Disconnect struct{}
|
||||||
|
|
||||||
|
// RateLimited is a struct for the RateLimited event
|
||||||
|
type RateLimited struct {
|
||||||
|
*TooManyRequests
|
||||||
|
URL string
|
||||||
|
}
|
||||||
|
|
||||||
// MessageCreate is a wrapper struct for an event.
|
// MessageCreate is a wrapper struct for an event.
|
||||||
type MessageCreate struct {
|
type MessageCreate struct {
|
||||||
*Message
|
*Message
|
||||||
|
|
|
@ -136,13 +136,15 @@ func (s *Session) request(method, urlStr, contentType string, b []byte) (respons
|
||||||
|
|
||||||
case 429: // TOO MANY REQUESTS - Rate limiting
|
case 429: // TOO MANY REQUESTS - Rate limiting
|
||||||
|
|
||||||
rl := RateLimit{}
|
rl := TooManyRequests{}
|
||||||
err = json.Unmarshal(response, &rl)
|
err = json.Unmarshal(response, &rl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.log(LogError, "rate limit unmarshal error, %s", err)
|
s.log(LogError, "rate limit unmarshal error, %s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.log(LogInformational, "Rate Limiting %s, retry in %d", urlStr, rl.RetryAfter)
|
s.log(LogInformational, "Rate Limiting %s, retry in %d", urlStr, rl.RetryAfter)
|
||||||
|
s.handle(RateLimited{TooManyRequests: &rl, URL: urlStr})
|
||||||
|
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
time.Sleep(rl.RetryAfter)
|
time.Sleep(rl.RetryAfter)
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
|
|
|
@ -318,8 +318,9 @@ type Relationship struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// A RateLimit struct holds information related to a specific rate limit.
|
// A TooManyRequests struct holds information received from Discord
|
||||||
type RateLimit struct {
|
// when receiving a HTTP 429 response.
|
||||||
|
type TooManyRequests struct {
|
||||||
Bucket string `json:"bucket"`
|
Bucket string `json:"bucket"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
RetryAfter time.Duration `json:"retry_after"`
|
RetryAfter time.Duration `json:"retry_after"`
|
||||||
|
|
Loading…
Reference in a new issue