userver
C++ Async Framework
Loading...
Searching...
No Matches
dist_lock_component_base.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/storages/mongo/dist_lock_component_base.hpp
4
/// @brief @copybrief storages::mongo::DistLockComponentBase
5
6
#
include
<
userver
/
components
/
component_base
.
hpp
>
7
#
include
<
userver
/
dist_lock
/
dist_locked_worker
.
hpp
>
8
#
include
<
userver
/
storages
/
mongo
/
collection
.
hpp
>
9
#
include
<
userver
/
storages
/
mongo
/
dist_lock_strategy
.
hpp
>
10
#
include
<
userver
/
utils
/
statistics
/
entry
.
hpp
>
11
12
USERVER_NAMESPACE_BEGIN
13
14
namespace
storages::mongo {
15
16
/// @ingroup userver_components userver_base_classes
17
///
18
/// @brief Base class for mongo-based distlock worker components
19
///
20
/// A component that implements a distlock with lock in Mongo. Inherit from
21
/// DistLockComponentBase and implement DoWork(). Lock options are configured in static config.
22
///
23
/// Mongo might not perform well on a high load, so you might want to use
24
/// postgres-based distlock storages::postgres::DistLockComponentBase instead.
25
/// @see storages::postgres::DistLockComponentBase
26
///
27
/// ## Cancellation checks
28
/// Functions engine::current_task::ShouldCancel(),
29
/// engine::InterruptibleSleepFor(), engine::InterruptibleSleepUntil() and
30
/// engine::current_task::CancellationPoint() check for task cancellation.
31
/// Overridden DistLockComponentBase::DoWork must use the above functions to
32
/// honour task cancellation and stop ASAP when it is cancelled.
33
///
34
/// ## Static configuration example:
35
///
36
/// ```yaml
37
/// example-distlock:
38
/// lockname: master
39
/// mongo-timeout: 1s
40
/// lock-ttl: 10s
41
/// ```
42
///
43
/// ## Static options of storages::mongo::DistLockComponentBase :
44
/// @include{doc} scripts/docs/en/components_schema/mongo/src/storages/mongo/dist_lock_component_base.md
45
///
46
/// Options inherited from @ref components::ComponentBase :
47
/// @include{doc} scripts/docs/en/components_schema/core/src/components/impl/component_base.md
48
///
49
/// @see @ref scripts/docs/en/userver/periodics.md
50
class
DistLockComponentBase
:
public
components
::
ComponentBase
{
51
public
:
52
DistLockComponentBase(
53
const
components
::ComponentConfig&,
54
const
components
::ComponentContext&,
55
storages::mongo::
Collection
56
);
57
58
dist_lock
::DistLockedWorker& GetWorker();
59
60
/// @note In testsuite always returns `true`, because there is only one host.
61
bool
OwnsLock
()
const
noexcept
;
62
63
static
yaml_config
::Schema GetStaticConfigSchema();
64
65
protected
:
66
/// Override this function with anything that must be done under the mongo
67
/// lock.
68
///
69
/// ## Example implementation
70
///
71
/// @code
72
/// void MyDistLockComponent::DoWork()
73
/// {
74
/// while (!engine::ShouldCancel())
75
/// {
76
/// // Start a new trace_id
77
/// auto span = tracing::Span::MakeRootSpan("my-dist-lock");
78
///
79
/// // If Foo() or other function in DoWork() throws an exception,
80
/// // DoWork() will be restarted in `restart-delay` seconds.
81
/// Foo();
82
///
83
/// // Check for cancellation after cpu-intensive Foo().
84
/// // You must check for cancellation at least every `lock-ttl`
85
/// // seconds to have time to notice lock prolongation failure.
86
/// if (engine::ShouldCancel()) break;
87
///
88
/// Bar();
89
/// }
90
/// }
91
/// @endcode
92
///
93
/// @note `DoWork` must honour task cancellation and stop ASAP when
94
/// it is cancelled, otherwise brain split is possible (IOW, two different
95
/// users do work assuming both of them hold the lock, which is not true).
96
virtual
void
DoWork
() = 0;
97
98
/// Override this function to provide custom testsuite handler.
99
virtual
void
DoWorkTestsuite
() {
DoWork
(
)
; }
100
101
/// Must be called in constructor
102
void
Start
();
103
104
/// Must be called in destructor
105
void
Stop
();
106
107
private
:
108
std::unique_ptr<
dist_lock
::DistLockedWorker> worker_;
109
bool
testsuite_enabled_{
false
};
110
};
111
112
}
// namespace storages::mongo
113
114
USERVER_NAMESPACE_END
userver
storages
mongo
dist_lock_component_base.hpp
Generated on Tue Jan 27 2026 16:47:32 for userver by
Doxygen
1.13.2