userver: userver/storages/postgres/notify.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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/engine/deadline.hpp>
7#include <userver/storages/postgres/options.hpp>
8#include <userver/utils/fast_pimpl.hpp>
9
10USERVER_NAMESPACE_BEGIN
11
12namespace storages::postgres {
13
14namespace detail {
15class ConnectionPtr;
16}
17
19 std::string channel;
20 std::optional<std::string> payload;
21};
22
23/// @brief RAII scope for receiving notifications.
24///
25/// Used for waiting for notifications on PostgreSQL connections. Created by
26/// calling storages::postgres::Cluster::Listen(). Exclusively holds a
27/// connection from a pool.
28///
29/// Non-copyable.
30///
31/// @par Usage synopsis
32/// @code
33/// auto scope = cluster.Listen("channel");
34/// cluster.Execute(pg::ClusterHostType::kMaster,
35/// "select pg_notify('channel', NULL)");
36/// auto ntf = scope.WaitNotify(engine::Deadline::FromDuration(100ms));
37/// @endcode
38class [[nodiscard]] NotifyScope final {
39public:
40 NotifyScope(detail::ConnectionPtr conn, std::string_view channel, OptionalCommandControl cmd_ctl);
41
42 ~NotifyScope();
43
44 NotifyScope(NotifyScope&&) noexcept;
45 NotifyScope& operator=(NotifyScope&&) noexcept;
46
47 NotifyScope(const NotifyScope&) = delete;
48 NotifyScope& operator=(const NotifyScope&) = delete;
49
50 /// Wait for notification on connection
51 Notification WaitNotify(engine::Deadline deadline);
52
53private:
54 struct Impl;
55 USERVER_NAMESPACE::utils::FastPimpl<Impl, 88, 8> pimpl_;
56};
57
58} // namespace storages::postgres
59
60USERVER_NAMESPACE_END