def test_check_request_age_decorator(self): """ Test the check_request_age decorator with a recent request""" # Set the start time of the request to now self._start = monotonic_time() # Call a mock function with the decorator - as the request is recent, the # underlying function should be called as normal decorator = self.handler.check_request_age func = MagicMock() decorated = decorator(func) decorated(self, "arg1") func.assert_called_once_with(self, "arg1")
def prepare(self): # Increment the request counter incoming_requests.increment() # timestamp the request self._start = monotonic_time() _log.info("Received request from %s - %s %s://%s%s" % (self.request.remote_ip, self.request.method, self.request.protocol, self.request.host, self.request.uri)) if not loadmonitor.admit_request(): _log.warning("Rejecting request because of overload") overload_counter.increment() return Failure(HTTPError(httplib.SERVICE_UNAVAILABLE))
def test_check_request_age_decorator_error(self): """ Test the check_request_age decorator with an old request""" self.send_error = MagicMock() # Ensure that the request is too old self._start = monotonic_time() - 1000 # Call a mock function with the decorator - as the request is old, the decorator # should send a 503 error and the underlying function should not be called. decorator = self.handler.check_request_age func = MagicMock() decorated = decorator(func) decorated(self, "arg") self.send_error.assert_called_once_with(503, "Request too old") self.assertFalse(func.called)
def on_finish(self): _log.info("Sending %s response to %s for %s %s://%s%s" % (self.get_status(), self.request.remote_ip, self.request.method, self.request.protocol, self.request.host, self.request.uri)) latency = monotonic_time() - self._start loadmonitor.request_complete(latency) # Track the latency of the requests (in usec) latency_accumulator.accumulate(latency * 1000000)
def __init__(self, max_size, rate): self.max_size = max_size self.tokens = max_size self.rate = rate self.replenish_time = monotonic_time()
def wrapper(handler, *pos_args, **kwd_args): if monotonic_time() - handler._start > MAX_REQUEST_TIME: handler.send_error(503, "Request too old") else: return func(handler, *pos_args, **kwd_args)
def replenish_bucket(self): replenish_time = monotonic_time() self.tokens += self.rate * (replenish_time - self.replenish_time) self.replenish_time = replenish_time if self.tokens > self.max_size: self.tokens = self.max_size