userver
C++ Async Framework
Toggle main menu visibility
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
/// autostart: true
42
/// ```
43
///
44
/// ## Static options of storages::mongo::DistLockComponentBase :
45
/// @include{doc} scripts/docs/en/components_schema/mongo/src/storages/mongo/dist_lock_component_base.md
46
///
47
/// Options inherited from @ref components::ComponentBase :
48
/// @include{doc} scripts/docs/en/components_schema/core/src/components/impl/component_base.md
49
///
50
/// @see @ref scripts/docs/en/userver/periodics.md
51
class
DistLockComponentBase
:
public
components
::
ComponentBase
{
52
public
:
53
DistLockComponentBase(
54
const
components
::ComponentConfig&,
55
const
components
::ComponentContext&,
56
storages::mongo::
Collection
57
);
58
59
dist_lock
::DistLockedWorker& GetWorker();
60
61
/// @note In testsuite always returns `true`, because there is only one host.
62
bool
OwnsLock
()
const
noexcept
;
63
64
static
yaml_config::Schema GetStaticConfigSchema();
65
66
protected
:
67
/// Override this function with anything that must be done under the mongo
68
/// lock.
69
///
70
/// ## Example implementation
71
///
72
/// @code
73
/// void MyDistLockComponent::DoWork()
74
/// {
75
/// while (!engine::ShouldCancel())
76
/// {
77
/// // Start a new trace_id
78
/// auto span = tracing::Span::MakeRootSpan("my-dist-lock");
79
///
80
/// // If Foo() or other function in DoWork() throws an exception,
81
/// // DoWork() will be restarted in `restart-delay` seconds.
82
/// Foo();
83
///
84
/// // Check for cancellation after cpu-intensive Foo().
85
/// // You must check for cancellation at least every `lock-ttl`
86
/// // seconds to have time to notice lock prolongation failure.
87
/// if (engine::ShouldCancel()) break;
88
///
89
/// Bar();
90
/// }
91
/// }
92
/// @endcode
93
///
94
/// @note `DoWork` must honour task cancellation and stop ASAP when
95
/// it is cancelled, otherwise brain split is possible (IOW, two different
96
/// users do work assuming both of them hold the lock, which is not true).
97
virtual
void
DoWork
() = 0;
98
99
/// Override this function to provide custom testsuite handler.
100
virtual
void
DoWorkTestsuite
() {
DoWork
(
)
; }
101
102
/// Must be called in constructor. Does not start the worker if static
103
/// configuration option `autostart` is `false`.
104
void
Start
();
105
106
/// Must be called in destructor
107
void
Stop
();
108
109
private
:
110
const
bool
autostart_;
111
std::unique_ptr<
dist_lock
::DistLockedWorker> worker_;
112
bool
testsuite_enabled_{
false
};
113
};
114
115
}
// namespace storages::mongo
116
117
USERVER_NAMESPACE_END
userver
storages
mongo
dist_lock_component_base.hpp
Generated on
for userver by
Doxygen
1.17.0