pgbackrest.git / summary / log / commit / refs

commit    f652261d32a4ffba98e7da26a41ef4ef1cd08da3
Author:   David Steele <david@pgbackrest.org>
Date:     Wed Aug 12 03:49:03 2026 +0000

    Send keep-alive noops only when the client has been idle.
    
    protocolKeepAlive() sent a noop to every remote each time it was called, and callers call it once per unit of work: per completed job in archive-push and backup, and per request in the server. A local process that was busy therefore produced a stream of round trips to remotes that had just been sent a request anyway.
    
    The member needed to prevent this has been present since 88201f37a, which stamped keepAliveTime on every write with the comment "Reset the keep alive time", but the check that would have read it was never written. 6a1c0337d and df8cbc91c each dropped one of those stamps, leaving only the initializer, so keepAliveTime has never been read in any version.
    
    Stamp keepAliveTime where requests are flushed and send a noop only after PROTOCOL_KEEP_ALIVE_TIME has elapsed. Since a request is itself data put to the server, a remote that is doing work needs no noop at all. A protocol-timeout below the 30 seconds chosen here would require the interval to be derived from it instead, but the default is 31 minutes.


src/protocol/client.c | 25 +++++++++++++++++++++++++ src/protocol/client.h | 4 ++++ src/protocol/helper.c | 2 +- test/src/module/protocol/protocolTest.c | 11 +++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/protocol/client.c b/src/protocol/client.c index 92a3844e2..3e31179a2 100644 --- a/src/protocol/client.c +++ b/src/protocol/client.c @@ -12,6 +12,11 @@ Protocol Client #include "protocol/server.h" #include "version.h" +/*********************************************************************************************************************************** +Minimum time between keep-alive noops +***********************************************************************************************************************************/ +#define PROTOCOL_KEEP_ALIVE_TIME ((TimeMSec)30000) + /*********************************************************************************************************************************** Client state enum ***********************************************************************************************************************************/ @@ -141,6 +146,9 @@ protocolClientRequestInternal(ProtocolClientSession *const this, const ProtocolC // Flush to send request immediately ioWriteFlush(this->client->write); + // Reset the keep alive time since data was just put to the server + this->client->keepAliveTime = timeMSec(); + this->pub.queued = true; // Switch state back to idle after successful request @@ -436,6 +444,23 @@ protocolClientNoOp(ProtocolClient *const this) FUNCTION_LOG_RETURN_VOID(); } +/**********************************************************************************************************************************/ +FN_EXTERN void +protocolClientKeepAlive(ProtocolClient *const this) +{ + FUNCTION_LOG_BEGIN(logLevelTrace); + FUNCTION_LOG_PARAM(PROTOCOL_CLIENT, this); + FUNCTION_LOG_END(); + + ASSERT(this != NULL); + + // A request is data put to the server, so a client that is busy needs no noop to keep the server from timing out + if (timeMSec() - this->keepAliveTime >= PROTOCOL_KEEP_ALIVE_TIME) + protocolClientNoOp(this); + + FUNCTION_LOG_RETURN_VOID(); +} + /*********************************************************************************************************************************** Free client session ***********************************************************************************************************************************/ diff --git a/src/protocol/client.h b/src/protocol/client.h index 85d6a147f..6cac9b136 100644 --- a/src/protocol/client.h +++ b/src/protocol/client.h @@ -127,6 +127,10 @@ protocolClientNoExit(ProtocolClient *const this) // Send noop to test connection or keep it alive FN_EXTERN void protocolClientNoOp(ProtocolClient *this); +// Send noop only when the client has been idle long enough for the server to be at risk of timing out. Callers send a keep-alive +// once per unit of work, which is far more often than the server needs one, so most calls do nothing. +FN_EXTERN void protocolClientKeepAlive(ProtocolClient *this); + // Simple request that does not require a session or async typedef struct ProtocolClientRequestParam { diff --git a/src/protocol/helper.c b/src/protocol/helper.c index b185a2328..df8e36114 100644 --- a/src/protocol/helper.c +++ b/src/protocol/helper.c @@ -888,7 +888,7 @@ protocolKeepAlive(void) ProtocolHelperClient *const match = lstGet(protocolHelper.clientList, clientIdx); if (match->type == protocolClientRemote) - protocolClientNoOp(match->client); + protocolClientKeepAlive(match->client); } } diff --git a/test/src/module/protocol/protocolTest.c b/test/src/module/protocol/protocolTest.c index d0cacbac1..94eae05cf 100644 --- a/test/src/module/protocol/protocolTest.c +++ b/test/src/module/protocol/protocolTest.c @@ -1321,7 +1321,18 @@ testRun(void) lstInsert(protocolHelper.clientList, 0, &protocolHelperClientAdd); + // The client was just created so no noop is needed to keep the server from timing out. A request updates the keep alive + // time, so the time not changing is what shows nothing was sent. + const TimeMSec keepAliveTime = client->keepAliveTime; + + TEST_RESULT_VOID(protocolKeepAlive(), "keep alive skipped when client is not idle"); + TEST_RESULT_UINT(client->keepAliveTime, keepAliveTime, "keep alive time not updated"); + + // Expire the keep alive to show the noop is sent once the client has been idle long enough + client->keepAliveTime = 0; + TEST_RESULT_VOID(protocolKeepAlive(), "keep alive"); + TEST_RESULT_INT_NE((int64_t)client->keepAliveTime, 0, "keep alive time updated"); lstRemoveIdx(protocolHelper.clientList, 0); TEST_RESULT_VOID(protocolFree(), "free remote protocol objects"); [parent: d8538e15e628]