Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Refactor: Rate-limiting middleware plus scaffolding #1563

Merged
merged 3 commits into from
May 19, 2020

Conversation

azdagron
Copy link
Member

@azdagron azdagron commented May 18, 2020

  • Middleware interface provides pre and post processing steps
  • Provides interceptor implemementations for gRPC
  • Implements logger middleware
  • Implements per-rpc and per-caller rate limiting

** Update ** This PR does not handle periodic pruning of the per-ip limiter map. It also doesn't have direct unit-tests for the middleware chaining, etc. These will come in follow up PRs.

- Middleware interface provides pre and post processing steps
- Provides interceptor implemementations for gRPC
- Implements logger middleware
- Implements per-rpc and per-caller rate limiting

Signed-off-by: Andrew Harding <andrew.harding@hpe.com>
Signed-off-by: Andrew Harding <andrew.harding@hpe.com>
Copy link
Member

@amartinezfayo amartinezfayo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good!
Adding more test coverage is intended to be addressed separately, right?

)

var (
// NewRateLimiter is used to create a new ratelimiter. It returns a limiter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// NewRateLimiter is used to create a new ratelimiter. It returns a limiter
// newRawRateLimiter is used to create a new ratelimiter. It returns a limiter

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Burst() int
}

// NoLimit returns a rate limiter does not rate limit. It is used to configure
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// NoLimit returns a rate limiter does not rate limit. It is used to configure
// NoLimit returns a rate limiter that does not rate limit. It is used to configure

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

// WithRateLimits returns a middleware that performs rate limiting for the
// group of methods descripted by the rateLimits map. It provides the
// configured rate limiter to the method handlers via the request context. If
// the middleware is invoked for a method is not described in the map, it will
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// the middleware is invoked for a method is not described in the map, it will
// the middleware is invoked for a method that is not described in the map, it will

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

lim.mtx.RUnlock()
return limiter
}
lim.mtx.RUnlock()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not call defer lim.mtx.Unlock() right after lim.mtx.RLock()? at the beginning?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would cause a deadlock below where this function takes the write lock to create a new limiter under this address.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it.

}

type rateLimitsMiddleware struct {
limiters map[string]api.RateLimiter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be accessed concurrently? should we have a mutex?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't be mutated after passing it into rate limits, so concurrent access should be safe. I'll document the intended ownership on the WithRateLimits call.

@azdagron
Copy link
Member Author

Yep, there is some unit-test coverage around middleware and stuff that is required. I'll have those in follow up PRs.

This also doesn't cover pruning the per-ip limit map. I've added a TODO and will implement that as well.

Signed-off-by: Andrew Harding <andrew.harding@hpe.com>
Copy link
Member

@amartinezfayo amartinezfayo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@azdagron azdagron merged commit e05327d into spiffe:master May 19, 2020
@azdagron azdagron deleted the rate-limiting-middleware branch May 19, 2020 01:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants