From 098d7861a417db4f467241d128be4c0d4863e672 Mon Sep 17 00:00:00 2001 From: Bruce Marriner Date: Thu, 28 Apr 2016 17:41:05 -0500 Subject: [PATCH] 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. --- events.go | 6 ++++++ restapi.go | 4 +++- structs.go | 5 +++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/events.go b/events.go index ce1f815..23aa6b9 100644 --- a/events.go +++ b/events.go @@ -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 diff --git a/restapi.go b/restapi.go index 99d3533..10f721f 100644 --- a/restapi.go +++ b/restapi.go @@ -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() diff --git a/structs.go b/structs.go index d9254bb..2721fb3 100644 --- a/structs.go +++ b/structs.go @@ -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"`