Skip to content

Commit

Permalink
Add basic socket activation support
Browse files Browse the repository at this point in the history
Signed-off-by: Tien-Ren Chen <[email protected]>
  • Loading branch information
trchen1033 committed Sep 27, 2023
1 parent ee62d07 commit d286887
Showing 1 changed file with 19 additions and 2 deletions.
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 @@ def parse_endpoint(self, http_path: str) -> str:
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 @@ def get_server(self) -> Server:
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

0 comments on commit d286887

Please sign in to comment.