userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
component.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/storages/postgres/component.hpp
4
/// @brief @copybrief components::Postgres
5
6
#
include
<
chrono
>
7
8
#
include
<
userver
/
components
/
component_base
.
hpp
>
9
#
include
<
userver
/
dynamic_config
/
snapshot
.
hpp
>
10
#
include
<
userver
/
engine
/
mutex
.
hpp
>
11
#
include
<
userver
/
storages
/
postgres
/
database
.
hpp
>
12
#
include
<
userver
/
storages
/
secdist
/
secdist
.
hpp
>
13
#
include
<
userver
/
utils
/
statistics
/
fwd
.
hpp
>
14
15
USERVER_NAMESPACE_BEGIN
16
17
namespace
components
{
18
19
/// @ingroup userver_components
20
///
21
/// @brief PosgreSQL client component
22
///
23
/// Provides access to a PostgreSQL cluster.
24
///
25
/// ## Dynamic options:
26
/// * @ref POSTGRES_DEFAULT_COMMAND_CONTROL
27
/// * @ref POSTGRES_HANDLERS_COMMAND_CONTROL
28
/// * @ref POSTGRES_QUERIES_COMMAND_CONTROL
29
/// * @ref POSTGRES_CONNECTION_POOL_SETTINGS
30
/// * @ref POSTGRES_TOPOLOGY_SETTINGS
31
/// * @ref POSTGRES_CONNECTION_SETTINGS
32
/// * @ref POSTGRES_STATEMENT_METRICS_SETTINGS
33
/// * @ref POSTGRES_CONNLIMIT_MODE_AUTO_ENABLED
34
///
35
/// ## Static configuration example:
36
///
37
/// ```
38
/// # yaml
39
/// postgres-taxi:
40
/// dbalias: taxi
41
/// blocking_task_processor: task-processor-name
42
/// max_replication_lag: 60s
43
/// min_pool_size: 4
44
/// max_pool_size: 15
45
/// max_queue_size: 200
46
/// max_statement_metrics: 50
47
/// ```
48
/// You must specify either `dbalias` or `dbconnection`.
49
/// If the component is configured with an alias, it will lookup connection data
50
/// in Secdist.
51
///
52
/// It is a common practice to provide a database connection string via
53
/// environment variables. To retrieve a value from the environment use
54
/// `dbconnection#env: THE_ENV_VARIABLE_WITH_CONNECTION_STRING` as described
55
/// in yaml_config::YamlConfig.
56
///
57
/// You must specify `blocking_task_processor` as well.
58
///
59
/// `max_replication_lag` can be used to tune replication lag limit for replicas.
60
/// Once the replica lag exceeds this value it will be automatically disabled.
61
/// Note, however, that client-size lag detection is not precise in nature
62
/// and can only provide the precision of couple seconds.
63
///
64
/// ## Secdist format
65
///
66
/// A PosgreSQL alias in secdist is described as a JSON array of objects
67
/// containing a single cluster description. There are two formats of describing
68
/// a cluster, the first one assigns predefined roles to DSNs, the second one
69
/// is just a list of DSNs and the Postgres component takes care of discovering
70
/// the cluster's topology itself.
71
///
72
/// Note that if the `dbalias` option is provided and components::Secdist component has `update-period` other
73
/// than 0, then new connections are created or gracefully closed as the secdist configuration change to new value.
74
///
75
/// ### Predefined roles
76
///
77
/// In predefined roles format the component requires single-host connection
78
/// strings.
79
///
80
/// ```json
81
/// {
82
/// "shard_number" : 0,
83
/// "master": "host=localhost dbname=example",
84
/// "sync_slave": "host=localhost dbname=example",
85
/// "slaves": [
86
/// "host=localhost dbname=example"
87
/// ]
88
/// }
89
/// ```
90
///
91
/// The predefined roles format is deprecated and the support will be removed
92
/// soon.
93
///
94
/// ### Automatic discovery
95
///
96
/// In automatic discovery format the connection strings are any valid
97
/// PostgreSQL connection strings including multi-host ones with the exception
98
/// of `target_session_attrs` which will be ignored.
99
///
100
/// ```json
101
/// {
102
/// "shard_number" : 0,
103
/// "hosts": [
104
/// "host=host1,host2,host3 dbname=example",
105
/// "postgresql://host1:5432,host2:6432,host3:12000/example"
106
/// ]
107
/// }
108
/// ```
109
///
110
/// The `shard_number` parameter is required in both formats and must match the
111
/// index of cluster description object in the alias array.
112
///
113
/// Please see [PostgreSQL documentation](https://www.postgresql.org/docs/12/libpq-connect.html#LIBPQ-CONNSTRING)
114
/// on connection strings.
115
///
116
/// ## Static options of components::Postgres :
117
/// @include{doc} scripts/docs/en/components_schema/postgresql/src/storages/postgres/component.md
118
///
119
/// Options inherited from @ref components::ComponentBase :
120
/// @include{doc} scripts/docs/en/components_schema/core/src/components/impl/component_base.md
121
class
Postgres
:
public
ComponentBase
{
122
public
:
123
/// Default shard number
124
static
constexpr
size_t
kDefaultShardNumber
= 0;
125
/// Default command control
126
static
constexpr
storages::
postgres
::
CommandControl
kDefaultCommandControl
{
127
std::chrono::milliseconds{500},
// network timeout
128
std::chrono::milliseconds{250}
// statement timeout
129
};
130
131
/// Component constructor
132
Postgres
(
const
ComponentConfig&,
const
ComponentContext&);
133
/// Component destructor
134
~
Postgres
() override;
135
136
/// Cluster accessor for default shard number
137
storages::
postgres
::
ClusterPtr
GetCluster
()
const
;
138
139
/// Cluster accessor for specific shard number
140
storages::
postgres
::
ClusterPtr
GetClusterForShard
(size_t shard)
const
;
141
142
/// Get total shard count
143
size_t
GetShardCount
()
const
;
144
145
/// Get database object
146
storages::
postgres
::DatabasePtr
GetDatabase
()
const
{
return
database_; }
147
148
/// Reports statistics for PostgreSQL driver
149
void
ExtendStatistics
(
utils
::
statistics
::Writer& writer);
150
151
static
yaml_config
::Schema GetStaticConfigSchema();
152
153
private
:
154
void
OnConfigUpdate(
const
dynamic_config::Snapshot& cfg);
155
156
void
OnSecdistUpdate(
const
storages::secdist::SecdistConfig& secdist);
157
158
std::string name_;
159
std::string db_name_;
160
std::string dbalias_;
161
storages::
postgres
::
ClusterSettings
initial_settings_;
162
storages::
postgres
::DatabasePtr database_;
163
164
dynamic_config::Source config_source_;
165
};
166
167
template
<>
168
inline
constexpr
bool
kHasValidate<Postgres> =
true
;
169
170
}
// namespace components
171
172
USERVER_NAMESPACE_END
userver
storages
postgres
component.hpp
Generated on
for userver by
Doxygen
1.17.0