From 8cf7905e1900f05a0a78929cea139844380c329a Mon Sep 17 00:00:00 2001 From: Sagar Vora Date: Wed, 13 Sep 2023 19:41:09 +0530 Subject: [PATCH] wip --- frappe/app.py | 15 ++++++++------- frappe/hooks.py | 6 ------ frappe/monitor.py | 2 +- frappe/rate_limiter.py | 8 ++++---- 4 files changed, 13 insertions(+), 18 deletions(-) diff --git a/frappe/app.py b/frappe/app.py index cab52e0ff4ea..e2b42ab2a086 100644 --- a/frappe/app.py +++ b/frappe/app.py @@ -8,7 +8,6 @@ import re from werkzeug.exceptions import HTTPException, NotFound -from werkzeug.local import LocalManager from werkzeug.middleware.profiler import ProfilerMiddleware from werkzeug.middleware.shared_data import SharedDataMiddleware from werkzeug.wrappers import Request, Response @@ -29,8 +28,6 @@ from frappe.utils.error import log_error_snapshot from frappe.website.serve import get_response -local_manager = LocalManager(frappe.local) - _site = None _sites_path = os.environ.get("SITES_PATH", ".") @@ -73,7 +70,13 @@ def after_response_wrapper(app): def application(environ, start_response): return ClosingIterator( app(environ, start_response), - [frappe.request.after_response.run, local_manager.cleanup], + ( + frappe.rate_limiter.update, + frappe.monitor.stop, + frappe.recorder.dump, + frappe.request.after_response.run, + frappe.destroy, + ), ) return application @@ -126,7 +129,7 @@ def application(request: Request): # this function *must* always return a response, hence any exception thrown outside of # try..catch block like this finally block needs to be handled appropriately. - if request.method in UNSAFE_HTTP_METHODS and frappe.db and rollback: + if rollback and request.method in UNSAFE_HTTP_METHODS and frappe.db: frappe.db.rollback() try: @@ -137,8 +140,6 @@ def application(request: Request): log_request(request, response) process_response(response) - if frappe.db: - frappe.db.close() return response diff --git a/frappe/hooks.py b/frappe/hooks.py index f4de9e2fdfcb..28ba377793c6 100644 --- a/frappe/hooks.py +++ b/frappe/hooks.py @@ -419,12 +419,6 @@ "frappe.rate_limiter.apply", ] -after_request = [ - "frappe.rate_limiter.update", - "frappe.monitor.stop", - "frappe.recorder.dump", -] - # Background Job Hooks before_job = [ "frappe.monitor.start", diff --git a/frappe/monitor.py b/frappe/monitor.py index b7c788cb9eea..9b8f50035828 100644 --- a/frappe/monitor.py +++ b/frappe/monitor.py @@ -22,7 +22,7 @@ def start(transaction_type="request", method=None, kwargs=None): def stop(response=None): if hasattr(frappe.local, "monitor"): - frappe.request.after_response(lambda: frappe.local.monitor.dump(response)) + frappe.local.monitor.dump(response) def add_data_to_monitor(**kwargs) -> None: diff --git a/frappe/rate_limiter.py b/frappe/rate_limiter.py index 9553407c15a7..d56415639133 100644 --- a/frappe/rate_limiter.py +++ b/frappe/rate_limiter.py @@ -21,7 +21,7 @@ def apply(): def update(): if hasattr(frappe.local, "rate_limiter"): - frappe.request.after_response(frappe.local.rate_limiter.update) + frappe.local.rate_limiter.update() def respond(): @@ -56,15 +56,15 @@ def reject(self): raise frappe.TooManyRequestsError def update(self): - self.end = datetime.utcnow() - self.duration = int((self.end - self.start).total_seconds() * 1000000) - pipeline = frappe.cache.pipeline() pipeline.incrby(self.key, self.duration) pipeline.expire(self.key, self.window) pipeline.execute() def headers(self): + self.end = datetime.utcnow() + self.duration = int((self.end - self.start).total_seconds() * 1000000) + headers = { "X-RateLimit-Reset": self.reset, "X-RateLimit-Limit": self.limit,