package ratelimit
import "github.com/grpc-ecosystem/go-grpc-middleware/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.
Simple example of server initialization code.
Code:play
Example¶
package main
import (
"github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/go-grpc-middleware/ratelimit"
"google.golang.org/grpc"
)
// alwaysPassLimiter is an example limiter which implements Limiter interface.
// It does not limit any request because Limit function always returns false.
type alwaysPassLimiter struct{}
func (*alwaysPassLimiter) Limit() bool {
return false
}
// Simple example of server initialization code.
func main() {
// Create unary/stream rateLimiters, based on token bucket here.
// You can implement your own ratelimiter for the interface.
limiter := &alwaysPassLimiter{}
_ = grpc.NewServer(
grpc_middleware.WithUnaryServerChain(
ratelimit.UnaryServerInterceptor(limiter),
),
grpc_middleware.WithStreamServerChain(
ratelimit.StreamServerInterceptor(limiter),
),
)
}
Index ¶
- func StreamServerInterceptor(limiter Limiter) grpc.StreamServerInterceptor
- func UnaryServerInterceptor(limiter Limiter) grpc.UnaryServerInterceptor
- type Limiter
Examples ¶
Functions ¶
func StreamServerInterceptor ¶
func StreamServerInterceptor(limiter Limiter) grpc.StreamServerInterceptor
StreamServerInterceptor returns a new stream server interceptor that performs rate limiting on the request.
func UnaryServerInterceptor ¶
func UnaryServerInterceptor(limiter Limiter) grpc.UnaryServerInterceptor
UnaryServerInterceptor returns a new unary server interceptors that performs request rate limiting.
Types ¶
type Limiter ¶
type Limiter interface { Limit() bool }
Limiter defines the interface to perform request rate limiting. If Limit function return true, the request will be rejected. Otherwise, the request will pass.
Source Files ¶
- Version
- v1.3.0
- Published
- Apr 22, 2021
- Platform
- js/wasm
- Imports
- 4 packages
- Last checked
- 1 hour ago –
Tools for package owners.