1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-01-18 04:58:51 +02:00

Add waitRemaining() to get remaining wait time.

This can be used to set timeouts so they do not wait longer than needed.
This commit is contained in:
David Steele 2020-04-07 14:35:36 -04:00
parent ac3cfa4c9c
commit 627b495352
3 changed files with 12 additions and 0 deletions

View File

@ -16,11 +16,14 @@ struct Wait
{
MemContext *memContext; // Context that contains the wait handler
TimeMSec waitTime; // Total time to wait (in usec)
TimeMSec remainTime; // Wait time remaining (in usec)
TimeMSec sleepTime; // Next sleep time (in usec)
TimeMSec sleepPrevTime; // Previous time slept (in usec)
TimeMSec beginTime; // Time the wait began (in epoch usec)
};
OBJECT_DEFINE_GET(Remaining, const, WAIT, TimeMSec, remainTime);
OBJECT_DEFINE_FREE(WAIT);
/**********************************************************************************************************************************/
@ -45,6 +48,7 @@ waitNew(TimeMSec waitTime)
{
.memContext = MEM_CONTEXT_NEW(),
.waitTime = waitTime,
.remainTime = waitTime,
};
// Calculate first sleep time -- start with 1/10th of a second for anything >= 1 second
@ -96,6 +100,7 @@ waitMore(Wait *this)
// Store new sleep times
this->sleepPrevTime = this->sleepTime;
this->sleepTime = sleepNextTime;
this->remainTime = this->waitTime - elapsedTime;
}
// Else set sleep to zero so next call will return false
else

View File

@ -25,6 +25,12 @@ Functions
// Wait and return whether the caller has more time left
bool waitMore(Wait *this);
/***********************************************************************************************************************************
Getters/Setters
***********************************************************************************************************************************/
// How much time is remaining? Recalculated each time waitMore() is called.
TimeMSec waitRemaining(const Wait *this);
/***********************************************************************************************************************************
Destructor
***********************************************************************************************************************************/

View File

@ -22,6 +22,7 @@ testRun(void)
TimeMSec begin = timeMSec();
TEST_ASSIGN(wait, waitNew(200), "new wait = 0.2 sec");
TEST_RESULT_UINT(waitRemaining(wait), 200, " check remaining time");
TEST_RESULT_UINT(wait->waitTime, 200, " check wait time");
TEST_RESULT_UINT(wait->sleepTime, 20, " check sleep time");
TEST_RESULT_UINT(wait->sleepPrevTime, 0, " check sleep prev time");