Skip to content

Refactor middleware architecture and implement native rate limiting#3

Merged
ChernegaSergiy merged 27 commits intomainfrom
refactor/middleware-architecture
Feb 28, 2026
Merged

Refactor middleware architecture and implement native rate limiting#3
ChernegaSergiy merged 27 commits intomainfrom
refactor/middleware-architecture

Conversation

@ChernegaSergiy
Copy link
Member

This pull request introduces a major architectural overhaul of the middleware pipeline to improve flow control, centralize caching strategies, and provide a robust rate limiting implementation.

Major Changes

  • Middleware Pipeline Restructuring:
    • Standardized the execution order to ensure predictable data flow.
    • Moved StaleOnErrorMiddleware to the correct position after lookup and lock to ensure reliable access to stale data during source failures.
  • New Strategy Management:
    • Introduced StrategyMiddleware as the single source of truth for Strict, Background (Stale-While-Revalidate), and cache hit decisions.
    • Removed scattered strategy logic from CacheLookupMiddleware and AsyncLockMiddleware to adhere to the Single Responsibility Principle.
  • Native Rate Limiting:
    • Implemented RateLimitMiddleware integrated with the Symfony Rate Limiter component.
    • Switched to RateLimiterFactoryInterface to support dynamic, key-based rate limiting buckets.
  • Locking Optimization:
    • Enhanced AsyncLockMiddleware with a "Double-Check" mechanism that verifies cache state after acquiring a lock, preventing redundant source fetches if another process completed the work while waiting.

Breaking Changes

  • Rate Limiter Configuration: AsyncCacheConfigBuilder::withRateLimiter() now requires RateLimiterFactoryInterface instead of a single LimiterInterface.
  • API Cleanup: Removed the obsolete clearRateLimiter() method. Added resetRateLimit(string $key) for granular control over specific rate limit buckets.

Additional Improvements

  • Standardized Logging: Unified internal logging format with the AsyncCache prefix for better observability and debugging of async flows.
  • Enhanced Documentation: Updated the middleware pipeline guides, rate limiting integration examples, and API reference to reflect the new architecture.
  • Full Test Coverage: Added comprehensive unit tests for new middlewares and a pipeline compliance example verifying all 8 major execution scenarios.

@ChernegaSergiy ChernegaSergiy merged commit f50642e into main Feb 28, 2026
6 checks passed
@ChernegaSergiy ChernegaSergiy deleted the refactor/middleware-architecture branch February 28, 2026 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant