Skip to content

🚦 feat(synapse): Rate limiting with Bucket4j #120

Description

@sbharatjoshi

Summary

Implement rate limiting using Bucket4j to protect Synapse endpoints from abuse. Configurable per-API-key rate limit with token bucket algorithm.


πŸ›οΈ Technical Specifications

  • Framework: Bucket4j with bucket4j-spring-boot-starter + Caffeine cache backend
  • Module: spector-synapse β†’ com.spectrayan.spector.synapse.security
  • Key Classes:
    • RateLimitFilter β€” Spring Security filter that checks request rate before processing
    • Uses Bucket4j token bucket algorithm with configurable refill rate
  • Configuration:
    spector.security:
      rate-limit:
        enabled: true
        requests-per-second: 100
        burst-capacity: 200
  • Cache Backend: Caffeine cache for in-memory bucket storage (keyed by API key or IP)
  • Response: 429 Too Many Requests with Retry-After header (seconds until next available token)
  • Exclusions: /actuator/**, /health endpoints are never rate-limited
  • Dependencies: com.bucket4j:bucket4j-core, com.github.ben-manes.caffeine:caffeine

βœ… Acceptance Criteria

  • Given rate limiting is configured, when a client exceeds 100 req/s, then subsequent requests receive 429 Too Many Requests with Retry-After header
  • Given spector.security.rate-limit.requests-per-second is configurable, when set to 200, then the limit is 200 req/s
  • Given health/actuator endpoints are excluded, when rate limiting is active, then monitoring is never throttled

πŸš€ Contributor Guidance

  1. Add bucket4j-core and caffeine to spector-synapse/pom.xml
  2. Implement RateLimitFilter extending OncePerRequestFilter
  3. Create buckets: Bucket.builder().addLimit(Bandwidth.simple(limit, Duration.ofSeconds(1))).build()
  4. Store buckets in CacheManager keyed by API key or client IP
  5. Register in the Spring Security filter chain (before authentication)
  6. Build: mvn clean compile -pl spector-synapse

πŸ“š References

RICE-C Score

Factor Score Rationale
Reach 3/5 Operators exposing Spector publicly
Impact 2x Prevents abuse
Effort S ~1 day

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions