Browse Source

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.
Bruce Marriner 9 years ago
parent
commit
098d7861a4
3 changed files with 12 additions and 3 deletions
  1. 6 0
      events.go
  2. 3 1
      restapi.go
  3. 3 2
      structs.go

+ 6 - 0
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

+ 3 - 1
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()

+ 3 - 2
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"`