HTTPServer: disconnect after idle timeout (-rpcservertimeout)

This commit is contained in:
Matthew Zipkin
2025-03-10 13:30:52 -04:00
parent e5f242eef3
commit cbb8d1fb33
2 changed files with 44 additions and 4 deletions

View File

@@ -23,6 +23,7 @@
#include <util/strencodings.h>
#include <util/string.h>
#include <util/threadinterrupt.h>
#include <util/time.h>
namespace util {
class SignalInterrupt;
@@ -413,6 +414,11 @@ public:
*/
void StopAccepting() { m_stop_accepting = true; }
/**
* Set the idle client timeout (-rpcservertimeout)
*/
void SetServerTimeout(std::chrono::seconds seconds) { m_rpcservertimeout = seconds; }
/**
* Force-remove all remaining clients from m_connected without waiting for
* graceful disconnection. Must only be called after JoinSocketsThreads().
@@ -503,6 +509,11 @@ private:
std::function<void(std::unique_ptr<HTTPRequest>&&)> m_request_dispatcher GUARDED_BY(m_request_dispatcher_mutex);
/// @}
/**
* Idle timeout after which clients are disconnected
*/
std::chrono::seconds m_rpcservertimeout{DEFAULT_HTTP_SERVER_TIMEOUT};
/**
* Accept a connection.
* @param[in] listen_sock Socket on which to accept the connection.
@@ -652,8 +663,13 @@ public:
//! possibly overriding all other disconnect flags.
std::atomic_bool m_disconnect{false};
//! Timestamp of last send or receive activity, used for -rpcservertimeout.
//! Due to optimistic sends it may be updated in either a worker thread or in the
//! I/O thread. It is checked in the I/O thread to disconnect idle clients.
std::atomic<SteadySeconds> m_idle_since;
explicit HTTPRemoteClient(HTTPServer::Id id, const CService& addr, std::unique_ptr<Sock> socket)
: m_id(id), m_addr(addr), m_origin(addr.ToStringAddrPort()), m_sock{std::move(socket)} {};
: m_id(id), m_addr(addr), m_origin(addr.ToStringAddrPort()), m_sock{std::move(socket)}, m_idle_since{Now<SteadySeconds>()} {}
// Disable copies (should only be used as shared pointers)
HTTPRemoteClient(const HTTPRemoteClient&) = delete;