Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sagarvora committed Sep 13, 2023
1 parent 263c7b4 commit 8cf7905
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 18 deletions.
15 changes: 8 additions & 7 deletions frappe/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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", ".")

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -137,8 +140,6 @@ def application(request: Request):

log_request(request, response)
process_response(response)
if frappe.db:
frappe.db.close()

return response

Expand Down
6 changes: 0 additions & 6 deletions frappe/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion frappe/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions frappe/rate_limiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 8cf7905

Please sign in to comment.