-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
#3227: Wait for the keepalive period to end before stopping the acceptance of new requests #3236
base: master
Are you sure you want to change the base?
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 |
---|---|---|
|
@@ -87,6 +87,10 @@ def run(self): | |
self.notify() | ||
gevent.sleep(1.0) | ||
|
||
# Wait for pending keepalive connections to complete before stopping the acceptance of new requests | ||
if self.cfg.keepalive: | ||
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. i disagree there, we shouldn't care about waiting for keepalive connections when closing. CLose should be done as fast as possible. |
||
self._wait_for_keepalive(servers) | ||
|
||
try: | ||
# Stop accepting requests | ||
for server in servers: | ||
|
@@ -117,6 +121,19 @@ def run(self): | |
except Exception: | ||
pass | ||
|
||
def _wait_for_keepalive(self, servers): | ||
# Retrieve all active greenlets and repeatedly check, until the keepalive period ends, | ||
# if any of those greenlets are still active. If none are active, exit the loop. | ||
|
||
greenlets = {id(greenlet) for server in servers for greenlet in server.pool.greenlets} | ||
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. When this line is executed, self.alive is already False, which means that all keepalive connections are either closed or are being handled by the existing greenlets. No new keepalive connections will be created until the worker restarts. |
||
ts = time.monotonic() | ||
|
||
while time.monotonic() - ts <= self.cfg.keepalive: | ||
if not greenlets.intersection({id(greenlet) for server in servers for greenlet in server.pool.greenlets}): | ||
break | ||
self.notify() | ||
gevent.sleep(1.0) | ||
|
||
def handle(self, listener, client, addr): | ||
# Connected socket timeout defaults to socket.getdefaulttimeout(). | ||
# This forces to blocking mode. | ||
|
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.
this change is OK