-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add rate limit tripperware and middleware (#33)
* add rate limit tripperware * use atomic uint64 * delete useless options * delete useless options * add rate limiter from golang.org/x/time/rate * move config * code style * update readme * refacto leaky_bucket rate limiter * remove useless test file * fix example and code style * remove vendor * remove bench * fix typo * fix port * fix example addr * improve and add middleware * improve * fix comments * Update README.md Co-authored-by: instabledesign <instabledesign@gmail.com> * update version * update version Co-authored-by: Anthony Moutte <instabledesign@gmail.com>
- Loading branch information
1 parent
14daf88
commit 071422a
Showing
12 changed files
with
496 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gol4ng/httpware/v3" | ||
"github.com/gol4ng/httpware/v3/rate_limit" | ||
) | ||
|
||
func RateLimit(limiter rate_limit.RateLimiter, options ...RateLimitOption) httpware.Middleware { | ||
config := NewRateLimitConfig(options...) | ||
|
||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(writer http.ResponseWriter, req *http.Request) { | ||
if err := limiter.Allow(req); err != nil { | ||
if !config.ErrorCallback(err, writer, req) { | ||
return | ||
} | ||
} | ||
|
||
limiter.Inc(req) | ||
defer limiter.Dec(req) | ||
next.ServeHTTP(writer, req) | ||
}) | ||
} | ||
} | ||
|
||
type RateLimitOption func(*RateLimitConfig) | ||
|
||
type RateLimitErrorCallback func(err error, writer http.ResponseWriter, req *http.Request) (next bool) | ||
|
||
type RateLimitConfig struct { | ||
ErrorCallback RateLimitErrorCallback | ||
} | ||
|
||
func (c *RateLimitConfig) apply(options ...RateLimitOption) *RateLimitConfig { | ||
for _, option := range options { | ||
option(c) | ||
} | ||
return c | ||
} | ||
|
||
func NewRateLimitConfig(options ...RateLimitOption) *RateLimitConfig { | ||
config := &RateLimitConfig{ | ||
ErrorCallback: DefaultRateLimitErrorCallback, | ||
} | ||
return config.apply(options...) | ||
} | ||
|
||
func DefaultRateLimitErrorCallback(err error, writer http.ResponseWriter, _ *http.Request) bool { | ||
http.Error(writer, err.Error(), http.StatusTooManyRequests) | ||
return false | ||
} | ||
|
||
func WithRateLimitErrorCallback(callback RateLimitErrorCallback) RateLimitOption { | ||
return func(config *RateLimitConfig) { | ||
config.ErrorCallback = callback | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package middleware_test | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/gol4ng/httpware/v3" | ||
"github.com/gol4ng/httpware/v3/middleware" | ||
"github.com/gol4ng/httpware/v3/mocks" | ||
"github.com/gol4ng/httpware/v3/rate_limit" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
func TestRateLimit(t *testing.T) { | ||
rateLimiterMock := &mocks.RateLimiter{} | ||
rateLimiterMock.On("Allow", mock.AnythingOfType("*http.Request")).Return(errors.New("failed")) | ||
|
||
req := httptest.NewRequest(http.MethodGet, "http://fake-addr", nil) | ||
responseWriter := httptest.NewRecorder() | ||
|
||
executed := false | ||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
executed = true | ||
}) | ||
|
||
middleware.RateLimit(rateLimiterMock)(handler).ServeHTTP(responseWriter, req) | ||
|
||
assert.False(t, executed) | ||
assert.Equal(t, http.StatusTooManyRequests, responseWriter.Result().StatusCode) | ||
|
||
content, err := ioutil.ReadAll(responseWriter.Result().Body) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "failed\n", string(content)) | ||
|
||
rateLimiterMock.AssertExpectations(t) | ||
} | ||
|
||
// ===================================================================================================================== | ||
// ========================================= EXAMPLES ================================================================== | ||
// ===================================================================================================================== | ||
|
||
func ExampleRateLimit() { | ||
limiter := rate_limit.NewTokenBucket(1*time.Second, 1) | ||
defer limiter.Stop() | ||
|
||
port := ":9105" | ||
// we recommend to use MiddlewareStack to simplify managing all wanted middlewares | ||
// caution middleware order matters | ||
stack := httpware.MiddlewareStack( | ||
middleware.RateLimit(limiter), | ||
) | ||
|
||
srv := http.NewServeMux() | ||
srv.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {}) | ||
go func() { | ||
if err := http.ListenAndServe(port, stack.DecorateHandler(srv)); err != nil { | ||
panic(err) | ||
} | ||
}() | ||
|
||
resp, _ := http.Get("http://localhost" + port) | ||
fmt.Println(resp.StatusCode) | ||
|
||
resp, _ = http.Get("http://localhost" + port) | ||
fmt.Println(resp.StatusCode) | ||
|
||
time.Sleep(2 * time.Second) | ||
resp, _ = http.Get("http://localhost" + port) | ||
fmt.Println(resp.StatusCode) | ||
// Output: | ||
//200 | ||
//429 | ||
//200 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package rate_limit | ||
|
||
const ( | ||
RequestLimitReachedErr = "request limit reached" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package rate_limit | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
type RateLimiter interface { | ||
Allow(req *http.Request) error | ||
Inc(req *http.Request) | ||
Dec(req *http.Request) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package rate_limit | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type TokenBucket struct { | ||
mutex sync.Mutex | ||
ticker *time.Ticker | ||
done chan struct{} | ||
callLimit uint32 | ||
count uint32 | ||
} | ||
|
||
func (t *TokenBucket) Allow(_ *http.Request) error { | ||
t.mutex.Lock() | ||
res := t.count >= t.callLimit | ||
t.mutex.Unlock() | ||
if res { | ||
return errors.New(RequestLimitReachedErr) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (t *TokenBucket) Inc(_ *http.Request) { | ||
t.mutex.Lock() | ||
t.count++ | ||
t.mutex.Unlock() | ||
} | ||
|
||
func (t *TokenBucket) Dec(_ *http.Request) {} | ||
|
||
func (t *TokenBucket) Stop() { | ||
t.done <- struct{}{} | ||
t.ticker.Stop() | ||
} | ||
|
||
func (t *TokenBucket) start() { | ||
go func() { | ||
for { | ||
select { | ||
case <-t.done: | ||
return | ||
case <-t.ticker.C: | ||
t.mutex.Lock() | ||
t.count = 0 | ||
t.mutex.Unlock() | ||
} | ||
} | ||
}() | ||
} | ||
|
||
func NewTokenBucket(timeBucket time.Duration, callLimit int) *TokenBucket { | ||
t := &TokenBucket{ | ||
ticker: time.NewTicker(timeBucket), | ||
done: make(chan struct{}), | ||
callLimit: uint32(callLimit), | ||
} | ||
|
||
t.start() | ||
|
||
return t | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package rate_limit_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/gol4ng/httpware/v3/rate_limit" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTokenBucket_Allow(t *testing.T) { | ||
limiter := rate_limit.NewTokenBucket(1 * time.Millisecond, 1) | ||
defer limiter.Stop() | ||
|
||
assert.NoError(t, limiter.Allow(nil)) | ||
limiter.Inc(nil) | ||
|
||
assert.EqualError(t, limiter.Allow(nil), "request limit reached") | ||
limiter.Inc(nil) | ||
|
||
time.Sleep(2 * time.Millisecond) | ||
assert.NoError(t, limiter.Allow(nil)) | ||
} |
Oops, something went wrong.