Skip to content

Commit 2176f4c

Browse files
committed
Use uvloop, raise ulimit on posix, and improve startup detection
Signed-off-by: Anuraag Agrawal <[email protected]>
1 parent d9ec8aa commit 2176f4c

File tree

4 files changed

+66
-14
lines changed

4 files changed

+66
-14
lines changed

conformance/test/server.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ async def serve_granian(
444444
# so we need to determine it ourselves. If we see race conditions because of it,
445445
# we can set max-servers=1 in the runner.
446446
port = _find_free_port()
447-
args = [f"--port={port}"]
447+
args = [f"--port={port}", "--loop=uvloop"]
448448
if certfile:
449449
args.append(f"--ssl-certificate={certfile}")
450450
if keyfile:
@@ -473,7 +473,7 @@ async def serve_granian(
473473
try:
474474
for _ in range(100):
475475
line = await stdout.readline()
476-
if b"Listening at:" in line:
476+
if b"Started worker-1 runtime-1" in line:
477477
break
478478
port_future.set_result(port)
479479
await proc.wait()
@@ -510,13 +510,20 @@ async def serve_gunicorn(
510510
)
511511
stdout = proc.stdout
512512
assert stdout is not None # noqa: S101
513+
port = None
513514
try:
514515
for _ in range(100):
515516
line = await stdout.readline()
516517
match = _port_regex.match(line.decode("utf-8"))
517518
if match:
518-
port_future.set_result(int(match.group(1)))
519+
port = int(match.group(1))
520+
break
521+
if b"Booting worker with pid" in line:
519522
break
523+
if port is None:
524+
msg = "Could not determine port from gunicorn output"
525+
raise RuntimeError(msg)
526+
port_future.set_result(port)
520527
await proc.wait()
521528
except asyncio.CancelledError:
522529
proc.terminate()
@@ -531,7 +538,7 @@ async def serve_hypercorn(
531538
cafile: str | None,
532539
port_future: asyncio.Future[int],
533540
):
534-
args = ["--bind=localhost:0"]
541+
args = ["--bind=localhost:0", "--worker-class=uvloop"]
535542
if certfile:
536543
args.append(f"--certfile={certfile}")
537544
if keyfile:

conformance/test/test_server.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import subprocess
23
import sys
34
from pathlib import Path
@@ -11,15 +12,17 @@
1112
_config_path = str(_current_dir / "config.yaml")
1213

1314

14-
# Servers often run out of file descriptors on macOS due to low default ulimit.
15+
# Servers often run out of file descriptors due to low default ulimit.
1516
# We go ahead and raise it automatically so tests can pass without special
1617
# configuration.
1718
@pytest.fixture(autouse=True, scope="session")
1819
def macos_raise_ulimit():
19-
if sys.platform == "darwin":
20-
import resource # noqa: PLC0415
20+
if os.name != "posix":
21+
return
2122

22-
resource.setrlimit(resource.RLIMIT_NOFILE, (16384, 16384))
23+
import resource # noqa: PLC0415
24+
25+
resource.setrlimit(resource.RLIMIT_NOFILE, (16384, 16384))
2326

2427

2528
@pytest.mark.parametrize("server", ["granian", "gunicorn", "hypercorn"])

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ dev = [
4040
"brotli==1.1.0",
4141
"connect-python-example",
4242
"httpx[http2]==0.28.1",
43-
"hypercorn==0.17.3",
44-
"granian==2.5.5",
43+
"hypercorn[uvloop]==0.17.3",
44+
"granian[uvloop]==2.5.5",
4545
"gunicorn==23.0.0",
4646
"just-bin==1.42.4; sys_platform != 'win32'",
4747
"mkdocs==1.6.1",

uv.lock

Lines changed: 46 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)