A disposable is a producer of values that allocates external resources when producing a value and provides a way to deallocate those resources when a produced value is no longer needed. A value produced by a disposable is called a disposable value.
Disposables are closely related to custodians, but differ in what sort of resources they manage and how those resources are reclaimed. Broadly speaking, resources requiring management can be divided into two categories:
External resources —
resources managed by arbitrary external systems that a program communicates with, such as test data in a database, network connections with graceful termination protocols, machines used to execute distributed data processing, temporary access credentials acquired from a remote key management service, etc.
These two kinds of resources have very different implications for programs that use them. Due to the distributed nature of external resources and the inherent unreliability of network communication, it is impossible to guarantee that a given external resource is released. However, system resources are very rarely impossible to release. Custodians are designed to mangage system resources and assume reliable reclamation is possible, placing several restrictions on how programs can use custodians as a result:
It must be possible to forcibly reclaim system resources during program termination as occurs when custodian-shutdown-all is called. This prevents reclaiming resources by communicating over a network because it’s impossible to guarantee successful distributed communication.
Placing a user-defined resource under the management of a custodian requires using the ffi/unsafe/custodian module. This is an unsafe API because custodian shutdown actions are executed in atomic mode, resulting in possible deadlocks if shutdown actions perform asynchronous IO.
Custodian shutdown callbacks normally should not strongly reference the values they’re meant to clean up, as these shutdown callbacks frequently double as finalizers that run when the garbage collector determines the value to finalize is no longer reachable. This defeats certain composition patterns for shutdown callbacks, particularly composition that uses closures to reference both composed callbacks and managed values.
These restrictions make managing external resources with custodians inappropriate. Instead, an external resource should be produced by a disposable resulting in a disposable value that can be safely managed in a distributed system.
Concretely, a disposable is implemented as a thunk that when called allocates a new disposable value and returns that value alongside another thunk that deallocates the external resources associated with that particular disposable value.
> (with-disposable ([x example-disposable] [y example-disposable]) (- x y))
> (with-disposable ([n example-disposable]) (error "uh oh!"))
> (call/disposable example-disposable (λ (n) (* n n)))
> (call/disposable example-disposable (λ (_) (error "uh oh!")))
disp : disposable? evt : evt? = (thread-dead-evt (current-thread))
disp : disposable? plumber : plumber? = (current-plumber)
> (define plumb (make-plumber)) > (define n (acquire-global example-disposable #:plumber plumb))
> (add1 n)
> (plumber-flush-all plumb)
> (define virtual-example (acquire-virtual example-disposable))
> (define (spawn) (thread (λ () (printf "Acquired ~v\n" (virtual-example))))) > (sync (spawn) (spawn) (spawn))
|(require disposable/unsafe)||package: disposable|
> (define-values (n dispose!) (acquire! example-disposable))
> (printf "Acquired ~v unsafely\n" n)
Acquired 45 unsafely
> (struct posn (x y) #:transparent)
> (define disposable-posn (disposable-apply posn example-disposable example-disposable))
> (with-disposable ([p disposable-posn]) (printf "Acquired ~v\n" p))
Acquired (posn 30 45)
> (define (add-example x) (disposable-apply (λ (y) (+ x y)) example-disposable)) > (define sum-disp (disposable-chain example-disposable add-example))
> (with-disposable ([v sum-disp]) (printf "Acquired ~v\n" v))
> (with-disposable ([n (disposable/async-dealloc example-disposable)]) (printf "Acquired ~v\n" n))
> (sleep 0.1)
Added in version 0.4 of package disposable.
(disposable/memoize f [ #:make-dict make-dict]) → (disposable/c procedure?) f : (unconstrained-domain-> disposable?) make-dict : (-> (and/c dict? dict-mutable?)) = make-hash
> (define (color+ex-disp c) (disposable-apply list (disposable-pure c) example-disposable))
> (with-disposable ([color+ex (disposable/memoize color+ex-disp)]) (displayln (color+ex "red")) (displayln (color+ex "blue")) (displayln (color+ex "red")))
Reuse of values by the memoized f is achieved by mapping arguments (both positional and keyword) to allocated values in a mutable dictionary. Allocation of the memoized f creates that mutable dictionary using make-dict, which by default produces a mutable hash table that holds keys and values strongly.
Added in version 0.5 of package disposable.