From 3979365f03d012e617553d96bdf34247e14af8dc Mon Sep 17 00:00:00 2001 From: Martin Alge Date: Wed, 30 Oct 2024 09:53:03 +0100 Subject: [PATCH 1/2] Dynamically calculate the value of Retry-After header --- limiter.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/limiter.go b/limiter.go index d5ef436..34d0207 100644 --- a/limiter.go +++ b/limiter.go @@ -97,7 +97,9 @@ func (l *RateLimiter) OnLimit(w http.ResponseWriter, r *http.Request, key string setHeader(w, l.headers.Remaining, fmt.Sprintf("%d", limit-rate)) l.mu.Unlock() - setHeader(w, l.headers.RetryAfter, fmt.Sprintf("%d", int(l.windowLength.Seconds()))) // RFC 6585 + + retryAfter := int(math.Ceil(time.Now().UTC().Sub(currentWindow.Add(l.windowLength)).Seconds())) + setHeader(w, l.headers.RetryAfter, fmt.Sprintf("%d", retryAfter)) // RFC 6585 return true } From 492536b66938a8113525ae43700f651fa1438f84 Mon Sep 17 00:00:00 2001 From: Martin Alge Date: Wed, 30 Oct 2024 09:59:03 +0100 Subject: [PATCH 2/2] Change packet name to get import to work --- _example/main.go | 6 +++--- go.mod | 2 +- limiter_test.go | 2 +- local_counter_test.go | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/_example/main.go b/_example/main.go index 8f51510..6c55207 100644 --- a/_example/main.go +++ b/_example/main.go @@ -7,9 +7,9 @@ import ( "net/http" "time" - "github.com/go-chi/chi/v5" - "github.com/go-chi/chi/v5/middleware" - "github.com/go-chi/httprate" + "github.com/Alge/chi/v5" + "github.com/Alge/chi/v5/middleware" + "github.com/Alge/httprate" ) func main() { diff --git a/go.mod b/go.mod index 998cbf5..e71da6f 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/go-chi/httprate +module github.com/Alge/httprate go 1.17 diff --git a/limiter_test.go b/limiter_test.go index 5ac41c1..95db51e 100644 --- a/limiter_test.go +++ b/limiter_test.go @@ -12,7 +12,7 @@ import ( "testing" "time" - "github.com/go-chi/httprate" + "github.com/Alge/httprate" ) func TestLimit(t *testing.T) { diff --git a/local_counter_test.go b/local_counter_test.go index b3ee478..b94e8eb 100644 --- a/local_counter_test.go +++ b/local_counter_test.go @@ -7,7 +7,7 @@ import ( "testing" "time" - "github.com/go-chi/httprate" + "github.com/Alge/httprate" "golang.org/x/sync/errgroup" )