def rate_limit_user(request: HttpRequest, user: UserProfile, domain: str) -> None: """Returns whether or not a user was rate limited. Will raise a RateLimited exception if the user has been rate limited, otherwise returns and modifies request to contain the rate limit information""" entity = RateLimitedUser(user, domain=domain) ratelimited, time = is_ratelimited(entity) request._ratelimit_applied_limits = True request._ratelimit_secs_to_freedom = time request._ratelimit_over_limit = ratelimited # Abort this request if the user is over their rate limits if ratelimited: statsd.incr("ratelimiter.limited.%s.%s" % (type(user), user.id)) raise RateLimited() try: incr_ratelimit(entity) except RateLimiterLockingException: logging.warning("Deadlock trying to incr_ratelimit for %s on %s" % ( user.id, request.path)) # rate-limit users who are hitting the API so hard we can't update our stats. raise RateLimited() calls_remaining, time_reset = api_calls_left(entity) request._ratelimit_remaining = calls_remaining request._ratelimit_secs_to_freedom = time_reset
def rate_limit_user(request: HttpRequest, user: UserProfile, domain: str) -> None: """Returns whether or not a user was rate limited. Will raise a RateLimited exception if the user has been rate limited, otherwise returns and modifies request to contain the rate limit information""" entity = RateLimitedUser(user, domain=domain) ratelimited, time = is_ratelimited(entity) request._ratelimit_applied_limits = True request._ratelimit_secs_to_freedom = time request._ratelimit_over_limit = ratelimited # Abort this request if the user is over their rate limits if ratelimited: statsd.incr("ratelimiter.limited.%s.%s" % (type(user), user.id)) raise RateLimited() try: incr_ratelimit(entity) except RateLimiterLockingException: # nocoverage # Should add on next rate limit pass logging.warning("Deadlock trying to incr_ratelimit for %s on %s" % ( user.id, request.path)) # rate-limit users who are hitting the API so hard we can't update our stats. raise RateLimited() calls_remaining, time_reset = api_calls_left(entity) request._ratelimit_remaining = calls_remaining request._ratelimit_secs_to_freedom = time_reset
def rate_limit_user(request, user, domain): """Returns whether or not a user was rate limited. Will raise a RateLimited exception if the user has been rate limited, otherwise returns and modifies request to contain the rate limit information""" ratelimited, time = is_ratelimited(user, domain) request._ratelimit_applied_limits = True request._ratelimit_secs_to_freedom = time request._ratelimit_over_limit = ratelimited # Abort this request if the user is over her rate limits if ratelimited: statsd.incr("ratelimiter.limited.%s.%s" % (type(user), user.id)) raise RateLimited() incr_ratelimit(user, domain) calls_remaining, time_reset = api_calls_left(user, domain) request._ratelimit_remaining = calls_remaining request._ratelimit_secs_to_freedom = time_reset
def rate_limit_user(request: HttpRequest, user: UserProfile, domain: str) -> None: """Returns whether or not a user was rate limited. Will raise a RateLimited exception if the user has been rate limited, otherwise returns and modifies request to contain the rate limit information""" entity = RateLimitedUser(user, domain=domain) ratelimited, time = rate_limit_entity(entity) request._ratelimit_applied_limits = True request._ratelimit_secs_to_freedom = time request._ratelimit_over_limit = ratelimited # Abort this request if the user is over their rate limits if ratelimited: raise RateLimited() calls_remaining, time_reset = api_calls_left(entity) request._ratelimit_remaining = calls_remaining request._ratelimit_secs_to_freedom = time_reset