#include <userver/dist_lock/dist_locked_worker.hpp>
A high-level primitive that perpetually tries to acquire a distributed lock and runs user callback in a separate task while the lock is held. Cancels the task when the lock is lost.
Definition at line 28 of file dist_locked_worker.hpp.
◆ WorkerFunc
using dist_lock::DistLockedWorker::WorkerFunc = std::function<void()> |
◆ DistLockedWorker()
dist_lock::DistLockedWorker::DistLockedWorker |
( |
std::string | name, |
|
|
WorkerFunc | worker_func, |
|
|
std::shared_ptr< DistLockStrategyBase > | strategy, |
|
|
const DistLockSettings & | settings = {}, |
|
|
engine::TaskProcessor * | task_processor = nullptr, |
|
|
logging::Level | locker_log_level = logging::Level::kInfo ) |
Creates a DistLockedWorker.
- Parameters
-
name | name of the worker |
worker_func | a callback that's started each time we acquire the lock and is cancelled when the lock is lost. |
settings | distributed lock settings |
strategy | distributed locking strategy |
task_processor | TaskProcessor for running worker_func , using current TaskProcessor if nullptr |
locker_log_level | Log level for Locker (default is info) |
- Note
worker_func
must honour task cancellation and stop ASAP when it is cancelled, otherwise brain split is possible (IOW, two different users do work assuming both of them hold the lock, which is not true).
◆ GetLockedDuration()
std::optional< std::chrono::steady_clock::duration > dist_lock::DistLockedWorker::GetLockedDuration |
( |
| ) |
const |
Returns for how long the lock is held (if held at all). Returned value may be less than the real duration.
◆ IsRunning()
bool dist_lock::DistLockedWorker::IsRunning |
( |
| ) |
const |
◆ OwnsLock()
bool dist_lock::DistLockedWorker::OwnsLock |
( |
| ) |
const |
|
noexcept |
- Returns
- is current worker owns the lock
◆ RunOnce()
void dist_lock::DistLockedWorker::RunOnce |
( |
| ) |
|
Run task once acquiring the lock.
- Exceptions
-
std::exception | rethrows exception from worker_func . |
◆ Start()
void dist_lock::DistLockedWorker::Start |
( |
| ) |
|
◆ Stop()
void dist_lock::DistLockedWorker::Stop |
( |
| ) |
|
Stops acquiring the lock. It is guaranteed that the lock is not held after Stop() return and WorkerFunc is stopped (if was started).
The documentation for this class was generated from the following file: