def _quiesce(self, environ, bypass_auth=False):
"""Set service state to quiesced and shed existing connections."""
if not bypass_auth and not self._authorized_to_quiesce(environ):
raise UnauthorizedError
# Delay shedding to allow service deregistration after quiescing
shed_delay_secs = 30
if not self.quiesced:
self.quiesced = True
total_conns = len(self.connections)
# Note: There's still a small chance that we miss connections
# that came in before we set to quiesced but are
# still being established.
conns = self.connections.copy()
# Shed shed_rate_per_sec connections every second
# after service deregistration delay.
cur_iter_sec = 0
for remaining in xrange(total_conns, 0, -self.shed_rate_per_sec):
cur_iter_sec += 1
# Check if fewer than shed_rate_per_sec conns left
# in set so there's no over-popping.
if remaining >= self.shed_rate_per_sec:
num_conns = self.shed_rate_per_sec
else:
num_conns = remaining
gevent.spawn_later(cur_iter_sec + shed_delay_secs,
self._shed_connections,
[conns.pop() for j in xrange(num_conns)])
# Terminate the service after shedding
termination_delay_secs = 10
gevent.spawn_later(shed_delay_secs + cur_iter_sec +
termination_delay_secs,
self._shutdown)
评论列表
文章目录