diff --git a/src/ProxyServer.cc b/src/ProxyServer.cc index 60dd58f2..7113a266 100644 --- a/src/ProxyServer.cc +++ b/src/ProxyServer.cc @@ -32,7 +32,7 @@ using namespace std; -static const uint32_t SESSION_TIMEOUT_USECS = 10000000; // 10 seconds +static const uint32_t SESSION_TIMEOUT_USECS = 10 * 60 * 1000000; // 10 minutes @@ -655,6 +655,10 @@ void ProxyServer::LinkedSession::disconnect() { event_add(this->timeout_event.get(), &tv); } +bool ProxyServer::LinkedSession::is_connected() const { + return (this->server_bev.get() && this->client_bev.get()); +} + void ProxyServer::LinkedSession::on_client_input() { @@ -774,9 +778,21 @@ shared_ptr ProxyServer::create_licensed_session( return emplace_ret.first->second; } - void ProxyServer::delete_session(uint64_t id) { if (this->id_to_session.erase(id)) { this->log(INFO, "Closed LinkedSession:%08" PRIX64, id); } } + +size_t ProxyServer::delete_disconnected_sessions() { + size_t count = 0; + for (auto it = this->id_to_session.begin(); it != this->id_to_session.end();) { + if (!it->second->is_connected()) { + it = this->id_to_session.erase(it); + count++; + } else { + it++; + } + } + return count; +} diff --git a/src/ProxyServer.hh b/src/ProxyServer.hh index 5d6968ee..8435b530 100644 --- a/src/ProxyServer.hh +++ b/src/ProxyServer.hh @@ -163,7 +163,7 @@ public: void disconnect(); - bool is_open() const; + bool is_connected() const; }; std::shared_ptr get_session(); @@ -174,6 +174,8 @@ public: const ClientConfigBB& newserv_client_config); void delete_session(uint64_t id); + size_t delete_disconnected_sessions(); + private: struct ListeningSocket { ProxyServer* server; diff --git a/src/ServerShell.cc b/src/ServerShell.cc index e7191bbc..e7e729bd 100644 --- a/src/ServerShell.cc +++ b/src/ServerShell.cc @@ -146,6 +146,8 @@ Proxy commands (these will only work when exactly one client is connected):\n\ responds as if the function was called (with the given return value), but\n\ does not send the code to the client. To stop blocking function calls, omit\n\ the return value.\n\ + close-idle-sessions\n\ + Closes all sessions that don\'t have a client and server connected.\n\ "); @@ -385,6 +387,10 @@ Proxy commands (these will only work when exactly one client is connected):\n\ session->function_call_return_value = stoul(command_args); } + } else if (command_name == "close-idle-sessions") { + size_t count = this->state->proxy_server->delete_disconnected_sessions(); + fprintf(stderr, "%zu sessions closed\n", count); + } else { throw invalid_argument("unknown command; try \'help\'"); }