Skip to content

Commit

Permalink
address review feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Gasch <mgasch@vmware.com>
  • Loading branch information
Michael Gasch committed Oct 25, 2021
1 parent 982247b commit 62d2dc0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
8 changes: 5 additions & 3 deletions v2/protocol/http/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,16 +283,18 @@ func (p *Protocol) Respond(ctx context.Context) (binding.Message, protocol.Respo
// ServeHTTP implements http.Handler.
// Blocks until ResponseFn is invoked.
func (p *Protocol) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// always apply limiter first
ok, reset, err := p.limiter.Take(context.TODO(), req)
// always apply limiter first using req context
ok, reset, err := p.limiter.Allow(req.Context(), req)
if err != nil {
p.incoming <- msgErr{msg: nil, err: fmt.Errorf("acquire rate limit token: %v", err)}
p.incoming <- msgErr{msg: nil, err: fmt.Errorf("unable to acquire rate limit token: %w", err)}
rw.WriteHeader(http.StatusInternalServerError)
return
}

if !ok {
rw.Header().Add("Retry-After", strconv.Itoa(int(reset)))
http.Error(rw, "limit exceeded", 429)
return
}

// Filter the GET style methods:
Expand Down
6 changes: 3 additions & 3 deletions v2/protocol/http/protocol_rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
)

type RateLimiter interface {
// Take attempts to take one token from the rate limiter for the specified
// Allow attempts to take one token from the rate limiter for the specified
// request. It returns ok when this operation was successful. In case ok is
// false, reset will indicate the time in seconds when it is safe to perform
// another attempt. An error is returned when this operation failed, e.g. due to
// a backend error.
Take(ctx context.Context, r *http.Request) (ok bool, reset uint64, err error)
Allow(ctx context.Context, r *http.Request) (ok bool, reset uint64, err error)
// Close terminates rate limiter and cleans up any data structures or
// connections that may remain open. After a store is stopped, Take() should
// always return zero values.
Expand All @@ -25,7 +25,7 @@ type RateLimiter interface {

type noOpLimiter struct{}

func (n noOpLimiter) Take(ctx context.Context, r *http.Request) (bool, uint64, error) {
func (n noOpLimiter) Allow(ctx context.Context, r *http.Request) (bool, uint64, error) {
return true, 0, nil
}

Expand Down
2 changes: 1 addition & 1 deletion v2/protocol/http/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func newRateLimiterTest(rps float64) RateLimiter {
return &rl
}

func (rl *rateLimiterTest) Take(_ context.Context, _ *http.Request) (bool, uint64, error) {
func (rl *rateLimiterTest) Allow(_ context.Context, _ *http.Request) (bool, uint64, error) {
if !rl.limiter.Allow() {
return false, 2, nil
}
Expand Down

0 comments on commit 62d2dc0

Please sign in to comment.