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
π Contributor Guidance
- Add
bucket4j-core and caffeine to spector-synapse/pom.xml
- Implement
RateLimitFilter extending OncePerRequestFilter
- Create buckets:
Bucket.builder().addLimit(Bandwidth.simple(limit, Duration.ofSeconds(1))).build()
- Store buckets in
CacheManager keyed by API key or client IP
- Register in the Spring Security filter chain (before authentication)
- 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 |
Summary
Implement rate limiting using Bucket4j to protect Synapse endpoints from abuse. Configurable per-API-key rate limit with token bucket algorithm.
ποΈ Technical Specifications
Bucket4jwithbucket4j-spring-boot-starter+ Caffeine cache backendspector-synapseβcom.spectrayan.spector.synapse.securityRateLimitFilterβ Spring Security filter that checks request rate before processingBucket4jtoken bucket algorithm with configurable refill rateCaffeinecache for in-memory bucket storage (keyed by API key or IP)429 Too Many RequestswithRetry-Afterheader (seconds until next available token)/actuator/**,/healthendpoints are never rate-limitedcom.bucket4j:bucket4j-core,com.github.ben-manes.caffeine:caffeineβ Acceptance Criteria
429 Too Many RequestswithRetry-Afterheaderspector.security.rate-limit.requests-per-secondis configurable, when set to 200, then the limit is 200 req/sπ Contributor Guidance
bucket4j-coreandcaffeinetospector-synapse/pom.xmlRateLimitFilterextendingOncePerRequestFilterBucket.builder().addLimit(Bandwidth.simple(limit, Duration.ofSeconds(1))).build()CacheManagerkeyed by API key or client IPmvn clean compile -pl spector-synapseπ References
RICE-C Score