Save server config deferred

This commit is contained in:
badaix 2020-02-08 12:17:12 +01:00
parent 13dd05545d
commit 384e71b7c9
2 changed files with 22 additions and 5 deletions

View file

@ -30,7 +30,8 @@ using namespace streamreader;
using json = nlohmann::json;
StreamServer::StreamServer(boost::asio::io_context& io_context, const ServerSettings& serverSettings) : io_context_(io_context), settings_(serverSettings)
StreamServer::StreamServer(boost::asio::io_context& io_context, const ServerSettings& serverSettings)
: io_context_(io_context), config_timer_(io_context), settings_(serverSettings)
{
}
@ -169,7 +170,7 @@ void StreamServer::onDisconnect(StreamSession* streamSession)
clientInfo->connected = false;
chronos::systemtimeofday(&clientInfo->lastSeen);
Config::instance().save();
saveConfig();
if (controlServer_ != nullptr)
{
// Check if there is no session of this client is left
@ -572,7 +573,7 @@ std::string StreamServer::onMessageReceived(ControlSession* controlSession, cons
{
jsonrpcpp::request_ptr request = dynamic_pointer_cast<jsonrpcpp::Request>(entity);
ProcessRequest(request, response, notification);
Config::instance().save();
saveConfig();
////cout << "Request: " << request->to_json().dump() << "\n";
if (notification)
{
@ -604,7 +605,7 @@ std::string StreamServer::onMessageReceived(ControlSession* controlSession, cons
notificationBatch.add_ptr(notification);
}
}
Config::instance().save();
saveConfig();
if (!notificationBatch.entities.empty())
controlServer_->send(notificationBatch.to_json().dump(), controlSession);
if (!responseBatch.entities.empty())
@ -690,7 +691,7 @@ void StreamServer::onMessageReceived(StreamSession* streamSession, const msg::Ba
}
LOG(DEBUG) << "Group: " << group->id << ", stream: " << group->streamId << "\n";
Config::instance().save();
saveConfig();
streamSession->sendAsync(stream->getMeta());
streamSession->setPcmStream(stream);
@ -721,6 +722,19 @@ void StreamServer::onMessageReceived(StreamSession* streamSession, const msg::Ba
}
void StreamServer::saveConfig()
{
config_timer_.cancel();
config_timer_.expires_after(2s);
config_timer_.async_wait([this](const boost::system::error_code& ec) {
if (!ec)
{
LOG(DEBUG) << "Saving config\n";
Config::instance().save();
}
});
}
session_ptr StreamServer::getStreamSession(StreamSession* streamSession) const
{

View file

@ -83,12 +83,15 @@ private:
session_ptr getStreamSession(StreamSession* session) const;
void ProcessRequest(const jsonrpcpp::request_ptr request, jsonrpcpp::entity_ptr& response, jsonrpcpp::notification_ptr& notification) const;
void cleanup();
/// save the server state deferred after 2s without a change to prevent blocking and too much disk io
void saveConfig();
mutable std::recursive_mutex sessionsMutex_;
mutable std::recursive_mutex clientMutex_;
std::vector<std::weak_ptr<StreamSession>> sessions_;
boost::asio::io_context& io_context_;
std::vector<acceptor_ptr> acceptor_;
boost::asio::steady_timer config_timer_;
ServerSettings settings_;
Queue<std::shared_ptr<msg::BaseMessage>> messages_;