Rate Limiting
Adaptive Rate Limiting Usage Example
Adaptive rate limiting determines whether to apply rate limiting based on a combination of default thresholds and system resource usage. Since the processing capacity varies among different servers, manually setting parameters can be difficult when there are many servers. Adaptive rate limiting based on server processing capacity avoids the hassle of manually configuring parameters for each service.
Click to view Adaptive Rate Limiting Usage Example.
Configuration Guide
Basic Configuration
In services created by sponge, the rate limiting component is disabled by default.
Set the enableLimit
field in the YAML file under the configs
directory:
app:
enableLimit: false # Whether to enable adaptive rate limiting, true: enable, false: disable
Web Service Configuration
Although there are default thresholds and system resource quotas, you can modify these default values.
For web
services, modify the default values in RateLimit in internal/routers/routers.go
, example:
// limit middleware
if config.Get().App.EnableLimit {
r.Use(middleware.RateLimit(
middleware.WithWindow(time.Second*5),
middleware.WithBucket(200),
middleware.WithCPUThreshold(600),
middleware.WithCPUQuota(0),
))
}
gRPC Service Configuration
For grpc
services, modify the default values in UnaryServerRateLimit in internal/server/grpc.go
, example:
// limit interceptor
if config.Get().App.EnableLimit {
unaryServerInterceptors = append(unaryServerInterceptors, interceptor.UnaryServerRateLimit(
interceptor.WithWindow(time.Second*5),
interceptor.WithBucket(200),
interceptor.WithCPUThreshold(600),
interceptor.WithCPUQuota(0),
))
}