grpclimit

package
v0.0.0-...-fc4a167 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 18, 2023 License: BSD-3-Clause Imports: 8 Imported by: 0

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

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

func Serve(server *grpc.Server, addr string, connectionLimit int) error

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

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL