]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Stream -> TCPStream (#339)
authorFlorimond Manca <florimond.manca@gmail.com>
Sun, 15 Sep 2019 21:14:48 +0000 (23:14 +0200)
committerSeth Michael Larson <sethmichaellarson@gmail.com>
Sun, 15 Sep 2019 21:14:48 +0000 (16:14 -0500)
httpx/__init__.py
httpx/concurrency/asyncio.py
httpx/concurrency/base.py
httpx/dispatch/connection.py
httpx/dispatch/http11.py
httpx/dispatch/http2.py
tests/dispatch/utils.py
tests/test_concurrency.py

index a070d6856271e7e36bc553f924cb32d0c5ea816d..b464daa95dadce2aa1fc127d036dfdcdc8f8e619 100644 (file)
@@ -5,7 +5,7 @@ from .concurrency.asyncio import AsyncioBackend
 from .concurrency.base import (
     BaseBackgroundManager,
     BasePoolSemaphore,
-    BaseStream,
+    BaseTCPStream,
     ConcurrencyBackend,
 )
 from .config import (
@@ -112,7 +112,7 @@ __all__ = [
     "TooManyRedirects",
     "WriteTimeout",
     "AsyncDispatcher",
-    "BaseStream",
+    "BaseTCPStream",
     "ConcurrencyBackend",
     "Dispatcher",
     "URL",
index 9e50445abb009bc9aa1dd68487d242592518e483..1a145bed9039e2ee1c149e9890cf232379bd9c91 100644 (file)
@@ -1,13 +1,3 @@
-"""
-The `Stream` class here provides a lightweight layer over
-`asyncio.StreamReader` and `asyncio.StreamWriter`.
-
-Similarly `PoolSemaphore` is a lightweight layer over `BoundedSemaphore`.
-
-These classes help encapsulate the timeout logic, make it easier to unit-test
-protocols, and help keep the rest of the package more `async`/`await`
-based, and less strictly `asyncio`-specific.
-"""
 import asyncio
 import functools
 import ssl
@@ -21,7 +11,7 @@ from .base import (
     BaseEvent,
     BasePoolSemaphore,
     BaseQueue,
-    BaseStream,
+    BaseTCPStream,
     ConcurrencyBackend,
     TimeoutFlag,
 )
@@ -50,7 +40,7 @@ def ssl_monkey_patch() -> None:
     MonkeyPatch.write = _fixed_write
 
 
-class Stream(BaseStream):
+class TCPStream(BaseTCPStream):
     def __init__(
         self,
         stream_reader: asyncio.StreamReader,
@@ -176,13 +166,13 @@ class AsyncioBackend(ConcurrencyBackend):
                 self._loop = asyncio.new_event_loop()
         return self._loop
 
-    async def connect(
+    async def open_tcp_stream(
         self,
         hostname: str,
         port: int,
         ssl_context: typing.Optional[ssl.SSLContext],
         timeout: TimeoutConfig,
-    ) -> BaseStream:
+    ) -> BaseTCPStream:
         try:
             stream_reader, stream_writer = await asyncio.wait_for(  # type: ignore
                 asyncio.open_connection(hostname, port, ssl=ssl_context),
@@ -191,17 +181,17 @@ class AsyncioBackend(ConcurrencyBackend):
         except asyncio.TimeoutError:
             raise ConnectTimeout()
 
-        return Stream(
+        return TCPStream(
             stream_reader=stream_reader, stream_writer=stream_writer, timeout=timeout
         )
 
     async def start_tls(
         self,
-        stream: BaseStream,
+        stream: BaseTCPStream,
         hostname: str,
         ssl_context: ssl.SSLContext,
         timeout: TimeoutConfig,
-    ) -> BaseStream:
+    ) -> BaseTCPStream:
 
         loop = self.loop
         if not hasattr(loop, "start_tls"):  # pragma: no cover
@@ -209,7 +199,7 @@ class AsyncioBackend(ConcurrencyBackend):
                 "asyncio.AbstractEventLoop.start_tls() is only available in Python 3.7+"
             )
 
-        assert isinstance(stream, Stream)
+        assert isinstance(stream, TCPStream)
 
         stream_reader = asyncio.StreamReader()
         protocol = asyncio.StreamReaderProtocol(stream_reader)
index e62253ddc4dc6193ddcaa98769f9a93120e79080..63fb14323d2177970b91f533e1d013a82d5a72ad 100644 (file)
@@ -37,9 +37,9 @@ class TimeoutFlag:
         self.raise_on_write_timeout = True
 
 
-class BaseStream:
+class BaseTCPStream:
     """
-    A stream with read/write operations. Abstracts away any asyncio-specific
+    A TCP stream with read/write operations. Abstracts away any asyncio-specific
     interfaces into a more generic base class, that we can use with alternate
     backends, or for stand-alone test cases.
     """
@@ -110,22 +110,22 @@ class BasePoolSemaphore:
 
 
 class ConcurrencyBackend:
-    async def connect(
+    async def open_tcp_stream(
         self,
         hostname: str,
         port: int,
         ssl_context: typing.Optional[ssl.SSLContext],
         timeout: TimeoutConfig,
-    ) -> BaseStream:
+    ) -> BaseTCPStream:
         raise NotImplementedError()  # pragma: no cover
 
     async def start_tls(
         self,
-        stream: BaseStream,
+        stream: BaseTCPStream,
         hostname: str,
         ssl_context: ssl.SSLContext,
         timeout: TimeoutConfig,
-    ) -> BaseStream:
+    ) -> BaseTCPStream:
         raise NotImplementedError()  # pragma: no cover
 
     def get_semaphore(self, limits: PoolLimits) -> BasePoolSemaphore:
index 529d373aeea27ac3464cb6e0ce49d6102221320b..6e8cf0d0a37705cf8fe2d685d2bb3382f7c0d68d 100644 (file)
@@ -85,7 +85,7 @@ class HTTPConnection(AsyncDispatcher):
             on_release = functools.partial(self.release_func, self)
 
         logger.debug(f"start_connect host={host!r} port={port!r} timeout={timeout!r}")
-        stream = await self.backend.connect(host, port, ssl_context, timeout)
+        stream = await self.backend.open_tcp_stream(host, port, ssl_context, timeout)
         http_version = stream.get_http_version()
         logger.debug(f"connected http_version={http_version!r}")
 
index cfd227f0787feddb3d7fb91ef70d90f319fb09f2..64d49469ec08deb52c8ad0458aadef0523ca7d33 100644 (file)
@@ -2,7 +2,7 @@ import typing
 
 import h11
 
-from ..concurrency.base import BaseStream, ConcurrencyBackend, TimeoutFlag
+from ..concurrency.base import BaseTCPStream, ConcurrencyBackend, TimeoutFlag
 from ..config import TimeoutConfig, TimeoutTypes
 from ..models import AsyncRequest, AsyncResponse
 from ..utils import get_logger
@@ -31,7 +31,7 @@ class HTTP11Connection:
 
     def __init__(
         self,
-        stream: BaseStream,
+        stream: BaseTCPStream,
         backend: ConcurrencyBackend,
         on_release: typing.Optional[OnReleaseCallback] = None,
     ):
index cf518f1b6ea74e31f698ab5134b19fe25499f86d..4ddd29f6f7ade6c6696c5acd4d67dd6c7c79ecaf 100644 (file)
@@ -4,7 +4,7 @@ import typing
 import h2.connection
 import h2.events
 
-from ..concurrency.base import BaseEvent, BaseStream, ConcurrencyBackend, TimeoutFlag
+from ..concurrency.base import BaseEvent, BaseTCPStream, ConcurrencyBackend, TimeoutFlag
 from ..config import TimeoutConfig, TimeoutTypes
 from ..models import AsyncRequest, AsyncResponse
 from ..utils import get_logger
@@ -17,7 +17,7 @@ class HTTP2Connection:
 
     def __init__(
         self,
-        stream: BaseStream,
+        stream: BaseTCPStream,
         backend: ConcurrencyBackend,
         on_release: typing.Callable = None,
     ):
index c7cbedb52fba0a1b728b811f8bf0d501c674b9ed..aee4c18a7321f0da37ea6fee1966f70db4a85644 100644 (file)
@@ -5,7 +5,7 @@ import h2.config
 import h2.connection
 import h2.events
 
-from httpx import AsyncioBackend, BaseStream, Request, TimeoutConfig
+from httpx import AsyncioBackend, BaseTCPStream, Request, TimeoutConfig
 from tests.concurrency import sleep
 
 
@@ -15,13 +15,13 @@ class MockHTTP2Backend:
         self.backend = AsyncioBackend() if backend is None else backend
         self.server = None
 
-    async def connect(
+    async def open_tcp_stream(
         self,
         hostname: str,
         port: int,
         ssl_context: typing.Optional[ssl.SSLContext],
         timeout: TimeoutConfig,
-    ) -> BaseStream:
+    ) -> BaseTCPStream:
         self.server = MockHTTP2Server(self.app, backend=self.backend)
         return self.server
 
@@ -30,7 +30,7 @@ class MockHTTP2Backend:
         return getattr(self.backend, name)
 
 
-class MockHTTP2Server(BaseStream):
+class MockHTTP2Server(BaseTCPStream):
     def __init__(self, app, backend):
         config = h2.config.H2Configuration(client_side=False)
         self.conn = h2.connection.H2Connection(config=config)
@@ -42,7 +42,7 @@ class MockHTTP2Server(BaseStream):
         self.return_data = {}
         self.returning = {}
 
-    # Stream interface
+    # TCP stream interface
 
     def get_http_version(self) -> str:
         return "HTTP/2"
index b9312eaa139b4c3d5c322442295c90389e722c2e..ab93b302829d2293eb35845fd77276d69811de95 100644 (file)
@@ -19,7 +19,7 @@ async def test_start_tls_on_socket_stream(https_server):
     ctx = SSLConfig().load_ssl_context_no_verify(HTTPVersionConfig())
     timeout = TimeoutConfig(5)
 
-    stream = await backend.connect(
+    stream = await backend.open_tcp_stream(
         https_server.url.host, https_server.url.port, None, timeout
     )