Skip to content

Commit

Permalink
dht_proxy_server: add push notification statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
François-Simon Fauteux-Chapleau committed Oct 4, 2024
1 parent 88d0af9 commit c1e14d6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
20 changes: 20 additions & 0 deletions include/opendht/dht_proxy_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ class OPENDHT_PUBLIC DhtProxyServer
size_t totalPermanentPuts {0};
/** Current number of push tokens with at least one listen operation */
size_t pushListenersCount {0};
/** Time at which the server was started */
time_point serverStartTime;
/** Last time at which the stats were updated */
time_point lastUpdated;
/** Total number of high priority push notification requests
* that the server attempted to send since being started */
uint64_t highPriorityPushRequestsCount {0};
/** Total number of normal priority push notification requests
* that the server attempted to send since being started */
uint64_t normalPriorityPushRequestsCount {0};
/** Average requests per second */
double requestRate {0};
/** Node Info **/
Expand All @@ -115,6 +125,9 @@ class OPENDHT_PUBLIC DhtProxyServer
std::string toString() const {
std::ostringstream ss;
ss << "Listens: " << listenCount << " Puts: " << putCount << " PushListeners: " << pushListenersCount << std::endl;
ss << "Push requests in the last " << print_duration(lastUpdated - serverStartTime) << ": "
<< highPriorityPushRequestsCount << " high priority, "
<< normalPriorityPushRequestsCount << " normal priority" << std::endl;
ss << "Requests: " << requestRate << " per second." << std::endl;
if (nodeInfo) {
auto& ipv4 = nodeInfo->ipv4;
Expand All @@ -136,6 +149,10 @@ class OPENDHT_PUBLIC DhtProxyServer
result["putCount"] = static_cast<Json::UInt64>(putCount);
result["totalPermanentPuts"] = static_cast<Json::UInt64>(totalPermanentPuts);
result["pushListenersCount"] = static_cast<Json::UInt64>(pushListenersCount);
result["serverStartTime"] = static_cast<Json::LargestInt>(to_time_t(serverStartTime));
result["lastUpdated"] = static_cast<Json::LargestInt>(to_time_t(lastUpdated));
result["highPriorityPushRequestsCount"] = static_cast<Json::UInt64>(highPriorityPushRequestsCount);
result["normalPriorityPushRequestsCount"] = static_cast<Json::UInt64>(normalPriorityPushRequestsCount);
result["requestRate"] = requestRate;
if (nodeInfo)
result["nodeInfo"] = nodeInfo->toJson();
Expand Down Expand Up @@ -393,6 +410,9 @@ class OPENDHT_PUBLIC DhtProxyServer
std::shared_ptr<ServerStats> stats_;
std::shared_ptr<NodeInfo> nodeInfo_ {};
std::unique_ptr<asio::steady_timer> printStatsTimer_;
const time_point serverStartTime_;
uint64_t highPriorityPushRequestsCount_ {0};
uint64_t normalPriorityPushRequestsCount_ {0};

// Thread-safe access to listeners map.
std::mutex lockListener_;
Expand Down
10 changes: 10 additions & 0 deletions src/dht_proxy_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ DhtProxyServer::DhtProxyServer(const std::shared_ptr<DhtRunner>& dht,
: ioContext_(std::make_shared<asio::io_context>()),
dht_(dht), persistPath_(config.persistStatePath), logger_(logger),
printStatsTimer_(std::make_unique<asio::steady_timer>(*ioContext_, 3s)),
serverStartTime_(clock::now()),
connListener_(std::make_shared<ConnectionListener>(std::bind(&DhtProxyServer::onConnectionClosed, this, std::placeholders::_1))),
pushServer_(config.pushServer),
bundleId_(config.bundleId)
Expand Down Expand Up @@ -542,7 +543,11 @@ DhtProxyServer::updateStats(std::shared_ptr<NodeInfo> info) const
stats.requestRate = count / dt.count();
#ifdef OPENDHT_PUSH_NOTIFICATIONS
stats.pushListenersCount = pushListeners_.size();
stats.highPriorityPushRequestsCount = highPriorityPushRequestsCount_;
stats.normalPriorityPushRequestsCount = normalPriorityPushRequestsCount_;
#endif
stats.serverStartTime = serverStartTime_;
stats.lastUpdated = now;
stats.totalPermanentPuts = 0;
std::for_each(puts_.begin(), puts_.end(), [&stats](const auto& put) {
stats.totalPermanentPuts += put.second.puts.size();
Expand Down Expand Up @@ -1155,6 +1160,11 @@ DhtProxyServer::sendPushNotification(const std::string& token, Json::Value&& jso
requests_[reqid] = request;
}
request->send();
// For monitoring purposes
if (highPriority)
highPriorityPushRequestsCount_++;
else
normalPriorityPushRequestsCount_++;
}
catch (const std::exception &e){
if (logger_)
Expand Down

0 comments on commit c1e14d6

Please sign in to comment.