Class: ResourceRentalPool
resource-rental/resource-rental-pool.ResourceRentalPool
Pool of resource rentals that can be borrowed, released or destroyed.
Table of contents
Constructors
Properties
Methods
Constructors
constructor
• new ResourceRentalPool(options
): ResourceRentalPool
Parameters
Name | Type |
---|---|
options | ResourceRentalPoolOptions & ResourceRentalPoolDependencies |
Returns
Defined in
src/resource-rental/resource-rental-pool.ts:96
Properties
events
• Readonly
events: EventEmitter
<ResourceRentalPoolEvents
, any
>
Defined in
src/resource-rental/resource-rental-pool.ts:59
Methods
acquire
▸ acquire(signalOrTimeout?
): Promise
<ResourceRental
>
Borrow a resource rental from the pool. If there is no valid resource rental a new one will be created.
Parameters
Name | Type | Description |
---|---|---|
signalOrTimeout? | number | AbortSignal | the timeout in milliseconds or an AbortSignal that will be used to cancel the rental request |
Returns
Promise
<ResourceRental
>
Defined in
src/resource-rental/resource-rental-pool.ts:216
release
▸ release(resourceRental
): Promise
<void
>
Parameters
Name | Type |
---|---|
resourceRental | ResourceRental |
Returns
Promise
<void
>
Defined in
src/resource-rental/resource-rental-pool.ts:255
destroy
▸ destroy(resourceRental
): Promise
<void
>
Parameters
Name | Type |
---|---|
resourceRental | ResourceRental |
Returns
Promise
<void
>
Defined in
src/resource-rental/resource-rental-pool.ts:272
drainAndClear
▸ drainAndClear(): Promise
<void
>
Sets the pool into draining mode and then clears it
When set to drain mode, no new acquires will be possible. At the same time, all agreements in the pool will be terminated with the Providers.
Returns
Promise
<void
>
Resolves when all agreements are terminated
Defined in
src/resource-rental/resource-rental-pool.ts:327
getSize
▸ getSize(): number
Total size (available + borrowed)
Returns
number
Defined in
src/resource-rental/resource-rental-pool.ts:340
getAvailableSize
▸ getAvailableSize(): number
Available size (how many resource rental are ready to be borrowed)
Returns
number
Defined in
src/resource-rental/resource-rental-pool.ts:347
getBorrowedSize
▸ getBorrowedSize(): number
Borrowed size (how many resource rental are currently out of the pool)
Returns
number
Defined in
src/resource-rental/resource-rental-pool.ts:354
ready
▸ ready(timeoutMs?
): Promise
<void
>
Wait till the pool is ready to use (min number of items in pool are usable). If an error occurs while creating new resource rentals, it will be retried until the pool is ready (potentially indefinitely). To stop this process if it fails to reach the desired state in a given time, you can pass either a timeout in milliseconds or an AbortSignal.
Parameters
Name | Type |
---|---|
timeoutMs? | number |
Returns
Promise
<void
>
Example
await pool.ready(10_000); // If the pool is not ready in 10 seconds, an error will be thrown
Example
await pool.ready(AbortSignal.timeout(10_000)); // If the pool is not ready in 10 seconds, an error will be thrown
Defined in
src/resource-rental/resource-rental-pool.ts:373
▸ ready(abortSignal?
): Promise
<void
>
Parameters
Name | Type |
---|---|
abortSignal? | AbortSignal |
Returns
Promise
<void
>
Defined in
src/resource-rental/resource-rental-pool.ts:374
withRental
▸ withRental<T
>(callback
, signalOrTimeout?
): Promise
<T
>
Acquire a resource rental from the pool and release it after the callback is done
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
callback | (rental : ResourceRental ) => Promise <T > | a function that takes a rental object as its argument. The rental is automatically released after the callback is executed, regardless of whether it completes successfully or throws an error. |
signalOrTimeout? | number | AbortSignal | the timeout in milliseconds or an AbortSignal that will be used to cancel the rental request |
Returns
Promise
<T
>
Example
const result = await pool.withRental(async (rental) => {
// Do something with the rented resources
return result;
// pool.release(rental) is called automatically
// even if an error is thrown in the callback
});