Protocol-agnostic TCP/IP connection pooling for Eio forked from anil.recoil.org/ocaml-conpool
README.md

conpool #

Connection pooling for Eio, for any protocol that runs over TCP or TLS.

Opening a TCP connection costs a round trip, and a TLS handshake one or two
more, so a client that talks to the same server repeatedly should keep its
connections open and reuse them. conpool keeps a pool of connections per
endpoint (a host and a port), dials them with Happy Eyeballs (RFC 8305) and
optional TLS. Happy Eyeballs is the rule for a host that has both IPv6 and
IPv4 addresses: start a connection attempt to the first address, start the
next one in parallel if the first has not connected after a short delay
(250 ms recommended), alternating families, and keep whichever connects
first, so a broken IPv6 path costs a fraction of a second instead of a full
timeout. It retries a failed dial with exponential backoff, and closes a
connection once it is too old, has sat idle too long or has served too many
borrows. The pool does not know the protocol. A protocol handler creates the
state of each connection, says whether the connection is still healthy, and
chooses between exclusive access (one borrower at a time, as in HTTP/1.x or
Redis) and shared access (several borrowers at once, as the streams of HTTP/2).

Two limits matter in practice. A borrower that finds its endpoint at the
connection limit waits without a bound for a release. Config.v also accepts
a health_check function and three connection hooks, which the pool never
calls.

Install #

$ opam install conpool

If opam cannot find the package, add the overlay repository first:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install conpool

Usage #

A basic pool dials plain TCP. Conpool.connection borrows a connection until
the switch it is given finishes:

open Eio.Std

let run env =
  Switch.run (fun sw ->
    (* Create a basic connection pool (no protocol state) *)
    let pool =
      Conpool.basic
      ~sw
      ~net:(Eio.Stdenv.net env)
      ~clock:(Eio.Stdenv.clock env)
      ()
    in

    (* Define an endpoint *)
    let endpoint = Conpool.Endpoint.v ~host:"example.com" ~port:80 in
    (* Use a connection from the pool *)
    Eio.Switch.run (fun conn_sw ->
      let conn = Conpool.connection ~sw:conn_sw pool endpoint in
      Eio.Flow.copy_string "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n"
        conn.flow;
      let buf = Eio.Buf_read.of_flow conn.flow ~max_size:4096 in
      Eio.Buf_read.take_all buf))

With a TLS configuration, every connection of the pool runs TLS, with the
endpoint's host as the server name:

open Eio.Std

let run env =
  Switch.run (fun sw ->
    (* Create TLS configuration - SNI servername is automatically set to the endpoint's hostname *)
    let authenticator = Result.get_ok (Ca_certs.authenticator ()) in
    let tls_config = Result.get_ok (Tls.Config.client ~authenticator ()) in
    (* Create pool with TLS *)
    let pool =
      Conpool.basic
      ~sw
      ~net:(Eio.Stdenv.net env)
      ~clock:(Eio.Stdenv.clock env)
      ~tls:tls_config
      ()
    in

    let endpoint = Conpool.Endpoint.v ~host:"example.com" ~port:443 in
    Eio.Switch.run (fun conn_sw ->
      let conn = Conpool.connection ~sw:conn_sw pool endpoint in
      (* Use TLS-encrypted conn.flow *)
      ignore conn.flow))

The limits and timeouts come from Conpool.Config.v, or from the command line
through the Cmdliner term Conpool.Cmd.config:

let make_pool ~sw ~net ~clock =
  let config =
    Conpool.Config.v
      ~max_connections_per_endpoint:20
      ~max_idle_time:60.0
      ~max_connection_lifetime:300.0
      ~connect_timeout:10.0
      ()
  in
  Conpool.basic ~sw ~net ~clock ~config ()

Each endpoint keeps counters:

let print_stats pool endpoint =
  let stats = Conpool.stats pool endpoint in
  Printf.printf "Active: %d, Idle: %d, Created: %d, Reused: %d\n"
    (Conpool.Stats.active stats)
    (Conpool.Stats.idle stats)
    (Conpool.Stats.total_created stats)
    (Conpool.Stats.total_reused stats)

A connection taken from the idle set may have been closed by the peer, since
the pool never probes the socket. The reused field of a borrowed connection
says so, and its invalidate function closes a connection the caller knows to
be broken and frees its slot at once.

License #

ISC. See LICENSE.md.