Skip to content

Commit

Permalink
event/net/PingClient: allow reusing the socket
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Aug 20, 2024
1 parent 865cd3f commit ab6773c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
1 change: 1 addition & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ cm4all-prometheus-exporters (0.16) unstable; urgency=low
* ping-exporter: wait for HTTP request to arrive
* ping-exporter: reduce interval to 5 seconds
* ping-exporter: verify the checksum of received packets
* ping-exporter: reuse the ICMP socket

--

Expand Down
2 changes: 1 addition & 1 deletion libcommon
12 changes: 4 additions & 8 deletions src/PingExporter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class PingTarget final : PingClientHandler {

Event::TimePoint start_time;

std::optional<PingClient> ping_client;
PingClient ping_client;

PingTargetStats stats{};

Expand All @@ -43,6 +43,7 @@ class PingTarget final : PingClientHandler {
public:
explicit PingTarget(EventLoop &event_loop, const IPv4Address &_address) noexcept
:timer(event_loop, BIND_THIS_METHOD(OnTimer)),
ping_client(event_loop, *this),
address(_address)
{
inet_ntop(AF_INET, &address.GetAddress(), name, sizeof(name));
Expand All @@ -64,30 +65,25 @@ class PingTarget final : PingClientHandler {

private:
void OnTimer() noexcept {
if (ping_client) {
if (ping_client.IsPending()) {
++stats.n_timeouts;
ping_client.reset();
}

timer.Schedule(interval);

++stats.n_requests;
start_time = GetEventLoop().SteadyNow();

PingClientHandler &handler = *this;
ping_client.emplace(GetEventLoop(), handler);
ping_client->Start(address);
ping_client.Start(address);
}

// virtual methods from class PingClientHandler
void PingResponse() noexcept override {
ping_client.reset();
++stats.n_replies;
stats.wait += GetEventLoop().SteadyNow() - start_time;
}

void PingError([[maybe_unused]] std::exception_ptr error) noexcept override {
ping_client.reset();
++stats.n_errors;
}
};
Expand Down

0 comments on commit ab6773c

Please sign in to comment.