TCP/TLS connection pooling for Eio
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

ocaml-linkedin: apply dune fmt

Pure formatting changes from `dune fmt`: doc comment placement moves
from above the binding to below it for `type`s, multi-line `match`
expressions collapse onto one line where they fit, and infix operator
applications pick up spaces (`Soup.($?)` -> `Soup.( $? )`). No
semantic changes.

+48 -28
+41 -28
README.md
··· 21 21 let run env = 22 22 Switch.run (fun sw -> 23 23 (* Create a basic connection pool (no protocol state) *) 24 - let pool = Conpool.basic 24 + let pool = 25 + Conpool.basic 25 26 ~sw 26 27 ~net:(Eio.Stdenv.net env) 27 28 ~clock:(Eio.Stdenv.clock env) ··· 30 31 31 32 (* Define an endpoint *) 32 33 let endpoint = Conpool.Endpoint.v ~host:"example.com" ~port:80 in 33 - 34 34 (* Use a connection from the pool *) 35 35 Eio.Switch.run (fun conn_sw -> 36 36 let conn = Conpool.connection ~sw:conn_sw pool endpoint in 37 - Eio.Flow.copy_string "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n" conn.flow; 37 + Eio.Flow.copy_string "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n" 38 + conn.flow; 38 39 let buf = Eio.Buf_read.of_flow conn.flow ~max_size:4096 in 39 - Eio.Buf_read.take_all buf 40 - ) 41 - ) 40 + Eio.Buf_read.take_all buf)) 42 41 ``` 43 42 44 43 With TLS configuration: 45 44 46 45 ```ocaml 46 + open Eio.Std 47 + 47 48 let run env = 48 49 Switch.run (fun sw -> 49 50 (* Create TLS configuration - SNI servername is automatically set to the endpoint's hostname *) 50 - let tls_config = Tls.Config.client ~authenticator:(Ca_certs.authenticator ()) () in 51 - 51 + let authenticator = Result.get_ok (Ca_certs.authenticator ()) in 52 + let tls_config = Result.get_ok (Tls.Config.client ~authenticator ()) in 52 53 (* Create pool with TLS *) 53 - let pool = Conpool.basic 54 + let pool = 55 + Conpool.basic 54 56 ~sw 55 57 ~net:(Eio.Stdenv.net env) 56 58 ~clock:(Eio.Stdenv.clock env) ··· 62 64 Eio.Switch.run (fun conn_sw -> 63 65 let conn = Conpool.connection ~sw:conn_sw pool endpoint in 64 66 (* Use TLS-encrypted conn.flow *) 65 - ignore conn.flow 66 - ) 67 - ) 67 + ignore conn.flow)) 68 68 ``` 69 69 70 70 Custom pool configuration: 71 71 72 72 ```ocaml 73 - let config = Conpool.Config.v 74 - ~max_connections_per_endpoint:20 75 - ~max_idle_time:60.0 76 - ~max_connection_lifetime:300.0 77 - ~connect_timeout:10.0 78 - () 79 - in 80 - 81 - let pool = Conpool.basic ~sw ~net ~clock ~config () 73 + let make_pool ~sw ~net ~clock = 74 + let config = 75 + Conpool.Config.v 76 + ~max_connections_per_endpoint:20 77 + ~max_idle_time:60.0 78 + ~max_connection_lifetime:300.0 79 + ~connect_timeout:10.0 80 + () 81 + in 82 + Conpool.basic ~sw ~net ~clock ~config () 82 83 ``` 83 84 84 85 Monitor pool statistics: 85 86 86 87 ```ocaml 87 - let stats = Conpool.stats pool endpoint in 88 - Printf.printf "Active: %d, Idle: %d, Created: %d, Reused: %d\n" 89 - (Conpool.Stats.active stats) 90 - (Conpool.Stats.idle stats) 91 - (Conpool.Stats.total_created stats) 92 - (Conpool.Stats.total_reused stats) 88 + let print_stats pool endpoint = 89 + let stats = Conpool.stats pool endpoint in 90 + Printf.printf "Active: %d, Idle: %d, Created: %d, Reused: %d\n" 91 + (Conpool.Stats.active stats) 92 + (Conpool.Stats.idle stats) 93 + (Conpool.Stats.total_created stats) 94 + (Conpool.Stats.total_reused stats) 93 95 ``` 94 96 95 97 ## Installation 96 98 99 + Install with opam: 100 + 101 + ```sh 102 + $ opam install conpool 97 103 ``` 98 - opam install conpool 104 + 105 + If opam cannot find the package, it may not yet be released in the public 106 + `opam-repository`. Add the overlay repository, then install it: 107 + 108 + ```sh 109 + $ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git 110 + $ opam update 111 + $ opam install conpool 99 112 ``` 100 113 101 114 ## Documentation
+1
conpool.opam
··· 17 17 "logs" 18 18 "fmt" 19 19 "cmdliner" 20 + "mdx" {with-test} 20 21 "odoc" {with-doc} 21 22 ] 22 23 build: [
+4
dune
··· 1 1 (env 2 2 (dev 3 3 (flags :standard %{dune-warnings}))) 4 + 5 + (mdx 6 + (files README.md) 7 + (libraries conpool eio eio.core eio.unix ca-certs tls))
+2
dune-project
··· 1 1 (lang dune 3.21) 2 + (using mdx 0.4) 2 3 (name conpool) 3 4 4 5 (generate_opam_files true) ··· 21 22 logs 22 23 fmt 23 24 cmdliner 25 + (mdx :with-test) 24 26 (odoc :with-doc)))