Commit 644df38d authored by Denis Bilenko's avatar Denis Bilenko

remove baseserver.DEFAULT_MAX_ACCEPT variable; add get_listener() class...

remove baseserver.DEFAULT_MAX_ACCEPT variable; add get_listener() class methods on StreamServer and DatagramServer
parent 68473677
......@@ -12,9 +12,6 @@ import errno
__all__ = ['BaseServer']
DEFAULT_MAX_ACCEPT = 100
class BaseServer(object):
"""An abstract base class that implements some common functionality for the servers in gevent.
......@@ -46,12 +43,10 @@ class BaseServer(object):
# Default is 100. Note, that in case of multiple working processes on the same
# listening value, it should be set to a lower value. (pywsgi.WSGIServer sets it
# to 1 when environ["wsgi.multiprocess"] is true)
max_accept = None
max_accept = 100
_spawn = Greenlet.spawn
reuse_addr = 1
# the default timeout that we wait for the client connections to close in stop()
stop_timeout = 1
......@@ -69,8 +64,6 @@ class BaseServer(object):
self.set_handle(handle)
self.delay = self.min_delay
self.loop = gevent.get_hub().loop
if self.max_accept is None:
self.max_accept = DEFAULT_MAX_ACCEPT
if self.max_accept < 1:
raise ValueError('max_accept must be positive int: %r' % (self.max_accept, ))
except:
......
......@@ -12,7 +12,6 @@ from urllib import unquote
from gevent import socket
import gevent
from gevent.server import StreamServer
from gevent import server
from gevent.hub import GreenletExit
......@@ -557,11 +556,8 @@ class WSGIServer(StreamServer):
self.environ['wsgi.errors'] = sys.stderr
def set_max_accept(self):
if self.max_accept is None:
if self.environ.get('wsgi.multiprocess'):
self.max_accept = 1
else:
self.max_accept = server.DEFAULT_MAX_ACCEPT
if self.environ.get('wsgi.multiprocess'):
self.max_accept = 1
def get_environ(self):
return self.environ.copy()
......
......@@ -35,6 +35,8 @@ class StreamServer(BaseServer):
# the default backlog to use if none was provided in __init__
backlog = 256
reuse_addr = 1
def __init__(self, listener, handle=None, backlog=None, spawn='default', **ssl_args):
BaseServer.__init__(self, listener, handle=handle, spawn=spawn)
try:
......@@ -66,14 +68,19 @@ class StreamServer(BaseServer):
def init_socket(self):
if not hasattr(self, 'socket'):
self.socket = _tcp_listener(self.address, backlog=self.backlog,
reuse_addr=self.reuse_addr, family=self.family)
self.socket = self.get_listener(self.address, self.backlog, self.family)
self.address = self.socket.getsockname()
if self.ssl_args:
self._handle = self.wrap_socket_and_handle
else:
self._handle = self.handle
@classmethod
def get_listener(self, address, backlog=None, family=None):
if backlog is None:
backlog = self.backlog
return _tcp_listener(address, backlog=backlog, reuse_addr=self.reuse_addr, family=family)
def do_read(self):
try:
client_socket, address = self.socket.accept()
......@@ -92,6 +99,8 @@ class StreamServer(BaseServer):
class DatagramServer(BaseServer):
"""A UDP server"""
reuse_addr = 1
def __init__(self, *args, **kwargs):
BaseServer.__init__(self, *args, **kwargs)
from gevent.coros import Semaphore
......@@ -99,7 +108,7 @@ class DatagramServer(BaseServer):
def init_socket(self):
if not hasattr(self, 'socket'):
self.socket = _udp_socket(self.address, reuse_addr=self.reuse_addr, family=self.family)
self.socket = self.get_listener(self.address, self.family)
self.address = self.socket.getsockname()
self._socket = self.socket
try:
......@@ -107,6 +116,10 @@ class DatagramServer(BaseServer):
except AttributeError:
pass
@classmethod
def get_listener(self, address, family=None):
return _udp_socket(address, reuse_addr=self.reuse_addr, family=family)
def do_read(self):
try:
data, address = self._socket.recvfrom(8192)
......
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