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 {
38 public:
39 NotifyScope(detail::ConnectionPtr conn, std::string_view channel,
40 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
53 private:
54 struct Impl;
55 USERVER_NAMESPACE::utils::FastPimpl<Impl, 80, 8> pimpl_;
56};
57
58} // namespace storages::postgres
59
60USERVER_NAMESPACE_END