Skip to content

Commit

Permalink
Merge pull request #702 from KeepSafe/server_timers_optimization
Browse files Browse the repository at this point in the history
Ceil server timeouts
  • Loading branch information
asvetlov committed Dec 22, 2015
2 parents 23e1fc8 + 1ac83ee commit f7efee0
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions aiohttp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import socket

from html import escape as html_escape
from math import ceil

import aiohttp
from aiohttp import errors, streams, hdrs, helpers
Expand Down Expand Up @@ -136,8 +137,9 @@ def closing(self, timeout=15.0):

# use slow request timeout for closing
# connection_lost cleans timeout handler
self._timeout_handle = self._loop.call_later(
timeout, self.cancel_slow_request)
now = self._loop.time()
self._timeout_handle = self._loop.call_at(
ceil(now+timeout), self.cancel_slow_request)

def connection_made(self, transport):
super().connection_made(transport)
Expand All @@ -146,8 +148,9 @@ def connection_made(self, transport):

# start slow request timer
if self._timeout:
self._timeout_handle = self._loop.call_later(
self._timeout, self.cancel_slow_request)
now = self._loop.time()
self._timeout_handle = self._loop.call_at(
ceil(now+self._timeout), self.cancel_slow_request)

if self._keep_alive_on:
tcp_keepalive(self, transport)
Expand Down Expand Up @@ -235,8 +238,9 @@ def start(self):

# start slow request timer
if self._timeout and self._timeout_handle is None:
self._timeout_handle = self._loop.call_later(
self._timeout, self.cancel_slow_request)
now = self._loop.time()
self._timeout_handle = self._loop.call_at(
ceil(now+self._timeout), self.cancel_slow_request)

# read request headers
httpstream = reader.set_parser(self._request_parser)
Expand Down Expand Up @@ -295,8 +299,10 @@ def start(self):
self.log_debug(
'Start keep-alive timer for %s sec.',
self._keep_alive_period)
self._keep_alive_handle = self._loop.call_later(
self._keep_alive_period, self.transport.close)
now = self._loop.time()
self._keep_alive_handle = self._loop.call_at(
ceil(now+self._keep_alive_period),
self.transport.close)
elif self._keep_alive and self._keep_alive_on:
# do nothing, rely on kernel or upstream server
pass
Expand Down

0 comments on commit f7efee0

Please sign in to comment.