userver: dist_lock::DistLockedWorker Class Reference
Loading...
Searching...
No Matches
dist_lock::DistLockedWorker Class Referencefinal

#include <userver/dist_lock/dist_locked_worker.hpp>

Detailed Description

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 27 of file dist_locked_worker.hpp.

Public Types

using WorkerFunc = std::function<void()>
 

Public Member Functions

 DistLockedWorker (std::string name, WorkerFunc worker_func, std::shared_ptr< DistLockStrategyBase > strategy, const DistLockSettings &settings={}, engine::TaskProcessor *task_processor=nullptr)
 
const std::string & Name () const
 Name of the worker.
 
DistLockSettings GetSettings () const
 Retrieves settings in a thread-safe way.
 
void UpdateSettings (const DistLockSettings &)
 Update settings in a thread-safe way.
 
void Start ()
 
void Stop ()
 
void RunOnce ()
 
bool IsRunning () const
 
std::optional< std::chrono::steady_clock::duration > GetLockedDuration () const
 
const StatisticsGetStatistics () const
 Returns lock acquisition statistics.
 

Member Typedef Documentation

◆ WorkerFunc

using dist_lock::DistLockedWorker::WorkerFunc = std::function<void()>

Definition at line 29 of file dist_locked_worker.hpp.

Constructor & Destructor Documentation

◆ DistLockedWorker()

dist_lock::DistLockedWorker::DistLockedWorker ( std::string name,
WorkerFunc worker_func,
std::shared_ptr< DistLockStrategyBase > strategy,
const DistLockSettings & settings = {},
engine::TaskProcessor * task_processor = nullptr )

Creates a DistLockedWorker.

Parameters
namename of the worker
worker_funca callback that's started each time we acquire the lock and is cancelled when the lock is lost.
settingsdistributed lock settings
strategydistributed locking strategy
task_processorTaskProcessor for running worker_func, using current TaskProcessor if nullptr
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).

Member Function Documentation

◆ 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
Returns
whether the DistLockedTask is started.

◆ RunOnce()

void dist_lock::DistLockedWorker::RunOnce ( )

Run task once acquiring the lock.

Exceptions
std::exceptionrethrows exception from worker_func.

◆ Start()

void dist_lock::DistLockedWorker::Start ( )

Starts acquiring the lock. Please note that it's possible that the lock is acquired and the WorkerFunc is entered before Start() returns.

See also
DistLockedTask::DistLockedTask

◆ 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: