Skip to content
Closed
Show file tree
Hide file tree
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
13 changes: 8 additions & 5 deletions uvicorn/protocols/http/flow_control.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
from dataclasses import dataclass, field

from uvicorn._types import ASGIReceiveCallable, ASGISendCallable, Scope

Expand All @@ -7,12 +8,14 @@
HIGH_WATER_LIMIT = 65536


@dataclass(slots=True, repr=False, eq=False)
class FlowControl:
def __init__(self, transport: asyncio.Transport) -> None:
self._transport = transport
self.read_paused = False
self.write_paused = False
self._is_writable_event = asyncio.Event()
_transport: asyncio.Transport
read_paused: bool = False
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

init=False or don't use dataclasses at all

write_paused: bool = False
_is_writable_event: asyncio.Event = field(default_factory=asyncio.Event)

def __post_init__(self) -> None:
self._is_writable_event.set()

async def drain(self) -> None:
Expand Down
27 changes: 27 additions & 0 deletions uvicorn/protocols/http/h11_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,33 @@ def _get_status_phrase(status_code: int) -> bytes:


class H11Protocol(asyncio.Protocol):
__slots__ = (
"config",
"server_state",
"app_state",
"loop",
"app",
"logger",
"access_logger",
"access_log",
"conn",
"ws_protocol_class",
"root_path",
"limit_concurrency",
"timeout_keep_alive_task",
"timeout_keep_alive",
"connections",
"tasks",
"transport",
"flow",
"server",
"client",
"scheme",
"scope",
"headers",
"cycle",
)

def __init__(
self,
config: Config,
Expand Down
30 changes: 30 additions & 0 deletions uvicorn/protocols/http/httptools_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,36 @@ def _get_status_line(status_code: int) -> bytes:


class HttpToolsProtocol(asyncio.Protocol):
__slots__ = (
"config",
"server_state",
"app_state",
"loop",
"app",
"logger",
"access_logger",
"access_log",
"parser",
"ws_protocol_class",
"root_path",
"limit_concurrency",
"timeout_keep_alive_task",
"timeout_keep_alive",
"connections",
"tasks",
"transport",
"flow",
"server",
"client",
"scheme",
"pipeline",
"scope",
"headers",
"expect_100_continue",
"cycle",
"url",
)

def __init__(
self,
config: Config,
Expand Down
30 changes: 22 additions & 8 deletions uvicorn/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import asyncio
import contextlib
import functools
import logging
import os
import platform
Expand All @@ -13,6 +12,7 @@
import threading
import time
from collections.abc import Generator, Sequence
from dataclasses import dataclass, field
from email.utils import formatdate
from types import FrameType
from typing import TYPE_CHECKING, TypeAlias
Expand Down Expand Up @@ -41,19 +41,32 @@
logger = logging.getLogger("uvicorn.error")


@dataclass(slots=True, repr=False, eq=False)
class ServerState:
"""
Shared servers state that is available between all protocol instances.
"""

def __init__(self) -> None:
self.total_requests = 0
self.connections: set[Protocols] = set()
self.tasks: set[asyncio.Task[None]] = set()
self.default_headers: list[tuple[bytes, bytes]] = []
total_requests: int = 0
connections: set[Protocols] = field(default_factory=set)
tasks: set[asyncio.Task[None]] = field(default_factory=set)
default_headers: list[tuple[bytes, bytes]] = field(default_factory=list)


class Server:
__slots__ = (
"config",
"server_state",
"started",
"should_exit",
"force_exit",
"last_notified",
"_captured_signals",
"lifespan",
"servers",
"limit_max_requests",
)

def __init__(self, config: Config) -> None:
self.config = config
self.server_state = ServerState()
Expand All @@ -65,8 +78,9 @@ def __init__(self, config: Config) -> None:

self._captured_signals: list[int] = []

@functools.cached_property
def limit_max_requests(self) -> int | None:
self.limit_max_requests = self._limit_max_requests()

def _limit_max_requests(self) -> int | None:
if self.config.limit_max_requests is None:
return None
return self.config.limit_max_requests + random.randint(0, self.config.limit_max_requests_jitter)
Expand Down
Loading