rpc: Add APIs for direct triggering of keepalive timer

Add virKeepAliveTimeout and virKeepAliveTrigger APIs that can be used to
set poll timeouts and trigger keepalive timer. virKeepAliveTrigger
checks if it is called to early and does nothing in that case.
(cherry picked from commit 28c75382b0)
This commit is contained in:
Jiri Denemark 2012-06-12 23:42:32 +02:00 committed by Cole Robinson
parent 2c5b4c5621
commit e48596773f
2 changed files with 60 additions and 4 deletions

View File

@ -45,6 +45,7 @@ struct _virKeepAlive {
unsigned int count; unsigned int count;
unsigned int countToDeath; unsigned int countToDeath;
time_t lastPacketReceived; time_t lastPacketReceived;
time_t intervalStart;
int timer; int timer;
virNetMessagePtr response; virNetMessagePtr response;
@ -158,8 +159,11 @@ virKeepAliveTimerInternal(virKeepAlivePtr ka,
{ {
time_t now = time(NULL); time_t now = time(NULL);
if (now - ka->lastPacketReceived < ka->interval - 1) { if (ka->interval <= 0 || ka->intervalStart == 0)
int timeout = ka->interval - (now - ka->lastPacketReceived); return false;
if (now - ka->intervalStart < ka->interval) {
int timeout = ka->interval - (now - ka->intervalStart);
virEventUpdateTimeout(ka->timer, timeout * 1000); virEventUpdateTimeout(ka->timer, timeout * 1000);
return false; return false;
} }
@ -179,6 +183,7 @@ virKeepAliveTimerInternal(virKeepAlivePtr ka,
return true; return true;
} else { } else {
ka->countToDeath--; ka->countToDeath--;
ka->intervalStart = now;
*msg = virKeepAliveMessage(KEEPALIVE_PROC_PING); *msg = virKeepAliveMessage(KEEPALIVE_PROC_PING);
virEventUpdateTimeout(ka->timer, ka->interval * 1000); virEventUpdateTimeout(ka->timer, ka->interval * 1000);
return false; return false;
@ -335,6 +340,7 @@ virKeepAliveStart(virKeepAlivePtr ka,
int ret = -1; int ret = -1;
time_t delay; time_t delay;
int timeout; int timeout;
time_t now;
virKeepAliveLock(ka); virKeepAliveLock(ka);
@ -365,11 +371,13 @@ virKeepAliveStart(virKeepAlivePtr ka,
"ka=%p client=%p interval=%d count=%u", "ka=%p client=%p interval=%d count=%u",
ka, ka->client, interval, count); ka, ka->client, interval, count);
delay = time(NULL) - ka->lastPacketReceived; now = time(NULL);
delay = now - ka->lastPacketReceived;
if (delay > ka->interval) if (delay > ka->interval)
timeout = 0; timeout = 0;
else else
timeout = ka->interval - delay; timeout = ka->interval - delay;
ka->intervalStart = now - (ka->interval - timeout);
ka->timer = virEventAddTimeout(timeout * 1000, virKeepAliveTimer, ka->timer = virEventAddTimeout(timeout * 1000, virKeepAliveTimer,
ka, virKeepAliveTimerFree); ka, virKeepAliveTimerFree);
if (ka->timer < 0) if (ka->timer < 0)
@ -427,6 +435,51 @@ virKeepAliveStopSending(virKeepAlivePtr ka)
} }
int
virKeepAliveTimeout(virKeepAlivePtr ka)
{
int timeout;
if (!ka)
return -1;
virKeepAliveLock(ka);
if (ka->interval <= 0 || ka->intervalStart == 0) {
timeout = -1;
} else {
timeout = ka->interval - (time(NULL) - ka->intervalStart);
if (timeout < 0)
timeout = 0;
}
virKeepAliveUnlock(ka);
if (timeout < 0)
return -1;
else
return timeout * 1000;
}
bool
virKeepAliveTrigger(virKeepAlivePtr ka,
virNetMessagePtr *msg)
{
bool dead;
*msg = NULL;
if (!ka)
return false;
virKeepAliveLock(ka);
dead = virKeepAliveTimerInternal(ka, msg);
virKeepAliveUnlock(ka);
return dead;
}
bool bool
virKeepAliveCheckMessage(virKeepAlivePtr ka, virKeepAliveCheckMessage(virKeepAlivePtr ka,
virNetMessagePtr msg) virNetMessagePtr msg)
@ -442,7 +495,7 @@ virKeepAliveCheckMessage(virKeepAlivePtr ka,
virKeepAliveLock(ka); virKeepAliveLock(ka);
ka->countToDeath = ka->count; ka->countToDeath = ka->count;
ka->lastPacketReceived = time(NULL); ka->lastPacketReceived = ka->intervalStart = time(NULL);
if (msg->header.prog == KEEPALIVE_PROGRAM && if (msg->header.prog == KEEPALIVE_PROGRAM &&
msg->header.vers == KEEPALIVE_PROTOCOL_VERSION && msg->header.vers == KEEPALIVE_PROTOCOL_VERSION &&

View File

@ -51,6 +51,9 @@ int virKeepAliveStart(virKeepAlivePtr ka,
void virKeepAliveStop(virKeepAlivePtr ka); void virKeepAliveStop(virKeepAlivePtr ka);
void virKeepAliveStopSending(virKeepAlivePtr ka); void virKeepAliveStopSending(virKeepAlivePtr ka);
int virKeepAliveTimeout(virKeepAlivePtr ka);
bool virKeepAliveTrigger(virKeepAlivePtr ka,
virNetMessagePtr *msg);
bool virKeepAliveCheckMessage(virKeepAlivePtr ka, bool virKeepAliveCheckMessage(virKeepAlivePtr ka,
virNetMessagePtr msg); virNetMessagePtr msg);