|
| 1 | +#include "contrib/reverse_tunnel_reporter/source/reporters/event_reporter/reporter.h" |
| 2 | + |
| 3 | +#include "source/common/protobuf/utility.h" |
| 4 | + |
| 5 | +namespace Envoy { |
| 6 | +namespace Extensions { |
| 7 | +namespace Bootstrap { |
| 8 | +namespace ReverseConnection { |
| 9 | + |
| 10 | +EventReporter::EventReporter(Server::Configuration::ServerFactoryContext& context, |
| 11 | + const ConfigProto& config, |
| 12 | + std::vector<ReverseTunnelReporterClientPtr>&& clients) |
| 13 | + : context_{context}, clients_{std::move(clients)}, |
| 14 | + stats_(generateStats( |
| 15 | + PROTOBUF_GET_STRING_OR_DEFAULT(config, stat_prefix, "reverse_tunnel_reporter"), |
| 16 | + context.scope())) { |
| 17 | + ENVOY_LOG(info, "Constructed with {} clients", clients_.size()); |
| 18 | +} |
| 19 | + |
| 20 | +void EventReporter::onServerInitialized() { |
| 21 | + ENVOY_LOG(info, "Initialized"); |
| 22 | + for (auto& client : clients_) { |
| 23 | + client->onServerInitialized(this); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +void EventReporter::reportConnectionEvent(absl::string_view node_id, absl::string_view cluster_id, |
| 28 | + absl::string_view tenant_id) { |
| 29 | + auto ptr = std::make_shared<ReverseTunnelEvent::Connected>( |
| 30 | + ReverseTunnelEvent::Connected{std::string(node_id), std::string(cluster_id), |
| 31 | + std::string(tenant_id), Envoy::SystemTime::clock::now()}); |
| 32 | + |
| 33 | + context_.mainThreadDispatcher().post( |
| 34 | + [this, ptr = std::move(ptr)]() mutable { this->addConnection(std::move(ptr)); }); |
| 35 | +} |
| 36 | + |
| 37 | +void EventReporter::reportDisconnectionEvent(absl::string_view node_id, absl::string_view) { |
| 38 | + std::string name = ReverseTunnelEvent::getName(node_id); |
| 39 | + auto ptr = std::make_shared<ReverseTunnelEvent::Disconnected>(name); |
| 40 | + |
| 41 | + context_.mainThreadDispatcher().post( |
| 42 | + [this, ptr = std::move(ptr)]() mutable { this->removeConnection(std::move(ptr)); }); |
| 43 | +} |
| 44 | + |
| 45 | +// This is only served on the main thread so no locks needed. |
| 46 | +ReverseTunnelEvent::ConnectionsList EventReporter::getAllConnections() { |
| 47 | + ASSERT(context_.mainThreadDispatcher().isThreadSafe()); |
| 48 | + stats_.reverse_tunnel_full_pulls_total_.inc(); |
| 49 | + |
| 50 | + ReverseTunnelEvent::ConnectionsList all_connections; |
| 51 | + all_connections.reserve(connections_.size()); |
| 52 | + |
| 53 | + for (auto& [key, val] : connections_) { |
| 54 | + all_connections.push_back(val.connection); |
| 55 | + } |
| 56 | + return all_connections; |
| 57 | +} |
| 58 | + |
| 59 | +EventReporterStats EventReporter::generateStats(const std::string& prefix, Stats::Scope& scope) { |
| 60 | + return EventReporterStats{ALL_EVENT_REPORTER_STATS(POOL_COUNTER_PREFIX(scope, prefix), |
| 61 | + POOL_GAUGE_PREFIX(scope, prefix))}; |
| 62 | +} |
| 63 | + |
| 64 | +void EventReporter::notifyClients(ReverseTunnelEvent::TunnelUpdates&& updates) { |
| 65 | + ASSERT(clients_.size() > 0, "Need atleast one client. Enforced via the protos."); |
| 66 | + |
| 67 | + for (size_t i = 0; i < clients_.size() - 1; i++) { |
| 68 | + clients_[i]->receiveEvents(updates); |
| 69 | + } |
| 70 | + |
| 71 | + clients_.back()->receiveEvents(std::move(updates)); |
| 72 | +} |
| 73 | + |
| 74 | +void EventReporter::addConnection(std::shared_ptr<ReverseTunnelEvent::Connected>&& connection) { |
| 75 | + ASSERT(context_.mainThreadDispatcher().isThreadSafe()); |
| 76 | + |
| 77 | + ENVOY_LOG(info, "Accepted a new connection. Node: {}, Cluster: {}, Tenant: {}", |
| 78 | + connection->node_id, connection->cluster_id, connection->tenant_id); |
| 79 | + |
| 80 | + std::string name = ReverseTunnelEvent::getName(connection->node_id); |
| 81 | + auto [it, inserted] = connections_.try_emplace(std::move(name), std::move(connection), 1); |
| 82 | + |
| 83 | + if (inserted) { |
| 84 | + stats_.reverse_tunnel_unique_active_.inc(); |
| 85 | + notifyClients(ReverseTunnelEvent::TunnelUpdates{{it->second.connection}, {}}); |
| 86 | + } else { |
| 87 | + // Multiple reverse tunnels can share the same name (same node). |
| 88 | + // We ref-count them and only notify clients of removal when the last one disconnects. |
| 89 | + it->second.count++; |
| 90 | + } |
| 91 | + |
| 92 | + stats_.reverse_tunnel_established_total_.inc(); |
| 93 | + stats_.reverse_tunnel_active_.inc(); |
| 94 | +} |
| 95 | + |
| 96 | +void EventReporter::removeConnection( |
| 97 | + std::shared_ptr<ReverseTunnelEvent::Disconnected>&& disconnection) { |
| 98 | + ASSERT(context_.mainThreadDispatcher().isThreadSafe()); |
| 99 | + |
| 100 | + const auto& name = disconnection->name; |
| 101 | + auto it = connections_.find(name); |
| 102 | + |
| 103 | + ENVOY_LOG(info, "Removed connection. Name: {}", name); |
| 104 | + |
| 105 | + if (it == connections_.end()) { |
| 106 | + ENVOY_LOG(warn, "Tried to remove a connection which doesnt exist"); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + // Only notify removal on the last ref — see addConnection for the ref-count rationale. |
| 111 | + if (it->second.count == 1) { |
| 112 | + connections_.erase(it); |
| 113 | + stats_.reverse_tunnel_unique_active_.dec(); |
| 114 | + notifyClients(ReverseTunnelEvent::TunnelUpdates{{}, {disconnection}}); |
| 115 | + } else { |
| 116 | + it->second.count--; |
| 117 | + } |
| 118 | + |
| 119 | + stats_.reverse_tunnel_closed_total_.inc(); |
| 120 | + stats_.reverse_tunnel_active_.dec(); |
| 121 | +} |
| 122 | + |
| 123 | +} // namespace ReverseConnection |
| 124 | +} // namespace Bootstrap |
| 125 | +} // namespace Extensions |
| 126 | +} // namespace Envoy |
0 commit comments