Resource Pool
pool?
make-pool
call-with-pool-resource
pool-take!
pool-release!
pool-close!
exn:  fail:  pool?
8.12

Resource Pool🔗ℹ

Bogdan Popa <bogdan@defn.io>

This module provides a generic blocking resource pool implementation. Useful for managing things such as database and HTTP connections.

procedure

(pool? v)  boolean?

  v : any/c

procedure

(make-pool make-resource    
  [destroy-resource    
  #:max-size max-size    
  #:idle-ttl idle-ttl])  pool?
  make-resource : (-> any/c)
  destroy-resource : (-> any/c void?) = void
  max-size : exact-positive-integer? = 8
  idle-ttl : (or/c +inf.0 exact-positive-integer?)
   = (* 3600 1000)
The make-pool function returns a new resource pool that lazily creates new resources using make-resource. The resulting pool can contain up to #:max-size resources.

The #:idle-ttl argument controls how long a resource can remain idle before destroy-resource is applied to it and it is removed from the pool.

procedure

(call-with-pool-resource p    
  f    
  #:timeout timeout)  any/c
  p : pool?
  f : (-> any/c any)
  timeout : (or/c #f exact-nonnegative-integer?)
Leases a resource from p and applies f to it, returning the leased value back into the pool once f finishes executing.

The #:timeout behaves the same as in pool-take!, except that if the timeout is hit, an exn:fail:pool? is raised and #f is not executed.

procedure

(pool-take! p [timeout])  (or/c #f any/c)

  p : pool?
  timeout : (or/c #f exact-nonnegative-integer?) = #f
Waits for a resource to become available and then leases it from p. If the timeout argument is provided, the function will block for at most timeout milliseconds before returning. On timeout, #f is returned.

procedure

(pool-release! p v)  void?

  p : pool?
  v : any/c
Releases v back into p. If v was not leased from p, then an exn:fail:pool? error is raised.

procedure

(pool-close! p)  void?

  p : pool?
Closes p. If pool-close is called before all of the leased resources have been returned to the pool, an exn:fail:pool? error is raised and the pool remains open.

Raises an error if p has already been closed.

procedure

(exn:fail:pool? v)  boolean?

  v : any/c
Returns #t when v is a resource pool error.