Documentation ¶
Overview ¶
Package grpclimit limits the number of concurrent requests and concurrent connections to a gRPC server to ensure that it does not run out of memory during overload scenarios.
Index ¶
- func NewServer(addr string, requestLimit int, options ...grpc.ServerOption) (*grpc.Server, error)
- func NewServerWithInterceptors(addr string, requestLimit int, unaryInterceptor grpc.UnaryServerInterceptor, ...) (*grpc.Server, error)
- func Serve(server *grpc.Server, addr string, connectionLimit int) error
- func UnaryInterceptor(limiter concurrentlimit.Limiter, next grpc.UnaryServerInterceptor) grpc.UnaryServerInterceptor
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewServer ¶
func NewServer( addr string, requestLimit int, options ...grpc.ServerOption, ) (*grpc.Server, error)
NewServer creates a grpc.Server and net.Listener that supports a limited number of concurrent requests. It sets the MaxConcurrentStreams option to the same value, which will cause requests to block on the client if a single client sends too many requests. You should also use Serve() with this server to protect against too many idle connections.
NOTE: options must not contain any interceptors, since this function relies on adding our own interceptor to limit the requests. Use NewServerWithInterceptors if you need interceptors.
TODO: Implement stream interceptors
func NewServerWithInterceptors ¶
func NewServerWithInterceptors( addr string, requestLimit int, unaryInterceptor grpc.UnaryServerInterceptor, options ...grpc.ServerOption, ) (*grpc.Server, error)
NewServerWithInterceptors is a version of NewServer that permits customizing the interceptors. The passed in interceptor will be called after the operation limiter permits the request. See NewServer's documentation for the remaining details.
func Serve ¶
Serve listens on addr but only accepts a maximum of connectionLimit conenctions at one time to limit memory usage. New connections will block in the kernel. This returns when grpc.Server.Serve would normally return.
func UnaryInterceptor ¶
func UnaryInterceptor(limiter concurrentlimit.Limiter, next grpc.UnaryServerInterceptor) grpc.UnaryServerInterceptor
UnaryInterceptor returns a grpc.UnaryServerInterceptor that uses limiter to limit the concurrent requests. It will return codes.ResourceExhausted if the limiter rejects an operation. If next is not nil, it will be called to chain the request handlers. If it is nil, this will invoke the operation directly.
Types ¶
This section is empty.