field_ios

In-the-field app for Radroots on iOS
git clone https://radroots.dev/git/field_ios.git
Log | Files | Refs | README | LICENSE

fixture_connections.py (4555B)


      1 """Elapsed read bounds and ownership for the isolated loopback fixture."""
      2 
      3 from __future__ import annotations
      4 
      5 import http.server
      6 import io
      7 import socket
      8 import threading
      9 import time
     10 
     11 READ_SECONDS = 15
     12 TEARDOWN_SECONDS = 5
     13 MAX_HANDLERS = 32
     14 
     15 
     16 def deadline() -> float:
     17     return time.monotonic() + READ_SECONDS
     18 
     19 
     20 def remaining(until: float) -> float:
     21     value = until - time.monotonic()
     22     if value <= 0:
     23         raise TimeoutError("fixture read deadline exceeded")
     24     return value
     25 
     26 
     27 def recv_exact(stream, length: int, until=None) -> bytes:
     28     until = deadline() if until is None else until
     29     value = bytearray()
     30     while len(value) < length:
     31         stream.settimeout(remaining(until))
     32         chunk = stream.recv(length - len(value))
     33         if not chunk:
     34             raise ConnectionError("socket closed")
     35         value.extend(chunk)
     36     return bytes(value)
     37 
     38 
     39 def recv_until(stream, marker: bytes, maximum: int) -> bytes:
     40     until = deadline()
     41     value = bytearray()
     42     while marker not in value:
     43         stream.settimeout(remaining(until))
     44         chunk = stream.recv(4096)
     45         if not chunk:
     46             raise ConnectionError("socket closed")
     47         value.extend(chunk)
     48         if len(value) > maximum:
     49             raise ValueError("request too large")
     50     return bytes(value)
     51 
     52 
     53 class DeadlineReader(io.RawIOBase):
     54     def __init__(self, stream: socket.socket, until: float):
     55         super().__init__()
     56         self.stream, self.until = stream, until
     57 
     58     def readable(self) -> bool:
     59         return True
     60 
     61     def readinto(self, buffer) -> int:
     62         self.stream.settimeout(remaining(self.until))
     63         return self.stream.recv_into(buffer)
     64 
     65 
     66 def http_reader(stream: socket.socket) -> io.BufferedReader:
     67     return io.BufferedReader(DeadlineReader(stream, deadline()))
     68 
     69 
     70 class OwnedConnectionsMixin:
     71     """Register work before starting it, then close and join only owned work."""
     72 
     73     max_handlers = MAX_HANDLERS
     74 
     75     def __init__(self, *args, **kwargs):
     76         self._owned_lock = threading.Lock()
     77         self._owned = {}
     78         self._handler_threads = set()
     79         self._closing = False
     80         super().__init__(*args, **kwargs)
     81 
     82     @property
     83     def active_handlers(self) -> int:
     84         with self._owned_lock:
     85             return len(self._owned)
     86 
     87     def process_request(self, request, client_address):
     88         with self._owned_lock:
     89             if self._closing or len(self._owned) >= self.max_handlers:
     90                 self.shutdown_request(request)
     91                 return
     92             thread = threading.Thread(
     93                 target=self._owned_request, args=(request, client_address), daemon=True
     94             )
     95             self._owned[request] = thread
     96             self._handler_threads = {
     97                 item for item in self._handler_threads if item.is_alive()
     98             }
     99             self._handler_threads.add(thread)
    100             try:
    101                 thread.start()
    102             except BaseException:
    103                 self._owned.pop(request)
    104                 self._handler_threads.remove(thread)
    105                 self.shutdown_request(request)
    106                 raise
    107 
    108     def _owned_request(self, request, client_address):
    109         try:
    110             try:
    111                 self.finish_request(request, client_address)
    112             except (ConnectionError, TimeoutError):
    113                 pass
    114             except OSError:
    115                 if not self._closing:
    116                     self.handle_error(request, client_address)
    117             except Exception:
    118                 self.handle_error(request, client_address)
    119         finally:
    120             self.shutdown_request(request)
    121             with self._owned_lock:
    122                 self._owned.pop(request, None)
    123 
    124     def server_close(self):
    125         until = time.monotonic() + TEARDOWN_SECONDS
    126         with self._owned_lock:
    127             self._closing = True
    128             owned = list(self._owned.items())
    129             threads = list(self._handler_threads)
    130         try:
    131             super().server_close()
    132             for request, _ in owned:
    133                 try:
    134                     request.shutdown(socket.SHUT_RDWR)
    135                 except OSError:
    136                     pass
    137             for thread in threads:
    138                 thread.join(max(0, until - time.monotonic()))
    139         finally:
    140             for request, _ in owned:
    141                 request.close()
    142         if any(thread.is_alive() for thread in threads):
    143             raise RuntimeError("fixture owned handler teardown is incomplete")
    144 
    145 
    146 class OwnedHTTPServer(OwnedConnectionsMixin, http.server.ThreadingHTTPServer):
    147     pass