Fetch.Transport (Fetch v0.1.0)

View Source

Moves bytes between us and the server: DNS, TCP and TLS.

A connected transport is a tuple {module, socket} where module is :gen_tcp or :ssl. Both modules have the same send/2, recv/3 and close/1, so the rest of the client does not care which one it talks to.

connect/2
  ├─ DNS   :inet.getaddrs/3        "example.com" → [{93, 184, 215, 14}]
  ├─ TCP   :gen_tcp.connect/4      try each address in order
  └─ TLS   :ssl.connect/3          upgrade the TCP socket (https only)

Sockets are in passive mode (active: false): data waits in the socket until recv/2 asks for it, so no process messages are involved.

Summary

Functions

Checks, without waiting, that an idle connection can be reused: still open and nothing unread. A server that closed it, or sent something on its own (like 408 Request Timeout before closing), makes it unusable.

Returns whatever bytes are available, waiting at most timeout ms for them. {:error, {:recv, :closed}} means the server closed the connection.

Types

t()

@type t() :: {:gen_tcp, :gen_tcp.socket()} | {:ssl, :ssl.sslsocket()}

Functions

close(arg)

@spec close(t()) :: :ok

connect(map, opts)

@spec connect(
  Fetch.URL.t(),
  keyword()
) :: {:ok, t()} | {:error, {:dns | :connect | :tls, term()}}

idle?(arg)

@spec idle?(t()) :: boolean()

Checks, without waiting, that an idle connection can be reused: still open and nothing unread. A server that closed it, or sent something on its own (like 408 Request Timeout before closing), makes it unusable.

recv(arg, timeout)

@spec recv(t(), timeout()) :: {:ok, binary()} | {:error, {:recv, term()}}

Returns whatever bytes are available, waiting at most timeout ms for them. {:error, {:recv, :closed}} means the server closed the connection.

send(arg, data)

@spec send(t(), iodata()) :: :ok | {:error, {:send, term()}}