add file_descriptor keword argument to daphne server and instruct twisted reactor to use the file descriptor

This commit is contained in:
Sean Mc Allister 2016-07-13 14:07:57 +02:00
parent d17394e84c
commit 0e1bce1714

View File

@ -1,6 +1,6 @@
import logging import logging
from twisted.internet import reactor from twisted.internet import reactor
import socket
from .http_protocol import HTTPFactory from .http_protocol import HTTPFactory
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -14,6 +14,7 @@ class Server(object):
host="127.0.0.1", host="127.0.0.1",
port=8000, port=8000,
unix_socket=None, unix_socket=None,
file_descriptor=None,
signal_handlers=True, signal_handlers=True,
action_logger=None, action_logger=None,
http_timeout=120, http_timeout=120,
@ -26,6 +27,7 @@ class Server(object):
self.host = host self.host = host
self.port = port self.port = port
self.unix_socket = unix_socket self.unix_socket = unix_socket
self.file_descriptor = file_descriptor
self.signal_handlers = signal_handlers self.signal_handlers = signal_handlers
self.action_logger = action_logger self.action_logger = action_logger
self.http_timeout = http_timeout self.http_timeout = http_timeout
@ -48,8 +50,13 @@ class Server(object):
) )
if self.unix_socket: if self.unix_socket:
reactor.listenUNIX(self.unix_socket, self.factory) reactor.listenUNIX(self.unix_socket, self.factory)
elif self.file_descriptor:
# socket returns the same socket if supplied with a fileno
sock = socket.socket(fileno=self.file_descriptor)
reactor.adoptStreamPort(self.file_descriptor, sock.family, self.factory)
else: else:
reactor.listenTCP(self.port, self.factory, interface=self.host) reactor.listenTCP(self.port, self.factory, interface=self.host)
reactor.callLater(0, self.backend_reader) reactor.callLater(0, self.backend_reader)
reactor.callLater(2, self.timeout_checker) reactor.callLater(2, self.timeout_checker)
reactor.run(installSignalHandlers=self.signal_handlers) reactor.run(installSignalHandlers=self.signal_handlers)