lazy-bracket: A bracket with lazy resource allocation.

[ bsd3, control, library ] [ Propose Tags ] [ Report a vulnerability ]

A version of bracket which returns a lazily allocated resource. The allocation happens when (if) the resource is accessed for the first time.

Some control operations can be stashed and applied once the resource is allocated, or skipped altogether if the resource is never allocated.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0
Change log CHANGELOG.md
Dependencies base (>=4.10.0.0 && <5), exceptions (>=0.10 && <0.11) [details]
License BSD-3-Clause
Author Daniel Diaz
Maintainer diaz_carrete@yahoo.com
Category Control
Source repo head: git clone https://github.com/danidiaz/lazy-bracket.git
Uploaded by DanielDiazCarrete at 2022-04-16T21:01:14Z
Distributions
Downloads 106 total (4 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2022-04-16 [all 1 reports]

Readme for lazy-bracket-0.1.0.0

[back to package description]

lazy-bracket

Sometimes, when bracketing some piece of code, the resource acquired by the bracket won't be actually used:

  • Finding a result in an in-memory cache can mean that a database query is avoided, and the database connection stays untouched.

  • You might be providing some resource (again, think database connection) to every REST endpoint handler in your API, even if some handlers don't make use of the resource, because treating these handlers as special cases would be tedious.

In order to be more frugal and avoid unnecessary resource acquisitions, one possible approach is to delay the acquisition to the first time the resource is actually used, if it's used at all.

What's more, certain "control" operations on the resource don't need to be applied immediately, and instead can wait until the resource is eventually acquired, or be omitted altogether if the resource isn't acquired:

  • It's wasteful to acquire a file handle just to perform hSetBuffering, if we are not going to write to the handle.

  • It's wasteful to acquire a database connection just to begin a transaction, if we aren't going to perform any query.

This package provides lazy versions of bracket for which resource acquisition is delayed until first use, and control operations are only applied once resources are acquired.

  • The inspiration: LazyConnectionDataSourceProxy form Java's Spring Framework.

    Proxy for a target DataSource, fetching actual JDBC Connections lazily, i.e. not until first creation of a Statement. Connection initialization properties like auto-commit mode, transaction isolation and read-only mode will be kept and applied to the actual JDBC Connection as soon as an actual Connection is fetched (if ever). Consequently, commit and rollback calls will be ignored if no Statements have been created.

    This DataSource proxy allows to avoid fetching JDBC Connections from a pool unless actually necessary. JDBC transaction control can happen without fetching a Connection from the pool or communicating with the database; this will be done lazily on first creation of a JDBC Statement.