...
Package ratelimit
`ratelimit` a generic server-side ratelimit middleware for gRPC.
Server Side Ratelimit Middleware
It allows to do grpc rate limit by your own rate limiter (e.g. token bucket, leaky bucket, etc.)
Please see examples for simple examples of use.
▾ Example
Simple example of server initialization code.
Code:
package ratelimit_test
import (
"github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/go-grpc-middleware/ratelimit"
"google.golang.org/grpc"
)
type alwaysPassLimiter struct{}
func (*alwaysPassLimiter) Limit() bool {
return false
}
func Example() {
limiter := &alwaysPassLimiter{}
_ = grpc.NewServer(
grpc_middleware.WithUnaryServerChain(
ratelimit.UnaryServerInterceptor(limiter),
),
grpc_middleware.WithStreamServerChain(
ratelimit.StreamServerInterceptor(limiter),
),
)
}
func StreamServerInterceptor(limiter Limiter) grpc.StreamServerInterceptor
StreamServerInterceptor returns a new stream server interceptor that performs rate limiting on the request.
func UnaryServerInterceptor(limiter Limiter) grpc.UnaryServerInterceptor
UnaryServerInterceptor returns a new unary server interceptors that performs request rate limiting.
Limiter defines the interface to perform request rate limiting.
If Limit function return true, the request will be rejected.
Otherwise, the request will pass.
type Limiter interface {
Limit() bool
}