Source code for konnect.curl._multi

# Copyright 2023-2026  Dom Sekotill <dom.sekotill@kodo.org.uk>

from __future__ import annotations

from collections.abc import AsyncGenerator
from collections.abc import Iterator
from contextlib import asynccontextmanager
from typing import TYPE_CHECKING
from typing import Final
from typing import Literal

import anyio
import pycurl
from anyio.abc import ObjectReceiveStream
from anyio.abc import ObjectSendStream
from kodo.quantities import Quantity

from ._enums import COMPLETED
from ._enums import MILLISECONDS
from ._enums import SECONDS
from ._enums import SocketEvt
from ._enums import Time
from ._exceptions import CurlError
from ._streams import ReadStream
from ._streams import WriteStream
from .abc import Request
from .abc import UpdateHandler

if TYPE_CHECKING:
	import anyio.abc

type Event = tuple[Literal[SocketEvt.IN, SocketEvt.OUT], int]

INFO_READ_SIZE: Final = 10


[docs] class Multi: """ A wrapper around `pycurl.CurlMulti` to bind it with Anyio-supported event frameworks Users should treat this class as a black-box. Only the `process()`, `get_send_stream()` and `get_receive_stream()` methods are available. There are no other methods or attributes they should attempt to call or override as doing so will have undefined results. """ def __init__(self) -> None: self._handler = pycurl.CurlMulti() self._handler.setopt(pycurl.M_SOCKETFUNCTION, self._add_socket_evt) self._handler.setopt(pycurl.M_TIMERFUNCTION, self._add_timer_evt) self._io_events = dict[int, SocketEvt]() self._deadline: Quantity[Time] | None = None self._perform_cond = anyio.Condition() self._governor_delegated = False self._completed = dict[pycurl.Curl, int]() self._requests = dict[Request[object, object], pycurl.Curl]() def _add_socket_evt(self, what: int, socket: int, *_: object) -> None: # Callback registered with CURLMOPT_SOCKETFUNCTION, registers socket events the # transfer manager wants to be activated in response to. what: SocketEvt = SocketEvt(what) if what == SocketEvt.REMOVE: assert socket in self._io_events, f"file descriptor {socket} not in events" del self._io_events[socket] elif socket in self._io_events: self._io_events[socket] = what else: self._io_events[socket] = what def _add_timer_evt(self, delay: int) -> None: # Callback registered with CURLMOPT_TIMERFUNCTION, registers when the transfer # manager next wants to be activated if no prior events occur, in milliseconds. self._deadline = ( None if delay < 0 else anyio.current_time() @ SECONDS + delay @ MILLISECONDS ) async def _single_event(self) -> int: # Await a single event and call pycurl.CurlMulti.socket_action to inform the handler # of it, then return the number of active transfers # Shortcut if no events are registered, or the only event is an immediate timeout if not self._io_events and self._deadline is None: _, running = self._handler.socket_action(pycurl.SOCKET_TIMEOUT, 0) return running # Start concurrent tasks awaiting each of the registered events, await a response # from the first task to wake and send one. resp: Event | None = None async with ( anyio.create_task_group() as tasks, _make_evt_channel() as (sendchan, recvchan), ): for socket, evt in self._io_events.items(): if SocketEvt.IN in evt: tasks.start_soon(_wait_readable, socket, sendchan) if SocketEvt.OUT in evt: tasks.start_soon(_wait_writable, socket, sendchan) if self._deadline is not None: tasks.start_soon(_wait_until, self._deadline, sendchan) resp = await recvchan.receive() tasks.cancel_scope.cancel() # Call pycurl.CurlMulti.socket_action() with details of the received event, and # return how many active handles remain. match resp: case None: self._deadline = None _, running = self._handler.socket_action(pycurl.SOCKET_TIMEOUT, 0) case [SocketEvt.IN, int(fd)]: _, running = self._handler.socket_action(fd, pycurl.CSELECT_IN) case [SocketEvt.OUT, int(fd)]: _, running = self._handler.socket_action(fd, pycurl.CSELECT_OUT) return running def _get_handle(self, request: Request[object, object]) -> pycurl.Curl: try: return self._requests[request] except KeyError: handle = self._requests[request] = pycurl.Curl() request.configure_handle(handle) self._handler.add_handle(handle) return handle def _del_handle(self, request: Request[object, object]) -> None: handle = self._requests.pop(request) self._handler.remove_handle(handle) handle.close() def _yield_complete(self) -> Iterator[tuple[pycurl.Curl, int]]: # Convert pycurl.CurlMulti.info_read() output into tuples of (handle, code) and # iteratively yield them n_msgs = -1 while n_msgs: n_msgs, complete, failed = self._handler.info_read(INFO_READ_SIZE) yield from ((handle, pycurl.E_OK) for handle in complete) yield from ((handle, res) for (handle, res, _) in failed) async def _govern_transfer(self, updates: UpdateHandler, handle: pycurl.Curl) -> None: # Await _single_event() repeatedly until the wanted handle is completed. # Store all intermediate completed handles and notify interested tasks. remaining = -1 while remaining: remaining = await self._single_event() self._completed.update(self._yield_complete()) has_resp = updates.has_update() if not has_resp and not self._completed: continue async with self._perform_cond: self._perform_cond.notify_all() # This check needs to be atomic with the notification # (more specifically, the _governor_delegated flag needs to be unset # atomically) if has_resp or handle in self._completed: self._governor_delegated = False return # SHOULD NOT fall off the end of the loop, but don't want it to run infinitely if # something goes wrong, so ensure it has a completion condition and raise # AssertionError if it does complete raise AssertionError("no response detected after all handles processed") async def _process_updates[T]( self, updates: UpdateHandler[T], handle: pycurl.Curl ) -> T | COMPLETED: # Await and return an update event from the update handler, or return COMPLETED if # the curl handle completes without any further updates issued. # # A Request is a specific type of UpdateHandler that Multi.process() passes to this # method. Splitting this functionality from Multi.process() allows alternative # UpdateHandlers to be used for specific portions of a transfer process; for # example, write updates. while handle not in self._completed: # If no task is governing the transfer, self-delegate the role to ourselves and # govern transfers until an update is passed back through 'updates'. if not self._governor_delegated: self._governor_delegated = True try: await self._govern_transfer(updates, handle) finally: self._governor_delegated = False # Otherwise await a notification of completed handles else: async with self._perform_cond: await self._perform_cond.wait() if updates.has_update(): return updates.get_update() return COMPLETED
[docs] async def process[UpdateT, ResultT]( self, request: Request[UpdateT, ResultT] ) -> UpdateT | ResultT: """ Perform a request as described by a Curl instance """ if request.has_update(): return request.get_update() handle = self._get_handle(request) if (update := await self._process_updates(request, handle)) is not COMPLETED: return update assert handle in self._completed match self._completed.pop(handle): case pycurl.E_OK: self._del_handle(request) return request.completed(handle) case err: assert isinstance(err, int) # nudge mypy errstr = handle.errstr() self._del_handle(request) raise CurlError(err, errstr)
[docs] def get_send_stream(self, request: Request) -> anyio.abc.ByteSendStream: """ Return a writable bytes stream for the upload portion of a transfer The returned stream object implements `anyio.abc.ByteSendStream`. """ handle = self._get_handle(request) return WriteStream(self._process_updates, handle)
[docs] def get_receive_stream(self, request: Request) -> anyio.abc.ByteReceiveStream: """ Return a readable bytes stream for the download portion of a transfer The returned stream object implements `anyio.abc.ByteReceiveStream`. """ handle = self._get_handle(request) return ReadStream(self._process_updates, handle)
@asynccontextmanager async def _make_evt_channel() -> AsyncGenerator[ tuple[ObjectSendStream[Event | None], ObjectReceiveStream[Event | None]] ]: send, recv = anyio.create_memory_object_stream[Event | None](1) async with send, recv: yield send, recv async def _wait_readable(fd: int, channel: ObjectSendStream[Event]) -> None: await anyio.wait_readable(fd) await channel.send((SocketEvt.IN, fd)) async def _wait_writable(fd: int, channel: ObjectSendStream[Event]) -> None: await anyio.wait_writable(fd) await channel.send((SocketEvt.OUT, fd)) async def _wait_until(time: Quantity[Time], channel: ObjectSendStream[None]) -> None: await anyio.sleep_until(time >> SECONDS) await channel.send(None)