-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerStats.h
More file actions
34 lines (27 loc) · 817 Bytes
/
ServerStats.h
File metadata and controls
34 lines (27 loc) · 817 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#ifndef SERVER_STATS_H
#define SERVER_STATS_H
#include <mutex>
#include <string>
struct ServerStats {
int total_requests = 0;
int active_connections = 0;
std::mutex stats_mutex;
void increment_requests() {
std::lock_guard<std::mutex> lock(stats_mutex);
total_requests++;
}
void connection_opened() {
std::lock_guard<std::mutex> lock(stats_mutex);
active_connections++;
}
void connection_closed() {
std::lock_guard<std::mutex> lock(stats_mutex);
active_connections--;
}
std::string to_json() {
std::lock_guard<std::mutex> lock(stats_mutex);
return "{ \"requests\": " + std::to_string(total_requests) +
", \"active_connections\": " + std::to_string(active_connections) + " }";
}
};
#endif