@ -1,5 +1,6 @@
######## KEEPALIVE PINGING ########
#!trydef KEEPALIVE_ENABLED 1
#!trydef KEEPALIVE_NAT_ONLY 0
#!trydef KEEPALIVE_UDP_ONLY 0
#!trydef KEEPALIVE_TIMERS 4
@ -31,6 +32,7 @@ kazoo.keepalive_expired_registration_action = KEEPALIVE_EXPIRED_REGISTRATION_ACT
kazoo.keepalive_expired_subscription_action = KEEPALIVE_EXPIRED_SUBSCRIPTION_ACTION descr "action when presence expires a subscription, 1 = delete , 2 = disable, 0 = none"
kazoo.keepalive_on_registration_action = KEEPALIVE_ON_REGISTRATION_ACTION descr "action on registration, 1 = insert in keepalive , 0 = none"
kazoo.keepalive_on_subscription_action = KEEPALIVE_ON_SUBSCRIPTION_ACTION descr "action on subscription, 1 = insert in keepalive , 0 = none"
kazoo.keepalive_enable = KEEPALIVE_ENABLED descr "enable keepalive, 1 = on , 0 = off"
modparam("rtimer", "timer", "name = keepalive_timer;interval=1;mode=KEEPALIVE_S_TIMERS;")
modparam("rtimer", "exec", "timer = keepalive_timer;route=KEEPALIVE_TIMER")
@ -38,8 +40,8 @@ modparam("rtimer", "exec", "timer=keepalive_timer;route=KEEPALIVE_TIMER")
modparam("rtimer", "timer", "name = keepalive_db_timer;interval=1;mode=1;")
modparam("rtimer", "exec", "timer = keepalive_db_timer;route=KEEPALIVE_DB_TIMER")
modparam("rtimer", "timer", "name = keepalive_cleanup;interval=5;mode=1;")
modparam("rtimer", "exec", "timer = keepalive_cleanup;route=KEEPALIVE_CLEANUP")
## modparam("rtimer", "timer", "name= keepalive_cleanup;interval=5;mode=1;")
## modparam("rtimer", "exec", "timer= keepalive_cleanup;route=KEEPALIVE_CLEANUP")
modparam("mqueue","mqueue", "name = keepalive_db_queue")
@ -66,6 +68,8 @@ modparam("statistics","variable", "keepalive:removed_from_expired_registration")
modparam("statistics","variable", "keepalive:disabled_from_expired_subscription")
modparam("statistics","variable", "keepalive:removed_from_expired_subscription")
modparam("htable", "htable", "keepalive = >size=32;")
route[KEEPALIVE_DB_TIMER]
{
$var(runloop) = 1;
@ -92,42 +96,58 @@ route[KEEPALIVE_DB_TIMER]
route[KEEPALIVE_CLEANUP]
{
$var(Query) = $_s(UPDATE keepalive SET SELECTED = 9 where selected < 3 and failed > $sel(cfg_get.kazoo.keepalive_failed_threshold));
sql_query("cb", "$var(Query)");
if($sel(cfg_get.kazoo.keepalive_enable) = = 0) return;
if($sel(cfg_get.kazoo.keepalive_expire_registrations) = = 1) {
$var(Query) = $_s(update location set expires = last_modified where id in(select b.id from w_keepalive_contact a inner join w_location_contact b on a.contact = b.contact where selected = 9));
sql_query("cb", "$var(Query)");
$var(stat_update) = $_s(+$sqlrows(cb));
update_stat("keepalive:expired_registrations", "$var(stat_update)");
}
$var(Query) = $_s(UPDATE keepalive SET SELECTED = 9 WHERE slot = $var(slot) AND selected = 0 and failed > $sel(cfg_get.kazoo.keepalive_failed_threshold));
# $var(Query) = $_s(UPDATE keepalive SET SELECTED = 9 where selected < 3 and failed > $sel(cfg_get.kazoo.keepalive_failed_threshold));
sql_query("cb", "$var(Query)");
if($sel(cfg_get.kazoo.keepalive_expire_subscriptions) = = 1) {
$var(Query) = $_s(DELETE FROM active_watchers where id in(select b.id from w_keepalive_contact a inner join w_watchers_contact b on a.contact = b.contact where selected = 9));
if($sqlrows(cb) > 0) {
if($sel(cfg_get.kazoo.keepalive_expire_registrations) = = 1) {
if($def(REGISTRAR_DB_MODE) = = 3) {
$var(Query) = $_s(update location set expires = last_modified where id in(select b.id from w_keepalive_contact a inner join w_location_contact b on a.contact = b.contact where selected = 9));
sql_query("cb", "$var(Query)");
$var(stat_update) = $_s(+$sqlrows(cb));
update_stat("keepalive:expired_registrations", "$var(stat_update)");
} else {
$var(Query) = $_s(update location set expires = last_modified where id in(select b.id from w_keepalive_contact a inner join w_location_contact b on a.contact = b.contact where selected = 9));
sql_query("cb", "$var(Query)");
$var(stat_update) = $_s(+$sqlrows(cb));
update_stat("keepalive:expired_registrations", "$var(stat_update)");
}
}
if($sel(cfg_get.kazoo.keepalive_expire_subscriptions) = = 1) {
$var(Query) = $_s(DELETE FROM active_watchers where id in(select b.id from w_keepalive_contact a inner join w_watchers_contact b on a.contact = b.contact where selected = 9));
sql_query("cb", "$var(Query)");
$var(stat_update) = $_s(+$sqlrows(cb));
update_stat("keepalive:expired_subscriptions", "$var(stat_update)");
}
if($sel(cfg_get.kazoo.keepalive_failed_action) = = 2) {
## disable
$var(Query) = $_s(UPDATE keepalive SET SELECTED = 10 where selected = 9);
$var(stat) = "keepalive:disabled";
} else if($sel(cfg_get.kazoo.keepalive_failed_action) = = 1) {
## delete - will be recreated on registration/subscription with same contact
$var(Query) = $_s(DELETE FROM keepalive where selected = 9);
$var(stat) = "keepalive:removed";
}
sql_query("cb", "$var(Query)");
$var(stat_update) = $_s(+$sqlrows(cb));
update_stat("keepalive:expired_subscriptions", "$var(stat_update)");
}
if($sel(cfg_get.kazoo.keepalive_failed_action) = = 2) {
## disable
$var(Query) = $_s(UPDATE keepalive SET SELECTED = 10 where selected = 9);
$var(stat) = "keepalive:disabled";
} else if($sel(cfg_get.kazoo.keepalive_failed_action) = = 1) {
## delete - will be recreated on registration/subscription with same contact
$var(Query) = $_s(DELETE FROM keepalive where selected = 9);
$var(stat) = "keepalive:removed";
update_stat("$var(stat)", "$var(stat_update)");
}
sql_query("cb", "$var(Query)");
$var(stat_update) = $_s(+$sqlrows(cb));
update_stat("$var(stat)", "$var(stat_update)");
}
route[KEEPALIVE_TIMER]
{
if($sel(cfg_get.kazoo.keepalive_enable) = = 0) return;
$var(base_slot) = $rtimer_worker * $sel(cfg_get.kazoo.keepalive_interval);
$var(slot) = $var(base_slot) + $var(tick);
$var(Query) = $_s(UPDATE keepalive SET selected = 1 WHERE slot = $var(slot) AND selected = 0 AND time_sent < datetime('now', '-$sel(cfg_get.kazoo.keepalive_interval) seconds'));
## xlog("L_NOTICE", "SQLTIMER ($var(base_slot) + $var(tick))> $var(Query)\n");
$var(sqlres) = sql_query("cb", "$var(Query)");
if($var(sqlres) < 0) {
xlog("L_ERROR", "$rtimer_worker|$var(tick)|log|error running query : $var(Query)\n");
@ -135,6 +155,8 @@ route[KEEPALIVE_TIMER]
$var(nrows) = $sqlrows(cb);
xlog("L_DEBUG", "$rtimer_worker|$var(tick)|log|selected $var(nrows) endpoints to ping\n");
}
route(KEEPALIVE_CLEANUP);
$var(Query) = $_s(SELECT id, contact, sockinfo from keepalive WHERE slot = $var(slot) AND selected = 1);
xlog("L_DEBUG", "$rtimer_worker|$var(tick)|timer|SQL = > $var(Query)\n");
@ -219,6 +241,7 @@ route[KEEPALIVE_PING]
route[KEEPALIVE_ON_REGISTRATION]
{
if($sel(cfg_get.kazoo.keepalive_enable) = = 0) return;
if($sel(cfg_get.kazoo.keepalive_on_registration_action) = = 0) {
return;
@ -227,6 +250,10 @@ route[KEEPALIVE_ON_REGISTRATION]
if($proto = = "ws" || $proto == "wss") {
return;
}
if($sht(keepalive = >$si~$sp~$prid) != $null) {
return;
}
if (isbflagset(FLB_NATB)) {
if(!isbflagset(FLB_NATSIPPING)) {
@ -259,24 +286,28 @@ route[KEEPALIVE_ON_REGISTRATION]
route[KEEPALIVE_ON_SUBSCRIBE]
{
if($sel(cfg_get.kazoo.keepalive_enable) = = 0) return;
if($sel(cfg_get.kazoo.keepalive_on_subscription_action) = = 0) {
return;
}
if(!( ($sel(cfg_get.kazoo.keepalive_udp_only) = = 1 && $proto != "udp")
|| (!isflagset(FLT_NATS) && $sel(cfg_get.kazoo.keepalive_nat_only) = = 1)
|| ($proto = = "ws" || $proto == "wss")
)) {
$var(max_slots) = $sel(cfg_get.kazoo.keepalive_interval) * KEEPALIVE_S_TIMERS;
$var(slot) = $(subs(contact){s.corehash, $var(max_slots)});
$var(alias) = $(subs(contact){uri.param,alias});
$var(sql) = $_s(INSERT OR IGNORE INTO keepalive (contact, received, sockinfo, slot) values("$subs(contact)", "$var(alias)", "$subs(sockinfo)", $var(slot)));
mq_add("keepalive_db_queue", "keepalive:from_subscription", "$var(sql)");
if($sht(keepalive = >$si~$sp~$prid) != $null) {
return;
}
$var(max_slots) = $sel(cfg_get.kazoo.keepalive_interval) * KEEPALIVE_S_TIMERS;
$var(slot) = $(subs(contact){s.corehash, $var(max_slots)});
$var(alias) = $(subs(contact){uri.param,alias});
$var(sql) = $_s(INSERT OR IGNORE INTO keepalive (contact, received, sockinfo, slot) values("$subs(contact)", "$var(alias)", "$subs(sockinfo)", $var(slot)));
mq_add("keepalive_db_queue", "keepalive:from_subscription", "$var(sql)");
}
route[KEEPALIVE_ON_EXPIRED_REGISTRATION]
{
if($sel(cfg_get.kazoo.keepalive_enable) = = 0) return;
if($sel(cfg_get.kazoo.keepalive_expired_registration_action) = = 2) {
## disable
$var(Query) = $_s(UPDATE keepalive SET SELECTED = 10 where selected < 3 and contact like "$ulc(exp=>addr)%");
@ -286,16 +317,27 @@ route[KEEPALIVE_ON_EXPIRED_REGISTRATION]
$var(Query) = $_s(DELETE FROM keepalive where selected < 3 and contact like "$ulc(exp=>addr)%");
mq_add("keepalive_db_queue", "keepalive:removed_from_expired_registration", "$var(Query)");
}
}
route[KEEPALIVE_ON_OPTIONS]
{
$var(Query) = $_s(UPDATE keepalive set selected = 3 where received = "$si~$sp~$prid" and selected <> 3 );
mq_add("keepalive_db_queue", "keepalive:client_options", "$var(Query)");
if($sel(cfg_get.kazoo.keepalive_enable) = = 0) return;
if($shtinc(keepalive = >$si~$sp~$prid) == 1) {
$var(Query) = $_s(UPDATE keepalive set selected = 3 where received = "$si~$sp~$prid" and selected <> 3 );
mq_add("keepalive_db_queue", "keepalive:client_options", "$var(Query)");
}
}
route[KEEPALIVE_ON_NOTIFY]
{
$var(Query) = $_s(UPDATE keepalive set selected = 4 where received = "$si~$sp~$prid" and selected <> 4 );
mq_add("keepalive_db_queue", "keepalive:client_notify", "$var(Query)");
if($sel(cfg_get.kazoo.keepalive_enable) = = 0) return;
if($shtinc(keepalive = >$si~$sp~$prid) == 1) {
$var(Query) = $_s(UPDATE keepalive set selected = 4 where received = "$si~$sp~$prid" and selected <> 4 );
mq_add("keepalive_db_queue", "keepalive:client_notify", "$var(Query)");
}
}