Commit abe00bb1 authored by Denis Bilenko's avatar Denis Bilenko

Server: exit the loop on fatal errors;

- rename first_delay to min_delay; set it to 0.01
- add max_delay which limits the maximum sleep time; set it to 1
parent 6b6fb5ca
# Copyright (c) 2009-2010 Denis Bilenko. See LICENSE for details. # Copyright (c) 2009-2010 Denis Bilenko. See LICENSE for details.
import sys import sys
import errno
from gevent.greenlet import Greenlet from gevent.greenlet import Greenlet
from gevent.pool import GreenletSet, Pool from gevent.pool import GreenletSet, Pool
from gevent import socket from gevent import socket
...@@ -8,11 +9,14 @@ from gevent import sleep ...@@ -8,11 +9,14 @@ from gevent import sleep
__all__ = ['StreamServer'] __all__ = ['StreamServer']
FATAL_ERRORS = (errno.EBADF, errno.EINVAL, errno.ENOTSOCK)
class StreamServer(Greenlet): class StreamServer(Greenlet):
backlog = 256 backlog = 256
first_delay = 0.1 min_delay = 0.01
max_delay = 1
_allowed_ssl_args = ['keyfile', 'certfile', 'cert_reqs', 'ssl_version', 'ca_certs', 'suppress_ragged_eofs'] _allowed_ssl_args = ['keyfile', 'certfile', 'cert_reqs', 'ssl_version', 'ca_certs', 'suppress_ragged_eofs']
def __init__(self, listener, backlog=None, pool=None, log=sys.stderr, **ssl_args): def __init__(self, listener, backlog=None, pool=None, log=sys.stderr, **ssl_args):
...@@ -83,16 +87,20 @@ class StreamServer(Greenlet): ...@@ -83,16 +87,20 @@ class StreamServer(Greenlet):
def _run(self): def _run(self):
try: try:
self.delay = self.first_delay self.delay = self.min_delay
while True: while True:
try: try:
client_socket, address = self.socket.accept() client_socket, address = self.socket.accept()
self.delay = self.first_delay self.delay = self.min_delay
self.pool.spawn(self.handle, client_socket, address) self.pool.spawn(self.handle, client_socket, address)
except socket.error, e: except socket.error, e:
self.log_message('WARNING: %s: accept() failed with %s: %s; will sleep %s seconds' % (self, e[0], e, self.delay)) if e[0] in FATAL_ERRORS:
sleep(self.delay) self.log_message('ERROR: %s failed with %s' % (self, e))
self.delay *= 2 return e
else:
self.log_message('WARNING: %s: ignoring %s (sleeping %s seconds)' % (self, e, self.delay))
sleep(self.delay)
self.delay = min(self.max_delay, self.delay*2)
finally: finally:
try: try:
self.socket.close() self.socket.close()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment