Skip to content

Commit

Permalink
BREAKING - Added RateLimited event
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bwmarrin committed Apr 28, 2016
1 parent 9477063 commit 098d786
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
6 changes: 6 additions & 0 deletions events.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ type Connect struct{}
// Disconnect is an empty struct for an event.
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.
type MessageCreate struct {
*Message
Expand Down
4 changes: 3 additions & 1 deletion restapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,15 @@ func (s *Session) request(method, urlStr, contentType string, b []byte) (respons

case 429: // TOO MANY REQUESTS - Rate limiting

rl := RateLimit{}
rl := TooManyRequests{}
err = json.Unmarshal(response, &rl)
if err != nil {
s.log(LogError, "rate limit unmarshal error, %s", err)
return
}
s.log(LogInformational, "Rate Limiting %s, retry in %d", urlStr, rl.RetryAfter)
s.handle(RateLimited{TooManyRequests: &rl, URL: urlStr})

mu.Lock()
time.Sleep(rl.RetryAfter)
mu.Unlock()
Expand Down
5 changes: 3 additions & 2 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,9 @@ type Relationship struct {
ID string `json:"id"`
}

// A RateLimit struct holds information related to a specific rate limit.
type RateLimit struct {
// A TooManyRequests struct holds information received from Discord
// when receiving a HTTP 429 response.
type TooManyRequests struct {
Bucket string `json:"bucket"`
Message string `json:"message"`
RetryAfter time.Duration `json:"retry_after"`
Expand Down

0 comments on commit 098d786

Please sign in to comment.