Drop write_no_block from backends. (#594)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Drop write_no_block * Drop redundant code from Trio backend
authored by Tom Christie · committed by GitHub · parent fc95c7e · Browse files · 4 files·+20 added−32 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+20 added−32 removed
- other:+20 added−32 removed
Provenance
- Commit
e56e120- Tree
f473417- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
fc95c7e
4 files changed+20 added−32 removed
modified:httpx/concurrency/asyncio.py-3 removed
160 unmodified lines161 break162 163 return data164 165- def write_no_block(self, data: bytes) -> None:166- self.stream_writer.write(data) # pragma: nocover167-165 async def write(166 self, data: bytes, timeout: Timeout = None, flag: TimeoutFlag = None167 ) -> None:168 if not data:182 unmodified linesmodified:httpx/concurrency/base.py-3 removed
75 unmodified lines76 77 async def read(self, n: int, timeout: Timeout, flag: typing.Any = None) -> bytes:78 raise NotImplementedError() # pragma: no cover79 80- def write_no_block(self, data: bytes) -> None:81- raise NotImplementedError() # pragma: no cover82-80 async def write(self, data: bytes, timeout: Timeout) -> None:81 raise NotImplementedError() # pragma: no cover82 83 async def close(self) -> None:97 unmodified linesmodified:httpx/concurrency/trio.py-19 removed
25 unmodified lines26 self, stream: typing.Union[trio.SocketStream, trio.SSLStream], timeout: Timeout,27 ) -> None:28 self.stream = stream29 self.timeout = timeout30- self.write_buffer = b""30 self.read_lock = trio.Lock()31 self.write_lock = trio.Lock()32 33 async def start_tls(34 self, hostname: str, ssl_context: ssl.SSLContext, timeout: Timeout35 ) -> "SocketStream":37- # Check that the write buffer is empty. We should never start a TLS stream38- # while there is still pending data to write.39- assert self.write_buffer == b""40-36 connect_timeout = _or_inf(timeout.connect_timeout)37 ssl_stream = trio.SSLStream(38 self.stream, ssl_context=ssl_context, server_hostname=hostname39 )46 unmodified lines86 # called `.recv()` on it, indicating that the other end has closed the socket.87 # See: https://github.com/encode/httpx/pull/143#issuecomment-51518177888 return stream.socket.is_readable()89 95- def write_no_block(self, data: bytes) -> None:96- self.write_buffer += data # pragma: no cover97-90 async def write(91 self, data: bytes, timeout: Timeout = None, flag: TimeoutFlag = None92 ) -> None:101- if self.write_buffer:102- previous_data = self.write_buffer103- # Reset before recursive call, otherwise we'll go through104- # this branch indefinitely.105- self.write_buffer = b""106- try:107- await self.write(previous_data, timeout=timeout, flag=flag)108- except WriteTimeout:109- self.writer_buffer = previous_data110- raise111-93 if not data:94 return95 96 if timeout is None:161 unmodified linesmodified:httpx/dispatch/http2.py+20 added-7 removed
33 unmodified lines34 self.on_release = on_release35 self.h2_state = h2.connection.H2Connection()36 self.events = {} # type: typing.Dict[int, typing.List[h2.events.Event]]37 self.timeout_flags = {} # type: typing.Dict[int, TimeoutFlag]38- self.initialized = False38 self.window_update_received = {} # type: typing.Dict[int, BaseEvent]39 40+ self.init_started = False41+42+ @property43+ def init_complete(self) -> BaseEvent:44+ # We do this lazily, to make sure backend autodetection always45+ # runs within an async context.46+ if not hasattr(self, "_initialization_complete"):47+ self._initialization_complete = self.backend.create_event()48+ return self._initialization_complete49+50 async def send(self, request: Request, timeout: Timeout = None) -> Response:51 timeout = Timeout() if timeout is None else timeout52 44- # Start sending the request.45- if not self.initialized:46- self.initiate_connection()53+ if not self.init_started:54+ # The very first stream is responsible for initiating the connection.55+ self.init_started = True56+ await self.send_connection_init(timeout)57+ self.init_complete.set()58+ else:59+ # All other streams need to wait until the connection is established.60+ await self.init_complete.wait()61 62 stream_id = await self.send_headers(request, timeout)63 64 self.events[stream_id] = []17 unmodified lines82 83 async def close(self) -> None:84 await self.stream.close()85 72- def initiate_connection(self) -> None:86+ async def send_connection_init(self, timeout: Timeout) -> None:87 # Need to set these manually here instead of manipulating via88 # __setitem__() otherwise the H2Connection will emit SettingsUpdate89 # frames in addition to sending the undesired defaults.90 self.h2_state.local_settings = Settings(16 unmodified lines107 ]108 109 self.h2_state.initiate_connection()110 data_to_send = self.h2_state.data_to_send()97- self.stream.write_no_block(data_to_send)98- self.initialized = True111+ await self.stream.write(data_to_send, timeout)112 113 async def send_headers(self, request: Request, timeout: Timeout) -> int:114 stream_id = self.h2_state.get_next_available_stream_id()115 headers = [144 unmodified lines