Rate limit errors, prevent requests if above limit

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian
2020-09-28 22:13:50 -04:00
parent 28b59f02c7
commit 6f79da6e35
3 changed files with 170 additions and 2 deletions
+31 -1
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"strings"
"time"
)
// APIError is an error coming from Reddit.
@@ -72,4 +73,33 @@ func (r *ErrorResponse) Error() string {
)
}
// todo: rate limit errors
// RateLimitError occurs when the client is sending too many requests to Reddit in a given time frame.
type RateLimitError struct {
// Rate specifies the last known rate limit for the client
Rate Rate
// HTTP response that caused this error
Response *http.Response
// Error message
Message string
}
func (e *RateLimitError) Error() string {
return fmt.Sprintf(
"%s %s: %d %s %s",
e.Response.Request.Method, e.Response.Request.URL, e.Response.StatusCode, e.Message, e.formateRateReset(),
)
}
func (e *RateLimitError) formateRateReset() string {
d := time.Until(e.Rate.Reset).Round(time.Second)
isNegative := d < 0
if isNegative {
d *= -1
}
if isNegative {
return fmt.Sprintf("[rate limit was reset %s ago]", d)
}
return fmt.Sprintf("[rate limit will reset in %s]", d)
}