Lightweight 0.20260921.0
Loading...
Searching...
No Matches
Lightweight::Pool< Config > Class Template Reference

#include <Pool.hpp>

Classes

class  PooledDataMapper
 

Public Member Functions

 Pool ()
 
 ~Pool () noexcept
 
 Pool (Pool const &)=delete
 
Pool & operator= (Pool const &)=delete
 
 Pool (Pool &&)=delete
 
Pool & operator= (Pool &&)=delete
 
PooledDataMapper Acquire ()
 
std::expected< PooledDataMapper, PoolError > Acquire (std::chrono::milliseconds timeout)
 
PooledDataMapper Acquire ()
 
std::expected< PooledDataMapper, PoolError > Acquire (std::chrono::milliseconds timeout)
 
Async::Task< PooledDataMapper > AcquireAsync (Async::IExecutor &dbWorkers, Async::IResumeScheduler &resume)
 
void SetAsyncExecutors (Async::IExecutor &dbWorkers, Async::IResumeScheduler &resume) noexcept
 
Async::Task< PooledDataMapper > AcquireAsync ()
 
void SetClock (std::function< Clock::time_point()> clock) noexcept
 

Detailed Description

template<PoolConfig Config>
class Lightweight::Pool< Config >

A thread-safe pool of DataMapper instances with the policy configured by the PoolConfig template parameter. The pool allows acquiring and returning DataMapper instances, and manages the lifecycle of these instances according to the specified growth strategy.

Definition at line 164 of file Pool.hpp.

Constructor & Destructor Documentation

◆ Pool()

template<PoolConfig Config>
Lightweight::Pool< Config >::Pool ( )
inlineexplicit

Default constructor that pre-creates the initial number of data mappers and stores them in the pool No other constructors are provided, as the pool is configured at compile time via the template parameter

Definition at line 525 of file Pool.hpp.

◆ ~Pool()

template<PoolConfig Config>
Lightweight::Pool< Config >::~Pool ( )
inlinenoexcept

Destructor. The pool manages the lifecycle of the idle data mappers; be aware that any acquired data mappers not returned to the pool are destroyed when the pool is destroyed, which may leak resources if not handled properly.

Definition at line 535 of file Pool.hpp.

References Lightweight::SqlLogger::GetLogger(), and Lightweight::SqlLogger::OnWarning().

Member Function Documentation

◆ Acquire() [1/4]

template<PoolConfig Config>
PooledDataMapper Lightweight::Pool< Config >::Acquire ( )
inline

Function to acquire a data mapper from the pool, the behavior of this function depends on the growth strategy this is a specific implementation for the BoundedWait strategy, which blocks until a data mapper is available if the pool is at maximum capacity

Prefer the Acquire(std::chrono::milliseconds) overload in production code: this one waits indefinitely, so an exhausted pool parks the calling thread with no diagnostic.

Definition at line 561 of file Pool.hpp.

Referenced by Lightweight::Pool< Config >::Acquire().

◆ Acquire() [2/4]

template<PoolConfig Config>
std::expected< PooledDataMapper, PoolError > Lightweight::Pool< Config >::Acquire ( std::chrono::milliseconds  timeout)
inline

Acquires a data mapper, giving up if none becomes available within timeout.

Bounds how long an exhausted BoundedWait pool may park the calling thread, so a stuck or overloaded pool surfaces as an error the caller can act on rather than as an indefinite hang.

Parameters
timeoutHow long to wait for a data mapper to be returned. A non-positive value makes this a pure try-acquire.
Returns
The acquired data mapper, or PoolError::Timeout if timeout elapsed first.

Definition at line 592 of file Pool.hpp.

References Timeout.

◆ Acquire() [3/4]

template<PoolConfig Config>
PooledDataMapper Lightweight::Pool< Config >::Acquire ( )
inline

Function to acquire a data mapper from the pool, the behavior of this function depends on the growth strategy this is a specific implementation for the strategies that do not block, which always creates a new data mapper if the pool is empty, regardless of the maximum capacity

Definition at line 625 of file Pool.hpp.

References Lightweight::SqlLogger::GetLogger(), and Lightweight::SqlLogger::OnConnectionReuse().

◆ Acquire() [4/4]

template<PoolConfig Config>
std::expected< PooledDataMapper, PoolError > Lightweight::Pool< Config >::Acquire ( std::chrono::milliseconds  timeout)
inline

Acquires a data mapper, for the strategies that never wait.

Provided so call sites can be written without knowing the strategy. These strategies create a fresh data mapper whenever no idle one is available, so the timeout can never elapse and the result always holds a value.

Parameters
timeoutIgnored; see above.
Returns
The acquired data mapper.

Definition at line 652 of file Pool.hpp.

References Lightweight::Pool< Config >::Acquire().

◆ AcquireAsync() [1/2]

template<PoolConfig Config>
Async::Task< PooledDataMapper > Lightweight::Pool< Config >::AcquireAsync ( Async::IExecutor &  dbWorkers,
Async::IResumeScheduler &  resume 
)
inline

Asynchronously acquires a DataMapper from the pool without blocking the calling thread.

If the pool is exhausted (BoundedWait at capacity), the awaiting coroutine is suspended and resumed — via resume — when a mapper is returned, rather than parking a thread. The acquired mapper's connection is wired for async via SqlConnection::EnableAsync(dbWorkers, resume), so the caller can immediately co_await its async methods.

Parameters
dbWorkersThe worker pool used to run the acquired mapper's blocking ODBC calls.
resumeThe scheduler used to resume coroutines (typically the app run loop).
Returns
A Task yielding a pooled DataMapper.

Definition at line 668 of file Pool.hpp.

◆ SetAsyncExecutors()

template<PoolConfig Config>
void Lightweight::Pool< Config >::SetAsyncExecutors ( Async::IExecutor &  dbWorkers,
Async::IResumeScheduler &  resume 
)
inlinenoexcept

Configures the executors that the no-argument AcquireAsync() overload wires acquired mappers for, so async consumers of this pool no longer repeat the executors at every call.

This is opt-in and scoped to the pool: only pools configured this way hand out async-enabled mappers via the no-arg overload; synchronous Acquire and connections outside the pool are unaffected. Unlike a process-global default, the executors' lifetime is tied to this pool, which already must outlive every acquirer.

Warning
dbWorkers and resume must outlive this pool's async use (the same contract the explicit-argument AcquireAsync overload already implies). Only references are retained. Intended to be called once during setup, before any concurrent AcquireAsync(); it is not synchronized against in-flight acquirers.
Parameters
dbWorkersThe worker pool used to run acquired mappers' blocking ODBC calls.
resumeThe scheduler used to resume coroutines (typically the app run loop).

Definition at line 688 of file Pool.hpp.

◆ AcquireAsync() [2/2]

template<PoolConfig Config>
Async::Task< PooledDataMapper > Lightweight::Pool< Config >::AcquireAsync ( )
inline

Asynchronously acquires a DataMapper using the executors previously set via SetAsyncExecutors, without blocking the calling thread.

Equivalent to the explicit-argument AcquireAsync overload with the pool's stored executors; pass the executors to that overload to override them for a single call.

Returns
A Task yielding a pooled DataMapper.
Exceptions
std::logic_errorif SetAsyncExecutors has not been called on this pool.

Definition at line 702 of file Pool.hpp.

◆ SetClock()

template<PoolConfig Config>
void Lightweight::Pool< Config >::SetClock ( std::function< Clock::time_point()>  clock)
inlinenoexcept

Overrides the clock the idle-time and lifetime bounds are measured against, so eviction can be driven deterministically (from a test, or from a simulated clock) instead of by sleeping.

Warning
Like SetAsyncExecutors, this is setup, not a runtime knob: it is not synchronized against in-flight acquirers, so call it before any concurrent use of the pool. Advancing whatever time clock reports is the caller's business afterwards.
Parameters
clockSource of the current time; pass {} to restore the real clock.

Definition at line 719 of file Pool.hpp.


The documentation for this class was generated from the following file: