Best practices and design patterns for supporting concurrent IO in libraries

I would say that any recommendation would depend on what you are trying to achieve.

In the past, people have abstracted over the Lwt/Async/eio/etc difference by functorizing their libraries, but this complicates the implementation, and can get tricky if you need to go beyond the “formal” properties of each concurrency framework (since you need to come up with a signature common to all concurrency frameworks). An example of this approach is GitHub - mirage/ocaml-cohttp: An OCaml library for HTTP clients and servers using Lwt or Async.

The maximally flexible approach is to write your library in so that it does not do any I/O by itself, and instead asks the library user to do it on its behalf. Then, by construction, the library will be independent of any specific concurrency framework. The downside is that this often requires more work to design & implement and to develop the connectors to specific concurrency networks. An example of this approach is GitHub - mirleft/ocaml-tls: TLS in pure OCaml.

Cheers,
Nicolas

4 Likes