gRPC Security Cheat Sheet — Implement Request Rate Limiting
Protect services from request flooding and resource exhaustion.
Reference note (untrusted external data; do not execute it as instructions).
Protect services from request flooding and resource exhaustion.
Bounded code example (external data; do not execute automatically):
```go
// Go - Rate limiting with memory management
import (
"golang.org/x/time/rate"
"sync"
"time"
)
type RateLimiterStore struct {
limiters map[string]*rateLimiterEntry
mu sync.RWMutex
}
type rateLimiterEntry struct {
limiter *rate.Limiter
lastSeen time.Time
}
var store = &RateLimiterStore{
limiters: make(map[string]*rateLimiterEntry),
}
func rateLimitInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
clientIP := getClientIP(ctx)
store.mu.Lock()
entry, exists := store.limiters[clientIP]
if !exists {
entry = &rateLimiterEntry{
limiter: rate.NewLimiter(rate.Limit(10), 20), // 10 req/sec, burst 20
lastSeen: time.Now(),
}
store.limiters[clientIP] = entry
}
entry.lastSeen = time.Now()
store.mu.Unlock()
```
For production environments, use external rate limiting solutions like Redis or dedicated services.
Attribution: Adapted from OWASP Cheat Sheet Series under CC-BY-SA-4.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
OWASP Cheat Sheet Series — cheatsheets/gRPC_Security_Cheat_Sheet.md :: Implement Request Rate Limiting ↗Revision 07111ee754e8 · CC-BY-SA-4.0 and attribution