package lwt
Install
    
    dune-project
 Dependency
Authors
Maintainers
Sources
md5=18742da8b8fe3618e3fa700b7a884fe7
    
    
  sha512=1c51fdb4d0856c89e2df08a1c0095ef28ebd0f613b07b03d0f66501ca5486515562071291e6d0932e57587ed0c9362c8b92c5c9eddb4d2bb2f5e129986b484a7
    
    
  doc/lwt/Lwt_pool/index.html
Module Lwt_poolSource
External resource pools.
This module provides an abstraction for managing collections of resources. One example use case is for managing a pool of database connections, where instead of establishing a new connection each time you need one (which is expensive), you can keep a pool of opened connections and reuse ones that are free.
It also provides the capability of:
- specifying the maximum number of resources that the pool can manage simultaneously,
 - checking whether a resource is still valid before/after use, and
 - performing cleanup logic before dropping a resource.
 
The following example illustrates how it is used with an imaginary Db module:
let uri = "postgresql://localhost:5432"
(* Create a database connection pool with max size of 10. *)
let pool =
  Lwt_pool.create 10
    ~dispose:(fun connection -> Db.close connection |> Lwt.return)
    (fun () -> Db.connect uri |> Lwt.return)
(* Use the pool in queries. *)
let create_user name =
  Lwt_pool.use pool (fun connection ->
      connection
      |> Db.insert "users" [("name", name)]
      |> Lwt.return
    )Note that this is not intended to keep a pool of system threads. If you want to have such pool, consider using Lwt_preemptive.
A pool containing elements of type 'a.
val create : 
  int ->
  ?validate:('a -> bool Lwt.t) ->
  ?check:('a -> (bool -> unit) -> unit) ->
  ?dispose:('a -> unit Lwt.t) ->
  (unit -> 'a Lwt.t) ->
  'a tcreate n ?check ?validate ?dispose f creates a new pool with at most n elements. f is used to create a new pool element. Elements are created on demand and re-used until disposed of.
use p f requests one free element of the pool p and gives it to the function f. The element is put back into the pool after the promise created by f completes.
In the case that p is exhausted and the maximum number of elements is reached, use will wait until one becomes free.
clear p will clear all elements in p, calling the dispose function associated with p on each of the cleared elements. Any elements from p which are currently in use will be disposed of once they are released.
The next call to use p after clear p guarantees a freshly created pool element.
Disposals are performed sequentially in an undefined order.