jackfirth / racket-disposable

An experimental Racket library providing an abstraction for values associated with external resources that allows automatic resource pooling, per-thread virtual construction, and monadic composition
Apache License 2.0
7 stars 0 forks source link

Virtual disposables #2

Closed jackfirth closed 7 years ago

jackfirth commented 7 years ago

Given a disposable value, it should be possible to create a virtual value - a thunk that when called returns a thread-specific instance of the disposable that is automatically deallocated when the calling thread dies. Combined with #1, this should make it possible to generalize the very nice behavior of the db library's virtual connections backed by connection pools. The pattern would be as follows:

  1. Define a disposable representing the connection
  2. Define a pooled disposable wrapping the connection disposable
  3. Globally provide the pooled disposable, using a plumber to deallocate the pool on shutdown (note that pooled disposables are double-wrapped, the outer disposable represents the lifetime of the pool while the inner disposable represents the lifetime of a lease of a pooled value)
  4. Virtually provide a wrapper around the pool's lease disposable

Voila! After step 4, there should be a thunk that implements the behavior of virtual-connection for an arbitrary disposable. Calling the thunk returns a thread-specific instance of the disposable value without needing with-disposable, and after the thread dies the thread-specific instance is returned to the global disposable pool. At program shutdown, all disposables in the pool are deallocated.