Skip to content

Commit 315a18b

Browse files
florimondmancasethmlarson
authored andcommitted
Stream -> TCPStream (#339)
1 parent 4616031 commit 315a18b

File tree

8 files changed

+27
-37
lines changed

8 files changed

+27
-37
lines changed

httpx/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from .concurrency.base import (
66
BaseBackgroundManager,
77
BasePoolSemaphore,
8-
BaseStream,
8+
BaseTCPStream,
99
ConcurrencyBackend,
1010
)
1111
from .config import (
@@ -112,7 +112,7 @@
112112
"TooManyRedirects",
113113
"WriteTimeout",
114114
"AsyncDispatcher",
115-
"BaseStream",
115+
"BaseTCPStream",
116116
"ConcurrencyBackend",
117117
"Dispatcher",
118118
"URL",

httpx/concurrency/asyncio.py

+8-18
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
"""
2-
The `Stream` class here provides a lightweight layer over
3-
`asyncio.StreamReader` and `asyncio.StreamWriter`.
4-
5-
Similarly `PoolSemaphore` is a lightweight layer over `BoundedSemaphore`.
6-
7-
These classes help encapsulate the timeout logic, make it easier to unit-test
8-
protocols, and help keep the rest of the package more `async`/`await`
9-
based, and less strictly `asyncio`-specific.
10-
"""
111
import asyncio
122
import functools
133
import ssl
@@ -21,7 +11,7 @@
2111
BaseEvent,
2212
BasePoolSemaphore,
2313
BaseQueue,
24-
BaseStream,
14+
BaseTCPStream,
2515
ConcurrencyBackend,
2616
TimeoutFlag,
2717
)
@@ -50,7 +40,7 @@ def _fixed_write(self, data: bytes) -> None: # type: ignore
5040
MonkeyPatch.write = _fixed_write
5141

5242

53-
class Stream(BaseStream):
43+
class TCPStream(BaseTCPStream):
5444
def __init__(
5545
self,
5646
stream_reader: asyncio.StreamReader,
@@ -176,13 +166,13 @@ def loop(self) -> asyncio.AbstractEventLoop:
176166
self._loop = asyncio.new_event_loop()
177167
return self._loop
178168

179-
async def connect(
169+
async def open_tcp_stream(
180170
self,
181171
hostname: str,
182172
port: int,
183173
ssl_context: typing.Optional[ssl.SSLContext],
184174
timeout: TimeoutConfig,
185-
) -> BaseStream:
175+
) -> BaseTCPStream:
186176
try:
187177
stream_reader, stream_writer = await asyncio.wait_for( # type: ignore
188178
asyncio.open_connection(hostname, port, ssl=ssl_context),
@@ -191,25 +181,25 @@ async def connect(
191181
except asyncio.TimeoutError:
192182
raise ConnectTimeout()
193183

194-
return Stream(
184+
return TCPStream(
195185
stream_reader=stream_reader, stream_writer=stream_writer, timeout=timeout
196186
)
197187

198188
async def start_tls(
199189
self,
200-
stream: BaseStream,
190+
stream: BaseTCPStream,
201191
hostname: str,
202192
ssl_context: ssl.SSLContext,
203193
timeout: TimeoutConfig,
204-
) -> BaseStream:
194+
) -> BaseTCPStream:
205195

206196
loop = self.loop
207197
if not hasattr(loop, "start_tls"): # pragma: no cover
208198
raise NotImplementedError(
209199
"asyncio.AbstractEventLoop.start_tls() is only available in Python 3.7+"
210200
)
211201

212-
assert isinstance(stream, Stream)
202+
assert isinstance(stream, TCPStream)
213203

214204
stream_reader = asyncio.StreamReader()
215205
protocol = asyncio.StreamReaderProtocol(stream_reader)

httpx/concurrency/base.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ def set_write_timeouts(self) -> None:
3737
self.raise_on_write_timeout = True
3838

3939

40-
class BaseStream:
40+
class BaseTCPStream:
4141
"""
42-
A stream with read/write operations. Abstracts away any asyncio-specific
42+
A TCP stream with read/write operations. Abstracts away any asyncio-specific
4343
interfaces into a more generic base class, that we can use with alternate
4444
backends, or for stand-alone test cases.
4545
"""
@@ -110,22 +110,22 @@ def release(self) -> None:
110110

111111

112112
class ConcurrencyBackend:
113-
async def connect(
113+
async def open_tcp_stream(
114114
self,
115115
hostname: str,
116116
port: int,
117117
ssl_context: typing.Optional[ssl.SSLContext],
118118
timeout: TimeoutConfig,
119-
) -> BaseStream:
119+
) -> BaseTCPStream:
120120
raise NotImplementedError() # pragma: no cover
121121

122122
async def start_tls(
123123
self,
124-
stream: BaseStream,
124+
stream: BaseTCPStream,
125125
hostname: str,
126126
ssl_context: ssl.SSLContext,
127127
timeout: TimeoutConfig,
128-
) -> BaseStream:
128+
) -> BaseTCPStream:
129129
raise NotImplementedError() # pragma: no cover
130130

131131
def get_semaphore(self, limits: PoolLimits) -> BasePoolSemaphore:

httpx/dispatch/connection.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ async def connect(
8585
on_release = functools.partial(self.release_func, self)
8686

8787
logger.debug(f"start_connect host={host!r} port={port!r} timeout={timeout!r}")
88-
stream = await self.backend.connect(host, port, ssl_context, timeout)
88+
stream = await self.backend.open_tcp_stream(host, port, ssl_context, timeout)
8989
http_version = stream.get_http_version()
9090
logger.debug(f"connected http_version={http_version!r}")
9191

httpx/dispatch/http11.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import h11
44

5-
from ..concurrency.base import BaseStream, ConcurrencyBackend, TimeoutFlag
5+
from ..concurrency.base import BaseTCPStream, ConcurrencyBackend, TimeoutFlag
66
from ..config import TimeoutConfig, TimeoutTypes
77
from ..models import AsyncRequest, AsyncResponse
88
from ..utils import get_logger
@@ -31,7 +31,7 @@ class HTTP11Connection:
3131

3232
def __init__(
3333
self,
34-
stream: BaseStream,
34+
stream: BaseTCPStream,
3535
backend: ConcurrencyBackend,
3636
on_release: typing.Optional[OnReleaseCallback] = None,
3737
):

httpx/dispatch/http2.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import h2.connection
55
import h2.events
66

7-
from ..concurrency.base import BaseEvent, BaseStream, ConcurrencyBackend, TimeoutFlag
7+
from ..concurrency.base import BaseEvent, BaseTCPStream, ConcurrencyBackend, TimeoutFlag
88
from ..config import TimeoutConfig, TimeoutTypes
99
from ..models import AsyncRequest, AsyncResponse
1010
from ..utils import get_logger
@@ -17,7 +17,7 @@ class HTTP2Connection:
1717

1818
def __init__(
1919
self,
20-
stream: BaseStream,
20+
stream: BaseTCPStream,
2121
backend: ConcurrencyBackend,
2222
on_release: typing.Callable = None,
2323
):

tests/dispatch/utils.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import h2.connection
66
import h2.events
77

8-
from httpx import AsyncioBackend, BaseStream, Request, TimeoutConfig
8+
from httpx import AsyncioBackend, BaseTCPStream, Request, TimeoutConfig
99
from tests.concurrency import sleep
1010

1111

@@ -15,13 +15,13 @@ def __init__(self, app, backend=None):
1515
self.backend = AsyncioBackend() if backend is None else backend
1616
self.server = None
1717

18-
async def connect(
18+
async def open_tcp_stream(
1919
self,
2020
hostname: str,
2121
port: int,
2222
ssl_context: typing.Optional[ssl.SSLContext],
2323
timeout: TimeoutConfig,
24-
) -> BaseStream:
24+
) -> BaseTCPStream:
2525
self.server = MockHTTP2Server(self.app, backend=self.backend)
2626
return self.server
2727

@@ -30,7 +30,7 @@ def __getattr__(self, name: str) -> typing.Any:
3030
return getattr(self.backend, name)
3131

3232

33-
class MockHTTP2Server(BaseStream):
33+
class MockHTTP2Server(BaseTCPStream):
3434
def __init__(self, app, backend):
3535
config = h2.config.H2Configuration(client_side=False)
3636
self.conn = h2.connection.H2Connection(config=config)
@@ -42,7 +42,7 @@ def __init__(self, app, backend):
4242
self.return_data = {}
4343
self.returning = {}
4444

45-
# Stream interface
45+
# TCP stream interface
4646

4747
def get_http_version(self) -> str:
4848
return "HTTP/2"

tests/test_concurrency.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async def test_start_tls_on_socket_stream(https_server):
1919
ctx = SSLConfig().load_ssl_context_no_verify(HTTPVersionConfig())
2020
timeout = TimeoutConfig(5)
2121

22-
stream = await backend.connect(
22+
stream = await backend.open_tcp_stream(
2323
https_server.url.host, https_server.url.port, None, timeout
2424
)
2525

0 commit comments

Comments
 (0)