diff options
| author | Mate Barany <mate.barany@qt.io> | 2025-11-11 15:12:26 +0100 |
|---|---|---|
| committer | Mate Barany <mate.barany@qt.io> | 2025-11-25 17:02:22 +0100 |
| commit | 89bd95df5a0fbec1fb45482b8d9dd260234e47d2 (patch) | |
| tree | eb45ab988926dff59933c4cccd50d457bfae6a2a /src | |
| parent | f216d13b67b9fb73c50593d7ab0047dc3b50805e (diff) | |
Use environment variables to change TCP keepalive settings in QNAM
TCP keepalive is enabled in QNetworkAccessManager but it uses the
default values meaning that it waits for 131 minutes to tear down
an inactive connections. Set some more aggressive defaults, so the
connection is terminated after 2 minutes of inactivity.
In QNetworkAccessManager we don't have direct access to the socket but
we might still want to change the default tcp keepalive settings if we
find them too agressive or too lenient. Use the following environment
variables:
QT_QNAM_TCP_KEEPIDLE - keepAliveIdleOption (in seconds)
QT_QNAM_TCP_KEEPINTVL - keepAliveIntervalOption (in seconds)
QT_QNAM_TCP_KEEPCNT - keepAliveCountOption (number of probes)
[ChangeLog][QtNetwork][QNetworkAccessManager] Added new environment
variables QT_QNAM_TCP_KEEPIDLE, QT_QNAM_TCP_KEEPINTVL and
QT_QNAM_TCP_KEEPCNT to change TCP keepalive options. Set the values
in QNAM so that it terminates an inactive connection after 2 minutes.
Task-number: QTBUG-136625
Change-Id: I7cb80164cfdfa1db90b5de25ce0e62457fd0e580
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src')
| -rw-r--r-- | src/network/access/qhttpnetworkconnectionchannel.cpp | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/src/network/access/qhttpnetworkconnectionchannel.cpp b/src/network/access/qhttpnetworkconnectionchannel.cpp index 6fe1ce38a61..bc23649a272 100644 --- a/src/network/access/qhttpnetworkconnectionchannel.cpp +++ b/src/network/access/qhttpnetworkconnectionchannel.cpp @@ -34,6 +34,12 @@ QT_BEGIN_NAMESPACE // connection times out) // We use 3 because we can get a _q_error 3 times depending on the timing: static const int reconnectAttemptsDefault = 3; +static const char keepAliveIdleOption[] = "QT_QNAM_TCP_KEEPIDLE"; +static const char keepAliveIntervalOption[] = "QT_QNAM_TCP_KEEPINTVL"; +static const char keepAliveCountOption[] = "QT_QNAM_TCP_KEEPCNT"; +static const int TCP_KEEPIDLE_DEF = 60; +static const int TCP_KEEPINTVL_DEF = 10; +static const int TCP_KEEPCNT_DEF = 5; QHttpNetworkConnectionChannel::QHttpNetworkConnectionChannel() : socket(nullptr) @@ -914,6 +920,13 @@ void QHttpNetworkConnectionChannel::_q_connected_abstract_socket(QAbstractSocket // not sure yet if it helps, but it makes sense absSocket->setSocketOption(QAbstractSocket::KeepAliveOption, 1); + int kaIdleOption = qEnvironmentVariableIntegerValue(keepAliveIdleOption).value_or(TCP_KEEPIDLE_DEF); + int kaIntervalOption = qEnvironmentVariableIntegerValue(keepAliveIntervalOption).value_or(TCP_KEEPINTVL_DEF); + int kaCountOption = qEnvironmentVariableIntegerValue(keepAliveCountOption).value_or(TCP_KEEPCNT_DEF); + absSocket->setSocketOption(QAbstractSocket::KeepAliveIdleOption, kaIdleOption); + absSocket->setSocketOption(QAbstractSocket::KeepAliveIntervalOption, kaIntervalOption); + absSocket->setSocketOption(QAbstractSocket::KeepAliveCountOption, kaCountOption); + pipeliningSupported = QHttpNetworkConnectionChannel::PipeliningSupportUnknown; // ### FIXME: if the server closes the connection unexpectedly, we shouldn't send the same broken request again! |
