feat(openapi): implement ResetInterface in OpenApiFactory#7922
feat(openapi): implement ResetInterface in OpenApiFactory#7922KevinMartinsDev wants to merge 2 commits intoapi-platform:mainfrom
Conversation
|
Hi, thanks for your work on this topic! This is much appreciated and the linter already proved very useful. However, I wonder if there is a real problem here. Routes and the OpenAPI spec cannot/shouldn't change between requests and users, so the current implement will be more performant. This matters because it's an endpoint that is called a lot of times in some scenarios (client dynamically calling the OpenAPI route). |
|
Thank you for your feedback! 😃 I completely understand the performance concern regarding the RouteCollection. Maintaining a fast response time for the OpenAPI endpoint is indeed important. However, I see two main reasons to consider a change here: Besides the routes, OpenApiFactory also maintains a $localErrorResourceCache. This array grows every time a unique error is encountered. In a long-running worker process, this leads to an unbounded memory leak. Even if we decide to keep the routes, we should at least clear this error cache. Would you like me to update this PR to follow one of these paths? Option A: Keep the ResetInterface but only reset the $localErrorResourceCache, leaving the routeCollection as is for performance. Option B: Transition the internal caching to a proper PSR-16 (Simple Cache) implementation. This would provide the same performance benefits while keeping the service stateless and making the cache shared/clearable via standard tools. Also, relying on private properties means the cache is isolated per worker. In a multi-worker setup (like FrankenPHP), this leads to redundant memory usage and potential state drift between processes. Using a PSR-16 implementation (e.g., via APCu) would allow workers to share the cache, significantly reducing the total memory footprint and ensuring consistency across the pool. What do you think? 🤔 |
Description
This PR is the fourth part of the worker mode compatibility audit (see #7918). It implements
ResetInterfaceinOpenApiFactoryto handle internal state persistence.The Issue
OpenApiFactoryis a singleton service that maintains two types of internal state:RouteCollectionis stored in a private property.$localErrorResourceCache) that stores resolved error resources to avoid redundant processing.In persistent memory environments like FrankenPHP:
RouteCollectionuntil the worker restarts.The Solution
By implementing
Symfony\Contracts\Service\ResetInterface, we ensure that:$routeCollectionis reset tonull.$localErrorResourceCacheis cleared (reset to[]).This happens automatically after each request in worker mode, ensuring that the next request starts with fresh metadata and a clean memory footprint.