-
Notifications
You must be signed in to change notification settings - Fork 0
Split logging features of fastapi_log #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 16.0-add-fastapi_log
Are you sure you want to change the base?
Split logging features of fastapi_log #8
Conversation
Each time a fastapi app is created, a new event loop thread is created by the ASGIMiddleware. Unfortunately, every time the cache is cleared, a new app is created with a new even loop thread. This leads to an increase in the number of threads created to manage the asyncio event loop, even though many of them are no longer in use. To avoid this problem, the thread in charge of the event loop is now created only once per thread / process and the result is stored in the thread's local storage. If a new instance of an app needs to be created following a cache reset, this ensures that the same event loop is reused. refs OCA#484
This commit adds event loop lifecycle management to the FastAPI dispatcher. Before this commit, an event loop and the thread to run it were created each time a FastAPI app was created. The drawback of this approach is that when the app was destroyed (for example, when the cache of app was cleared), the event loop and the thread were not properly stopped, which could lead to memory leaks and zombie threads. This commit fixes this issue by creating a pool of event loops and threads that are shared among all FastAPI apps. On each call to a FastAPI app, a event loop is requested from the pool and is returned to the pool when the app is destroyed. At request time of an event loop, the pool try to reuse an existing event loop and if no event loop is available, a new event loop is created. The cache of the FastAPI app is also refactored to use it's own mechanism. It's now based on a dictionary of queues by root path by database, where each queue is a pool of FastAPI app. This allows a better management of the invalidation of the cache. It's now possible to invalidate the cache of FastAPI app by root path without affecting the cache of others root paths.
On server shutdown, ensure that created the event loops are closed properly.
defaultdict in python is not thread safe. Since this data structure is used to store the cache of FastAPI apps, we must ensure that the access to this cache is thread safe. This is done by using a lock to protect the access to the cache.
This commit improves the lifecycle of the fastapi app cache. It first ensures that the cache is effectively invalidated when changes are made to the app configuration even if theses changes occur into an other server instance. It also remove the use of a locking mechanism put in place to ensure a thread safe access to a value into the cache to avoid potential concurrency issue when a default value is set to the cache at access time. This lock could lead to unnecessary contention and reduce the performance benefits of queue.Queue's fine-grained internal synchronization for a questionable gain. The only expected gain was to avoid the useless creation of a queue.Queue instance that would never be used since at the time of puting the value into the cache we are sure that a value is already present into the dictionary.
Before this change, an eventloop thread was created on each instanciation of the ASGIMiddleware class. We want to avoid this behavior to avoid the creation of zombie threads
Add a new test to ensure that if a retryable error occurs when processing a call to a base_rest endpoint, the retrying mechanism of odoo works as expected
In case of a retryable error, the initial error must bubble up to the retrying mechanism. If this kind of error is wrapped into another one, the retrying mechanism no more works
Hi @paradoxxxzero, |
Currently translated at 100.0% (3 of 3 strings) Translation: rest-framework-16.0/rest-framework-16.0-fastapi_auth_jwt_demo Translate-URL: https://translation.odoo-community.org/projects/rest-framework-16-0/rest-framework-16-0-fastapi_auth_jwt_demo/it/
Currently translated at 100.0% (43 of 43 strings) Translation: rest-framework-16.0/rest-framework-16.0-fastapi Translate-URL: https://translation.odoo-community.org/projects/rest-framework-16-0/rest-framework-16-0-fastapi/it/
1e08ade
to
ff9d6af
Compare
ff9d6af
to
1e08ade
Compare
Regarding the multi-slash PR maybe we should wait to include it in its entirety until we receive some feedback (and I implement some tests). I could extract the What do you think? |
Yes that's a good conservative solution. Then OCA#515 will build on the new PR to edit the I will then include the new refactoring PR instead of OCA#515. |
Currently translated at 100.0% (90 of 90 strings) Translation: rest-framework-16.0/rest-framework-16.0-auth_partner Translate-URL: https://translation.odoo-community.org/projects/rest-framework-16-0/rest-framework-16-0-auth_partner/it/
05fb39b
to
cfdb0e3
Compare
cfdb0e3
to
f88ff23
Compare
f88ff23
to
29c911a
Compare
This way other APIs might use the new module `api_log` to store logs.
29c911a
to
b5429b3
Compare
b5429b3
to
a1c02fa
Compare
There are several conflicts because https://github.com/akretion/rest-framework/tree/16.0-add-fastapi_log is not updated with https://github.com/OCA/rest-framework/tree/16.0, maybe it's time to move this PR to OCA? |
As suggested in
Originally posted by @simahawk in OCA#501 (comment)
Also add the module
fastapi_log_mail
to optionally send an email when an exception occurs.Depends on: