userver: storages::postgres::DistLockStrategy Class Reference
Loading...
Searching...
No Matches
storages::postgres::DistLockStrategy Class Referencefinal

#include <userver/storages/postgres/dist_lock_strategy.hpp>

Detailed Description

Postgres distributed locking strategy. Release history, when requested, retains one expired row per lock key to track the shared idle interval used by fallback acquisition.

Definition at line 22 of file dist_lock_strategy.hpp.

Inheritance diagram for storages::postgres::DistLockStrategy:

Public Member Functions

 DistLockStrategy (ClusterPtr cluster, std::string_view table, std::string_view lock_name, const dist_lock::DistLockSettings &settings)
void Acquire (std::chrono::milliseconds lock_ttl, const std::string &locker_id) override
dist_lock::LockOperationResult ExecuteOperation (const std::string &locker_id, const dist_lock::LockOperationOptions &options) override
 Acquires, prolongs or checks the lock according to options. A check never changes the lock. Fallback acquisition requires the shared lock row to have been inactive for the entire minimum idle interval.
void Prolong (std::chrono::milliseconds lock_ttl, const std::string &locker_id) override
void Release (const std::string &locker_id) override
 Releases the lock by deleting its row.
void Release (const std::string &locker_id, const dist_lock::ReleaseOptions &options) override
 Releases the lock according to the shared fallback policy. When dist_lock::ReleaseOptions::preserve_history is enabled, retains the release time in an expired row instead of deleting the row. Retained history prevents fallback contenders from reusing observations made before another worker acquired and released the lock. Ordinary acquisition can reuse the released row immediately.
void UpdateCommandControl (CommandControl cc)

Member Function Documentation

◆ Acquire()

void storages::postgres::DistLockStrategy::Acquire ( std::chrono::milliseconds lock_ttl,
const std::string & locker_id )
overridevirtual

Acquires the distributed lock.

Parameters
lock_ttlThe duration for which the lock must be held.
locker_idGlobally unique ID of the locking entity.
Exceptions
LockIsAcquiredByAnotherHostErrorwhen the lock is busy
anythingelse when the locking fails, strategy is responsible for cleanup, Release won't be invoked.

Implements dist_lock::DistLockStrategyBase.

◆ ExecuteOperation()

dist_lock::LockOperationResult storages::postgres::DistLockStrategy::ExecuteOperation ( const std::string & locker_id,
const dist_lock::LockOperationOptions & options )
overridevirtual

Acquires, prolongs or checks the lock according to options. A check never changes the lock. Fallback acquisition requires the shared lock row to have been inactive for the entire minimum idle interval.

Exceptions
std::invalid_argumentif the minimum idle interval is negative.

Reimplemented from dist_lock::DistLockStrategyBase.

◆ Prolong()

void storages::postgres::DistLockStrategy::Prolong ( std::chrono::milliseconds lock_ttl,
const std::string & locker_id )
overridevirtual

Prolongs (refreshes the TTL of) a lock already held by locker_id.

Called instead of Acquire() on every refresh while the lock is held, so backends may implement a cheaper query than the full Acquire().

Parameters
lock_ttlThe new duration for which the lock must be held.
locker_idGlobally unique ID of the locking entity, must be the same as in Acquire().
Exceptions
LockIsAcquiredByAnotherHostExceptionwhen the lock is no longer held by locker_id (ownership was lost)
anythingelse when the prolongation fails, strategy is responsible for cleanup, Release won't be invoked.
Note
The default implementation simply calls Acquire(), preserving the legacy behaviour for strategies that do not override it.

Reimplemented from dist_lock::DistLockStrategyBase.

◆ Release() [1/2]

void storages::postgres::DistLockStrategy::Release ( const std::string & locker_id)
overridevirtual

Releases the lock by deleting its row.

Exceptions
dist_lock::LockIsAcquiredByAnotherHostExceptionif this locker no longer owns the lock.

Implements dist_lock::DistLockStrategyBase.

◆ Release() [2/2]

void storages::postgres::DistLockStrategy::Release ( const std::string & locker_id,
const dist_lock::ReleaseOptions & options )
overridevirtual

Releases the lock according to the shared fallback policy. When dist_lock::ReleaseOptions::preserve_history is enabled, retains the release time in an expired row instead of deleting the row. Retained history prevents fallback contenders from reusing observations made before another worker acquired and released the lock. Ordinary acquisition can reuse the released row immediately.

Exceptions
dist_lock::LockIsAcquiredByAnotherHostExceptionif this locker no longer owns the lock.

Reimplemented from dist_lock::DistLockStrategyBase.


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