mirror of https://github.com/zulip/zulip.git
Revert "Revert "request: Refactor to record rate limit data using ZulipRequestNotes.""
This reverts commit 49eab4efef
.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
parent
91282ab490
commit
7c32134fb5
|
@ -42,11 +42,12 @@ class RateLimitedObject(ABC):
|
||||||
)
|
)
|
||||||
|
|
||||||
def rate_limit_request(self, request: HttpRequest) -> None:
|
def rate_limit_request(self, request: HttpRequest) -> None:
|
||||||
ratelimited, time = self.rate_limit()
|
from zerver.lib.request import get_request_notes
|
||||||
|
|
||||||
if not hasattr(request, "_ratelimits_applied"):
|
ratelimited, time = self.rate_limit()
|
||||||
request._ratelimits_applied = []
|
request_notes = get_request_notes(request)
|
||||||
request._ratelimits_applied.append(
|
|
||||||
|
request_notes.ratelimits_applied.append(
|
||||||
RateLimitResult(
|
RateLimitResult(
|
||||||
entity=self,
|
entity=self,
|
||||||
secs_to_freedom=time,
|
secs_to_freedom=time,
|
||||||
|
@ -61,8 +62,8 @@ class RateLimitedObject(ABC):
|
||||||
|
|
||||||
calls_remaining, seconds_until_reset = self.api_calls_left()
|
calls_remaining, seconds_until_reset = self.api_calls_left()
|
||||||
|
|
||||||
request._ratelimits_applied[-1].remaining = calls_remaining
|
request_notes.ratelimits_applied[-1].remaining = calls_remaining
|
||||||
request._ratelimits_applied[-1].secs_to_freedom = seconds_until_reset
|
request_notes.ratelimits_applied[-1].secs_to_freedom = seconds_until_reset
|
||||||
|
|
||||||
def block_access(self, seconds: int) -> None:
|
def block_access(self, seconds: int) -> None:
|
||||||
"Manually blocks an entity for the desired number of seconds"
|
"Manually blocks an entity for the desired number of seconds"
|
||||||
|
|
|
@ -540,8 +540,9 @@ class RateLimitMiddleware(MiddlewareMixin):
|
||||||
return response
|
return response
|
||||||
|
|
||||||
# Add X-RateLimit-*** headers
|
# Add X-RateLimit-*** headers
|
||||||
if hasattr(request, "_ratelimits_applied"):
|
ratelimits_applied = get_request_notes(request).ratelimits_applied
|
||||||
self.set_response_headers(response, request._ratelimits_applied)
|
if len(ratelimits_applied) > 0:
|
||||||
|
self.set_response_headers(response, ratelimits_applied)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
|
@ -238,10 +238,10 @@ class AsyncDjangoHandler(tornado.web.RequestHandler, base.BaseHandler):
|
||||||
# Add to this new HttpRequest logging data from the processing of
|
# Add to this new HttpRequest logging data from the processing of
|
||||||
# the original request; we will need these for logging.
|
# the original request; we will need these for logging.
|
||||||
request_notes.log_data = old_request_notes.log_data
|
request_notes.log_data = old_request_notes.log_data
|
||||||
|
if request_notes.rate_limit is not None:
|
||||||
|
request_notes.rate_limit = old_request_notes.rate_limit
|
||||||
if request_notes.requestor_for_logs is not None:
|
if request_notes.requestor_for_logs is not None:
|
||||||
request_notes.requestor_for_logs = old_request_notes.requestor_for_logs
|
request_notes.requestor_for_logs = old_request_notes.requestor_for_logs
|
||||||
if hasattr(request, "_rate_limit"):
|
|
||||||
request._rate_limit = old_request._rate_limit
|
|
||||||
request.user = old_request.user
|
request.user = old_request.user
|
||||||
request_notes.client = old_request_notes.client
|
request_notes.client = old_request_notes.client
|
||||||
request_notes.client_name = old_request_notes.client_name
|
request_notes.client_name = old_request_notes.client_name
|
||||||
|
|
|
@ -260,12 +260,11 @@ def rate_limit_authentication_by_username(request: HttpRequest, username: str) -
|
||||||
|
|
||||||
|
|
||||||
def auth_rate_limiting_already_applied(request: HttpRequest) -> bool:
|
def auth_rate_limiting_already_applied(request: HttpRequest) -> bool:
|
||||||
if not hasattr(request, "_ratelimits_applied"):
|
request_notes = get_request_notes(request)
|
||||||
return False
|
|
||||||
|
|
||||||
return any(
|
return any(
|
||||||
isinstance(r.entity, RateLimitedAuthenticationByUsername)
|
isinstance(r.entity, RateLimitedAuthenticationByUsername)
|
||||||
for r in request._ratelimits_applied
|
for r in request_notes.ratelimits_applied
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue