Skip to content

Commit

Permalink
Wire up notification mechanism to new handlers.
Browse files Browse the repository at this point in the history
  • Loading branch information
jtv committed Dec 18, 2024
1 parent 75ff5d0 commit 3c987e1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
7.10.0
- Deprecate `errorhandler`; replace with lambda-friendly "notice handlers."
- Deprecate `notification_receiver`; replace with "notification handlers"
- Bump minimum CMake version to 3.28. (#874)
- Fixed error message on clashing transaction focuses. (#879)
- Don't call`/bin/true`; macOS doesn't have it. Just call `true`. (#885)
Expand Down
1 change: 1 addition & 0 deletions include/pqxx/connection.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,7 @@ private:
/// 9.0: Replace with just notice handler.
std::shared_ptr<pqxx::internal::notice_waiters> m_notice_waiters;

// TODO: Remove these when we retire notification_receiver.
// TODO: Can we make these movable?
using receiver_list =
std::multimap<std::string, pqxx::notification_receiver *>;
Expand Down
3 changes: 3 additions & 0 deletions include/pqxx/notification.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@ public:
* @param cx Connnection to operate on.
* @param channel Name of the notification to listen for.
*/
[[deprecated("Use pqxx::connection::listen() instead.")]]
notification_receiver(connection &cx, std::string_view channel);
/// Register the receiver with a connection.
[[deprecated("Use pqxx::connection::listen() instead.")]]
notification_receiver(notification_receiver const &) = delete;
/// Register the receiver with a connection.
[[deprecated("Use pqxx::connection::listen() instead.")]]
notification_receiver &operator=(notification_receiver const &) = delete;
/// Deregister the receiver.
virtual ~notification_receiver();
Expand Down
16 changes: 14 additions & 2 deletions src/connection.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -608,16 +608,20 @@ int pqxx::connection::get_notifs()

// Even if somehow we receive notifications during our transaction, don't
// deliver them.
if (m_trans)
if (m_trans != nullptr)
PQXX_UNLIKELY
return 0;

int notifs = 0;

// Old mechanism. This is going away.
for (auto N{get_notif(m_conn)}; N.get(); N = get_notif(m_conn))
{
notifs++;

auto const Hit{m_receivers.equal_range(std::string{N->relname})};
std::string const name{N->relname};

auto const Hit{m_receivers.equal_range(name)};
if (Hit.second != Hit.first)
{
std::string const payload{N->extra};
Expand Down Expand Up @@ -649,8 +653,16 @@ int pqxx::connection::get_notifs()
}
}

auto const handler{m_notification_handlers.find(N->relname)};
if (handler != std::end(m_notification_handlers))
{
++notifs;
(handler->second)(name, N->be_pid, N->extra);
}

N.reset();
}

return notifs;
}

Expand Down

0 comments on commit 3c987e1

Please sign in to comment.