Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic socket activation support #717

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions moonraker/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@
import logging
import json
import traceback
import socket
import ssl
import pathlib
import urllib.parse
import tornado
import tornado.iostream
import tornado.httputil
import tornado.netutil
import tornado.web
from contextlib import closing
from inspect import isclass
from tornado.escape import url_unescape, url_escape
from tornado.routing import Rule, PathMatches, AnyMatches
from tornado.http1connection import HTTP1Connection
from tornado.httpserver import HTTPServer
from tornado.log import access_log
from .common import WebRequest, APIDefinition, APITransport
from .utils import ServerError, source_info
Expand All @@ -46,7 +50,6 @@
AsyncGenerator,
)
if TYPE_CHECKING:
from tornado.httpserver import HTTPServer
from .server import Server
from .eventloop import EventLoop
from .confighelper import ConfigHelper
Expand Down Expand Up @@ -269,6 +272,20 @@
return http_path[len(self._route_prefix):]

def listen(self, host: str, port: int, ssl_port: int) -> None:
if "LISTEN_FDS" in os.environ:
assert int(os.environ["LISTEN_FDS"]) >= 1
logging.info("Using socket activation, only serve HTTP and the actual port might not match the port in config.")

Check warning on line 277 in moonraker/app.py

View workflow job for this annotation

GitHub Actions / lint-python-code

line too long (124 > 88 characters)
self.http_server = HTTPServer(self.app, max_body_size=MAX_BODY_SIZE, xheaders=True)

Check warning on line 278 in moonraker/app.py

View workflow job for this annotation

GitHub Actions / lint-python-code

line too long (95 > 88 characters)
with closing(socket.fromfd(3, -1, -1)) as sock_tmp:
sock = socket.fromfd(3,
sock_tmp.getsockopt(socket.SOL_SOCKET, socket.SO_DOMAIN),

Check warning on line 281 in moonraker/app.py

View workflow job for this annotation

GitHub Actions / lint-python-code

continuation line under-indented for visual indent
sock_tmp.getsockopt(socket.SOL_SOCKET, socket.SO_TYPE),

Check warning on line 282 in moonraker/app.py

View workflow job for this annotation

GitHub Actions / lint-python-code

continuation line under-indented for visual indent
sock_tmp.getsockopt(socket.SOL_SOCKET, socket.SO_PROTOCOL),

Check warning on line 283 in moonraker/app.py

View workflow job for this annotation

GitHub Actions / lint-python-code

continuation line under-indented for visual indent
)

Check warning on line 284 in moonraker/app.py

View workflow job for this annotation

GitHub Actions / lint-python-code

closing bracket does not match visual indentation
sock.setblocking(0)
sock.listen(tornado.netutil._DEFAULT_BACKLOG)
self.http_server.add_socket(sock)
return
if host.lower() == "all":
host = ""
self.http_server = self.app.listen(
Expand Down Expand Up @@ -312,7 +329,7 @@
return self.server

def https_enabled(self) -> bool:
return self.cert_path.exists() and self.key_path.exists()
return self.cert_path.exists() and self.key_path.exists() and "LISTEN_FDS" not in os.environ

Check warning on line 332 in moonraker/app.py

View workflow job for this annotation

GitHub Actions / lint-python-code

line too long (100 > 88 characters)

async def close(self) -> None:
if self.http_server is not None:
Expand Down
Loading