Merge pull request #32 from mcallistersean/master

Support inherited file descriptors
This commit is contained in:
Andrew Godwin 2016-07-15 10:15:31 -07:00 committed by GitHub
commit cfc7ec51b3
3 changed files with 31 additions and 1 deletions

View File

@ -24,6 +24,21 @@ set a bind address and port (defaults to localhost, port 8000)::
daphne -b 0.0.0.0 -p 8001 django_project.asgi:channel_layer daphne -b 0.0.0.0 -p 8001 django_project.asgi:channel_layer
If you intend to run daphne behind a proxy server you can use UNIX
sockets to communicate between the two::
daphne -u /tmp/daphne.sock django_project.asgi:channel_layer
If daphne is being run inside a process manager such as
`Circus <https://github.com/circus-tent/circus/>`_ you might
want it to bind to a file descriptor passed down from a parent process.
To achieve this you can use the --fd flag::
daphne --fd 5 django_project.asgi:channel_layer
To see all available command line options run daphne with the *-h* flag.
Root Path (SCRIPT_NAME) Root Path (SCRIPT_NAME)
----------------------- -----------------------

View File

@ -41,6 +41,13 @@ class CommandLineInterface(object):
help='Bind to a UNIX socket rather than a TCP host/port', help='Bind to a UNIX socket rather than a TCP host/port',
default=None, default=None,
) )
self.parser.add_argument(
'--fd',
type=int,
dest='file_descriptor',
help='Bind to a file descriptor rather than a TCP host/port or named unix socket',
default=None,
)
self.parser.add_argument( self.parser.add_argument(
'-v', '-v',
'--verbosity', '--verbosity',
@ -133,6 +140,7 @@ class CommandLineInterface(object):
host=args.host, host=args.host,
port=args.port, port=args.port,
unix_socket=args.unix_socket, unix_socket=args.unix_socket,
file_descriptor=args.file_descriptor,
http_timeout=args.http_timeout, http_timeout=args.http_timeout,
ping_interval=args.ping_interval, ping_interval=args.ping_interval,
action_logger=AccessLogGenerator(access_log_stream) if access_log_stream else None, action_logger=AccessLogGenerator(access_log_stream) if access_log_stream else None,

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)