]> git.sesse.net Git - nageru/commitdiff
Add an exported metrics for number of connected clients.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 8 Jun 2017 23:00:21 +0000 (01:00 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 8 Jun 2017 23:00:21 +0000 (01:00 +0200)
httpd.cpp
httpd.h

index e76cc80634ca9b2b03cd253d69b0641674693d19..2b19779ea450f253a2dee0e689b121eda4733335 100644 (file)
--- a/httpd.cpp
+++ b/httpd.cpp
@@ -22,6 +22,7 @@ using namespace std;
 
 HTTPD::HTTPD()
 {
+       global_metrics.register_int_metric("num_connected_clients", &metric_num_connected_clients);
 }
 
 HTTPD::~HTTPD()
@@ -94,6 +95,7 @@ int HTTPD::answer_to_connection(MHD_Connection *connection,
                unique_lock<mutex> lock(streams_mutex);
                streams.insert(stream);
        }
+       ++metric_num_connected_clients;
        *con_cls = stream;
 
        // Does not strictly have to be equal to MUX_BUFFER_SIZE.
@@ -119,6 +121,7 @@ void HTTPD::free_stream(void *cls)
                delete stream;
                httpd->streams.erase(stream);
        }
+       --httpd->metric_num_connected_clients;
 }
 
 ssize_t HTTPD::Stream::reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max)
diff --git a/httpd.h b/httpd.h
index 902745a5845b85d5e873914189057846c3a90585..6e37c5226206b3cd8f5cd5b3dcc8ea9b8b0bcecd 100644 (file)
--- a/httpd.h
+++ b/httpd.h
@@ -6,6 +6,7 @@
 #include <stddef.h>
 #include <stdint.h>
 #include <sys/types.h>
+#include <atomic>
 #include <condition_variable>
 #include <deque>
 #include <mutex>
@@ -78,6 +79,9 @@ private:
        std::mutex streams_mutex;
        std::set<Stream *> streams;  // Not owned.
        std::string header;
+
+       // Metrics.
+       std::atomic<int64_t> metric_num_connected_clients{0};
 };
 
 #endif  // !defined(_HTTPD_H)