userver: userver/storages/postgres/notify.hpp Source File
Loading...
Searching...
No Matches
notify.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/postgres/notify.hpp
4/// @brief Asynchronous notifications
5
6#include <userver/storages/postgres/options.hpp>
7#include <userver/utils/fast_pimpl.hpp>
8
9USERVER_NAMESPACE_BEGIN
10
11namespace storages::postgres {
12
13namespace detail {
14class ConnectionPtr;
15}
16
18 std::string channel;
19 std::optional<std::string> payload;
20};
21
22/// @brief RAII scope for receiving notifications.
23///
24/// Used for waiting for notifications on PostgreSQL connections. Created by
25/// calling storages::postgres::Cluster::Listen(). Exclusively holds a
26/// connection from a pool.
27///
28/// Non-copyable.
29///
30/// @par Usage synopsis
31/// @code
32/// auto scope = cluster.Listen("channel");
33/// cluster.Execute(pg::ClusterHostType::kMaster,
34/// "select pg_notify('channel', NULL)");
35/// auto ntf = scope.WaitNotify(engine::Deadline::FromDuration(100ms));
36/// @endcode
37class [[nodiscard]] NotifyScope final {
38public:
39 NotifyScope(detail::ConnectionPtr conn, std::string_view channel, OptionalCommandControl cmd_ctl);
40
41 ~NotifyScope();
42
43 NotifyScope(NotifyScope&&) noexcept;
44 NotifyScope& operator=(NotifyScope&&) noexcept;
45
46 NotifyScope(const NotifyScope&) = delete;
47 NotifyScope& operator=(const NotifyScope&) = delete;
48
49 /// Wait for notification on connection
50 Notification WaitNotify(engine::Deadline deadline);
51
52private:
53 struct Impl;
54 USERVER_NAMESPACE::utils::FastPimpl<Impl, 80, 8> pimpl_;
55};
56
57} // namespace storages::postgres
58
59USERVER_NAMESPACE_END