Fixed lint issues and missing documentation
This commit is contained in:
1 parent
7fd321df97
commit
6e6dec68c0
34 files changed
+346
-156
No files matched your search
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
// ---------- rate limiting ----------
|
||||
|
||||
// clientWindow is one client's rate limiting state.
|
||||
type clientWindow struct {
|
||||
hour time.Time // clock hour the anchor belongs to
|
||||
anchor time.Time // first request of that hour
|
||||
@@ -18,6 +19,10 @@ type clientWindow struct {
|
||||
count int // accepted requests in the current window
|
||||
}
|
||||
|
||||
// RateLimiter limits each client (by remote host) to a number of requests per
|
||||
// fixed window, answering the rest with 429 Too Many Requests and a
|
||||
// Retry-After header, as the Find a Tender API does. A client's windows are
|
||||
// counted from its first request in each clock hour.
|
||||
type RateLimiter struct {
|
||||
mu sync.Mutex
|
||||
limit int
|
||||
@@ -26,6 +31,7 @@ type RateLimiter struct {
|
||||
now func() time.Time
|
||||
}
|
||||
|
||||
// NewRateLimiter returns a RateLimiter allowing limit requests per window.
|
||||
func NewRateLimiter(limit int, window time.Duration) *RateLimiter {
|
||||
return &RateLimiter{
|
||||
limit: limit,
|
||||
@@ -108,7 +114,7 @@ func (rl *RateLimiter) Middleware(next http.HandlerFunc) http.HandlerFunc {
|
||||
w.Header().Set("Retry-After", strconv.Itoa(retry))
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
w.WriteHeader(http.StatusTooManyRequests)
|
||||
fmt.Fprintf(
|
||||
_, _ = fmt.Fprintf(
|
||||
w, "Rate limit of %d exceeded. Please retry after %d seconds.",
|
||||
rl.limit, retry,
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user