| 392 | } |
| 393 | |
| 394 | func (s *Server) checkRateLimit(ctx context.Context) (context.Context, error) { |
| 395 | // Any request type might be limited separately as it is part of Metadata |
| 396 | // Any request type might be excluded from this limit check and limited later, |
| 397 | // e.g. in the corresponding request handler by calling s.limiter.Limit(ctx, "limitKey", redis_rate.PerMinute(100)) |
| 398 | if auth.GetClaims(ctx, "").UserID == "" { |
| 399 | method, ok := grpc.Method(ctx) |
| 400 | if !ok { |
| 401 | return ctx, fmt.Errorf("server context does not have a method") |
| 402 | } |
| 403 | limitKey := ratelimit.AnonLimitKey(method, observability.GrpcPeer(ctx)) |
| 404 | if err := s.limiter.Limit(ctx, limitKey, ratelimit.Public); err != nil { |
| 405 | if errors.As(err, &ratelimit.QuotaExceededError{}) { |
| 406 | return ctx, status.Error(codes.ResourceExhausted, err.Error()) |
| 407 | } |
| 408 | return ctx, err |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | return ctx, nil |
| 413 | } |
| 414 | |
| 415 | func (s *Server) addInstanceRequestAttributes(ctx context.Context, instanceID string) { |
| 416 | attrs := s.runtime.GetInstanceAttributes(ctx, instanceID) |