Resource management with co.
Features
- automatic allocation and destruction of resources
- co/koa friendly
- easy schema for resource management
- builtin synchronization resources - mutex, semaphore and read/write lock
// actual implementation from using.js { return }
Example
let using = // Define how connections are created and destroyedlet connectionPool = { return } { connection } // Use a connection, ensuring it gets destroyed
Api
<resource manager> <co 4 compatible generator>
A resource manager is any object implementing acquire
and release
.
The result of acquire
should be a thenable-
let resourceManager = { return Promise } { ... }
Extras
Mutexes (mutual exclusion), semaphores (limited parallel access) and read write locks are synchronization primitives that follows the acquire/release
pattern very well. For your convenience, they are all included.
All primitives can be named and optionally scoped to a naming container. This is useful when you want to control access to resources not known in advance, such as files or database records. Memory is reclaimed for named resources not active in any execution path.
Use
DEBUG=co-using:* node --harmony <...>
to get debug output.
Mutex
Mutually exclusive access to generator code.
let using = let mutex = let m =
A mutex can also be (globally) named,
let m =
A mutex can also be named in a specific scope,
let mutexNamingScope = {}let m =
Semaphore
Limited parallel access to generator code.
let using = let semaphore = let sem =
A semaphore can also be (globally) named,
let sem =
A semaphore can also be named in a specific scope,
let semaphoreNamingScope = {}let sem =
Read/Write lock
Parallel access to generator code, with the following restrictions
- if any writer is active, no other readers or writers can be active
- if any reader is active, only other readers up to specified limit can be active
- scheduling is fair, i.e. access is granted in the requested order
let using = let rwlock = let l =
It is possible to specify max concurrency (default is many) of readers (note that for writers this is always 1).
let l =
A read/write lock can also be (globally) named,
let l =
A read/write lock can also be named in a specific scope,
let lockNamingScope = {}let lock =