|
| 1 | +import select |
| 2 | +from ssl import SSLContext, SSLSocket |
| 3 | +from typing import Optional |
| 4 | + |
| 5 | +import curio |
| 6 | +import curio.io |
| 7 | + |
| 8 | +from .._exceptions import ( |
| 9 | + ConnectError, |
| 10 | + ConnectTimeout, |
| 11 | + ReadError, |
| 12 | + ReadTimeout, |
| 13 | + WriteError, |
| 14 | + WriteTimeout, |
| 15 | + map_exceptions, |
| 16 | +) |
| 17 | +from .._types import TimeoutDict |
| 18 | +from .._utils import get_logger |
| 19 | +from .base import AsyncBackend, AsyncLock, AsyncSemaphore, AsyncSocketStream |
| 20 | + |
| 21 | +logger = get_logger(__name__) |
| 22 | + |
| 23 | +ONE_DAY_IN_SECONDS = float(60 * 60 * 24) |
| 24 | + |
| 25 | + |
| 26 | +def convert_timeout(value: Optional[float]) -> float: |
| 27 | + return value if value is not None else ONE_DAY_IN_SECONDS |
| 28 | + |
| 29 | + |
| 30 | +class Lock(AsyncLock): |
| 31 | + def __init__(self) -> None: |
| 32 | + self._lock = curio.Lock() |
| 33 | + |
| 34 | + async def acquire(self) -> None: |
| 35 | + await self._lock.acquire() |
| 36 | + |
| 37 | + async def release(self) -> None: |
| 38 | + await self._lock.release() |
| 39 | + |
| 40 | + |
| 41 | +class Semaphore(AsyncSemaphore): |
| 42 | + def __init__(self, max_value: int, exc_class: type) -> None: |
| 43 | + self.max_value = max_value |
| 44 | + self.exc_class = exc_class |
| 45 | + |
| 46 | + @property |
| 47 | + def semaphore(self) -> curio.Semaphore: |
| 48 | + if not hasattr(self, "_semaphore"): |
| 49 | + self._semaphore = curio.Semaphore(value=self.max_value) |
| 50 | + return self._semaphore |
| 51 | + |
| 52 | + async def acquire(self, timeout: float = None) -> None: |
| 53 | + timeout = convert_timeout(timeout) |
| 54 | + |
| 55 | + try: |
| 56 | + return await curio.timeout_after(timeout, self.semaphore.acquire()) |
| 57 | + except curio.TaskTimeout: |
| 58 | + raise self.exc_class() |
| 59 | + |
| 60 | + async def release(self) -> None: |
| 61 | + await self.semaphore.release() |
| 62 | + |
| 63 | + |
| 64 | +class SocketStream(AsyncSocketStream): |
| 65 | + def __init__(self, socket: curio.io.Socket) -> None: |
| 66 | + self.read_lock = curio.Lock() |
| 67 | + self.write_lock = curio.Lock() |
| 68 | + self.socket = socket |
| 69 | + self.stream = socket.as_stream() |
| 70 | + |
| 71 | + def get_http_version(self) -> str: |
| 72 | + if hasattr(self.socket, "_socket"): |
| 73 | + raw_socket = self.socket._socket |
| 74 | + |
| 75 | + if isinstance(raw_socket, SSLSocket): |
| 76 | + ident = raw_socket.selected_alpn_protocol() |
| 77 | + return "HTTP/2" if ident == "h2" else "HTTP/1.1" |
| 78 | + |
| 79 | + return "HTTP/1.1" |
| 80 | + |
| 81 | + async def start_tls( |
| 82 | + self, hostname: bytes, ssl_context: SSLContext, timeout: TimeoutDict |
| 83 | + ) -> "AsyncSocketStream": |
| 84 | + connect_timeout = convert_timeout(timeout.get("connect")) |
| 85 | + exc_map = { |
| 86 | + curio.TaskTimeout: ConnectTimeout, |
| 87 | + curio.CurioError: ConnectError, |
| 88 | + OSError: ConnectError, |
| 89 | + } |
| 90 | + |
| 91 | + with map_exceptions(exc_map): |
| 92 | + wrapped_sock = curio.io.Socket( |
| 93 | + ssl_context.wrap_socket( |
| 94 | + self.socket._socket, |
| 95 | + do_handshake_on_connect=False, |
| 96 | + server_hostname=hostname.decode("ascii"), |
| 97 | + ) |
| 98 | + ) |
| 99 | + |
| 100 | + await curio.timeout_after( |
| 101 | + connect_timeout, |
| 102 | + wrapped_sock.do_handshake(), |
| 103 | + ) |
| 104 | + |
| 105 | + return SocketStream(wrapped_sock) |
| 106 | + |
| 107 | + async def read(self, n: int, timeout: TimeoutDict) -> bytes: |
| 108 | + read_timeout = convert_timeout(timeout.get("read")) |
| 109 | + exc_map = { |
| 110 | + curio.TaskTimeout: ReadTimeout, |
| 111 | + curio.CurioError: ReadError, |
| 112 | + OSError: ReadError, |
| 113 | + } |
| 114 | + |
| 115 | + with map_exceptions(exc_map): |
| 116 | + async with self.read_lock: |
| 117 | + return await curio.timeout_after(read_timeout, self.stream.read(n)) |
| 118 | + |
| 119 | + async def write(self, data: bytes, timeout: TimeoutDict) -> None: |
| 120 | + write_timeout = convert_timeout(timeout.get("write")) |
| 121 | + exc_map = { |
| 122 | + curio.TaskTimeout: WriteTimeout, |
| 123 | + curio.CurioError: WriteError, |
| 124 | + OSError: WriteError, |
| 125 | + } |
| 126 | + |
| 127 | + with map_exceptions(exc_map): |
| 128 | + async with self.write_lock: |
| 129 | + await curio.timeout_after(write_timeout, self.stream.write(data)) |
| 130 | + |
| 131 | + async def aclose(self) -> None: |
| 132 | + await self.stream.close() |
| 133 | + await self.socket.close() |
| 134 | + |
| 135 | + def is_connection_dropped(self) -> bool: |
| 136 | + rready, _, _ = select.select([self.socket.fileno()], [], [], 0) |
| 137 | + |
| 138 | + return bool(rready) |
| 139 | + |
| 140 | + |
| 141 | +class CurioBackend(AsyncBackend): |
| 142 | + async def open_tcp_stream( |
| 143 | + self, |
| 144 | + hostname: bytes, |
| 145 | + port: int, |
| 146 | + ssl_context: Optional[SSLContext], |
| 147 | + timeout: TimeoutDict, |
| 148 | + *, |
| 149 | + local_address: Optional[str], |
| 150 | + ) -> AsyncSocketStream: |
| 151 | + connect_timeout = convert_timeout(timeout.get("connect")) |
| 152 | + exc_map = { |
| 153 | + curio.TaskTimeout: ConnectTimeout, |
| 154 | + curio.CurioError: ConnectError, |
| 155 | + OSError: ConnectError, |
| 156 | + } |
| 157 | + host = hostname.decode("ascii") |
| 158 | + kwargs = ( |
| 159 | + {} if ssl_context is None else {"ssl": ssl_context, "server_hostname": host} |
| 160 | + ) |
| 161 | + |
| 162 | + with map_exceptions(exc_map): |
| 163 | + sock: curio.io.Socket = await curio.timeout_after( |
| 164 | + connect_timeout, |
| 165 | + curio.open_connection(hostname, port, **kwargs), |
| 166 | + ) |
| 167 | + |
| 168 | + return SocketStream(sock) |
| 169 | + |
| 170 | + async def open_uds_stream( |
| 171 | + self, |
| 172 | + path: str, |
| 173 | + hostname: bytes, |
| 174 | + ssl_context: Optional[SSLContext], |
| 175 | + timeout: TimeoutDict, |
| 176 | + ) -> AsyncSocketStream: |
| 177 | + connect_timeout = convert_timeout(timeout.get("connect")) |
| 178 | + exc_map = { |
| 179 | + curio.TaskTimeout: ConnectTimeout, |
| 180 | + curio.CurioError: ConnectError, |
| 181 | + OSError: ConnectError, |
| 182 | + } |
| 183 | + host = hostname.decode("ascii") |
| 184 | + kwargs = ( |
| 185 | + {} if ssl_context is None else {"ssl": ssl_context, "server_hostname": host} |
| 186 | + ) |
| 187 | + |
| 188 | + with map_exceptions(exc_map): |
| 189 | + sock: curio.io.Socket = await curio.timeout_after( |
| 190 | + connect_timeout, curio.open_unix_connection(path, **kwargs) |
| 191 | + ) |
| 192 | + |
| 193 | + return SocketStream(sock) |
| 194 | + |
| 195 | + def create_lock(self) -> AsyncLock: |
| 196 | + return Lock() |
| 197 | + |
| 198 | + def create_semaphore(self, max_value: int, exc_class: type) -> AsyncSemaphore: |
| 199 | + return Semaphore(max_value, exc_class) |
| 200 | + |
| 201 | + async def time(self) -> float: |
| 202 | + return await curio.clock() |
0 commit comments