-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Get rid of high connection latency #671
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,6 +101,7 @@ def __init__(self, *, loop=None, | |
self._keep_alive_period = keep_alive # number of seconds to keep alive | ||
self._timeout = timeout # slow request timeout | ||
self._loop = loop if loop is not None else asyncio.get_event_loop() | ||
self._tcp_nodelay = False | ||
|
||
self.logger = log or logger | ||
self.debug = debug | ||
|
@@ -291,15 +292,17 @@ def start(self): | |
reader.unset_parser() | ||
|
||
if self._request_handler: | ||
if self._keep_alive and self._keep_alive_period: | ||
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) | ||
elif self._keep_alive and self._keep_alive_on: | ||
# do nothing, rely on kernel or upstream server | ||
pass | ||
if self._keep_alive: | ||
self.set_tcp_nodelay(True) | ||
if self._keep_alive_period: | ||
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) | ||
elif self._keep_alive_on: | ||
# do nothing, rely on kernel or upstream server | ||
pass | ||
else: | ||
self.log_debug('Close client connection.') | ||
self._request_handler = None | ||
|
@@ -388,3 +391,19 @@ def handle_request(self, message, payload): | |
self.log_access(message, None, response, self._loop.time() - now) | ||
|
||
return drain | ||
|
||
@property | ||
def tcp_nodelay(self): | ||
return self._tcp_nodelay | ||
|
||
def set_tcp_nodelay(self, nodelay): | ||
if nodelay == self._tcp_nodelay: | ||
return | ||
self._tcp_nodelay = nodelay | ||
if self.transport is None: | ||
# transport is closed | ||
return | ||
sock = self.transport.get_extra_info('socket') | ||
if sock.family not in (socket.AF_INET, socket.AF_INET6): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Transports need not have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @asvetlov I agree with @jashandeep-sohi |
||
return | ||
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also it will be nice to cache socket reference in order to speedup setting and removing nodelay