improve gateway resilience

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@10528 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Anthony Minessale 2008-11-24 16:56:21 +00:00
parent 47015599dc
commit e077cbe18e
4 changed files with 32 additions and 12 deletions

View File

@ -1368,6 +1368,7 @@ static const char *sofia_state_names[] = {
"REGED", "REGED",
"UNREGISTER", "UNREGISTER",
"FAILED", "FAILED",
"FAIL_WAIT",
"EXPIRED", "EXPIRED",
"NOREG", "NOREG",
NULL NULL

View File

@ -257,6 +257,7 @@ typedef enum {
REG_STATE_REGED, REG_STATE_REGED,
REG_STATE_UNREGISTER, REG_STATE_UNREGISTER,
REG_STATE_FAILED, REG_STATE_FAILED,
REG_STATE_FAIL_WAIT,
REG_STATE_EXPIRED, REG_STATE_EXPIRED,
REG_STATE_NOREG, REG_STATE_NOREG,
REG_STATE_LAST REG_STATE_LAST
@ -333,6 +334,7 @@ struct sofia_gateway {
int deleted; int deleted;
switch_event_t *vars; switch_event_t *vars;
char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1]; char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1];
int failures;
struct sofia_gateway *next; struct sofia_gateway *next;
sofia_gateway_subscription_t *subscriptions; sofia_gateway_subscription_t *subscriptions;
}; };

View File

@ -1001,7 +1001,7 @@ static void parse_gateways(sofia_profile_t *profile, switch_xml_t gateways_tag)
for (param = switch_xml_child(gateway_tag, "param"); param; param = param->next) { for (param = switch_xml_child(gateway_tag, "param"); param; param = param->next) {
char *var = (char *) switch_xml_attr_soft(param, "name"); char *var = (char *) switch_xml_attr_soft(param, "name");
char *val = (char *) switch_xml_attr_soft(param, "value"); char *val = (char *) switch_xml_attr_soft(param, "value");
if (!strcmp(var, "register")) { if (!strcmp(var, "register")) {
register_str = val; register_str = val;
} else if (!strcmp(var, "scheme")) { } else if (!strcmp(var, "scheme")) {
@ -1028,6 +1028,8 @@ static void parse_gateways(sofia_profile_t *profile, switch_xml_t gateways_tag)
expire_seconds = val; expire_seconds = val;
} else if (!strcmp(var, "retry-seconds")) { } else if (!strcmp(var, "retry-seconds")) {
retry_seconds = val; retry_seconds = val;
} else if (!strcmp(var, "retry_seconds")) { // support typo for back compat
retry_seconds = val;
} else if (!strcmp(var, "from-user")) { } else if (!strcmp(var, "from-user")) {
from_user = val; from_user = val;
} else if (!strcmp(var, "from-domain")) { } else if (!strcmp(var, "from-domain")) {
@ -1089,6 +1091,7 @@ static void parse_gateways(sofia_profile_t *profile, switch_xml_t gateways_tag)
if (!switch_true(register_str)) { if (!switch_true(register_str)) {
gateway->state = REG_STATE_NOREG; gateway->state = REG_STATE_NOREG;
gateway->status = SOFIA_GATEWAY_UP;
} }
if (switch_strlen_zero(from_domain)) { if (switch_strlen_zero(from_domain)) {
@ -1108,7 +1111,7 @@ static void parse_gateways(sofia_profile_t *profile, switch_xml_t gateways_tag)
} }
gateway->retry_seconds = atoi(retry_seconds); gateway->retry_seconds = atoi(retry_seconds);
if (gateway->retry_seconds < 10) { if (gateway->retry_seconds < 5) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Invalid retry-seconds of %d on gateway %s, using the value of 30 instead.\n", switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Invalid retry-seconds of %d on gateway %s, using the value of 30 instead.\n",
gateway->retry_seconds, name); gateway->retry_seconds, name);
gateway->retry_seconds = 30; gateway->retry_seconds = 30;
@ -2217,7 +2220,6 @@ static void sofia_handle_sip_r_options(switch_core_session_t *session, int statu
if (status == 200 || status == 404 || status == 501) { if (status == 200 || status == 404 || status == 501) {
if (gateway->state == REG_STATE_FAILED) { if (gateway->state == REG_STATE_FAILED) {
gateway->state = REG_STATE_UNREGED; gateway->state = REG_STATE_UNREGED;
gateway->retry = 0;
} }
gateway->status = SOFIA_GATEWAY_UP; gateway->status = SOFIA_GATEWAY_UP;
} else { } else {

View File

@ -228,13 +228,18 @@ void sofia_reg_check_gateway(sofia_profile_t *profile, time_t now)
switch (ostate) { switch (ostate) {
case REG_STATE_NOREG: case REG_STATE_NOREG:
gateway_ptr->status = SOFIA_GATEWAY_UP; if (!gateway_ptr->ping && !gateway_ptr->pinging) {
gateway_ptr->status = SOFIA_GATEWAY_UP;
}
break; break;
case REG_STATE_REGISTER: case REG_STATE_REGISTER:
if (profile->debug) { if (profile->debug) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Registered %s\n", gateway_ptr->name); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Registered %s\n", gateway_ptr->name);
} }
if (gateway_ptr->expires > 60) {
gateway_ptr->failures = 0;
if (gateway_ptr->freq > 60) {
gateway_ptr->expires = now + (gateway_ptr->freq - 15); gateway_ptr->expires = now + (gateway_ptr->freq - 15);
} else { } else {
gateway_ptr->expires = now + (gateway_ptr->freq - 2); gateway_ptr->expires = now + (gateway_ptr->freq - 2);
@ -251,7 +256,7 @@ void sofia_reg_check_gateway(sofia_profile_t *profile, time_t now)
case REG_STATE_UNREGED: case REG_STATE_UNREGED:
gateway_ptr->status = SOFIA_GATEWAY_DOWN; gateway_ptr->status = SOFIA_GATEWAY_DOWN;
sofia_reg_kill_reg(gateway_ptr, 0); sofia_reg_kill_reg(gateway_ptr, 0);
if ((gateway_ptr->nh = nua_handle(gateway_ptr->profile->nua, NULL, if ((gateway_ptr->nh = nua_handle(gateway_ptr->profile->nua, NULL,
SIPTAG_CALL_ID_STR(gateway_ptr->uuid_str), SIPTAG_CALL_ID_STR(gateway_ptr->uuid_str),
NUTAG_URL(gateway_ptr->register_proxy), NUTAG_URL(gateway_ptr->register_proxy),
@ -289,22 +294,31 @@ void sofia_reg_check_gateway(sofia_profile_t *profile, time_t now)
NUTAG_REGISTRAR(gateway_ptr->register_proxy), NUTAG_REGISTRAR(gateway_ptr->register_proxy),
NUTAG_OUTBOUND("no-options-keepalive"), NUTAG_OUTBOUND("no-validate"), NUTAG_KEEPALIVE(0), TAG_NULL()); NUTAG_OUTBOUND("no-options-keepalive"), NUTAG_OUTBOUND("no-validate"), NUTAG_KEEPALIVE(0), TAG_NULL());
} }
gateway_ptr->retry = now + gateway_ptr->retry_seconds;
gateway_ptr->state = REG_STATE_TRYING; gateway_ptr->state = REG_STATE_TRYING;
} else { } else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error registering %s\n", gateway_ptr->name); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error registering %s failure #%d\n", gateway_ptr->name, ++gateway_ptr->failures);
gateway_ptr->state = REG_STATE_FAILED; gateway_ptr->state = REG_STATE_FAILED;
} }
break; break;
case REG_STATE_FAILED: case REG_STATE_FAILED:
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "%s Failed Registration, setting retry to %d seconds.\n",
gateway_ptr->name, gateway_ptr->retry_seconds * (gateway_ptr->failures + 1));
gateway_ptr->retry = now + (gateway_ptr->retry_seconds * (gateway_ptr->failures + 1));
sofia_reg_kill_reg(gateway_ptr, 0); sofia_reg_kill_reg(gateway_ptr, 0);
gateway_ptr->status = SOFIA_GATEWAY_DOWN; gateway_ptr->status = SOFIA_GATEWAY_DOWN;
case REG_STATE_TRYING: gateway_ptr->state = REG_STATE_FAIL_WAIT;
if (gateway_ptr->retry && now >= gateway_ptr->retry) { break;
case REG_STATE_FAIL_WAIT:
if (!gateway_ptr->retry || now >= gateway_ptr->retry) {
gateway_ptr->state = REG_STATE_UNREGED; gateway_ptr->state = REG_STATE_UNREGED;
gateway_ptr->retry = 0; }
break;
case REG_STATE_TRYING:
if (!gateway_ptr->retry || now >= gateway_ptr->retry) {
gateway_ptr->state = REG_STATE_FAILED;
} }
break; break;
default: default:
@ -1196,7 +1210,8 @@ void sofia_reg_handle_sip_r_register(int status,
break; break;
default: default:
sofia_private->gateway->state = REG_STATE_FAILED; sofia_private->gateway->state = REG_STATE_FAILED;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Registration Failed with status %d\n", status); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "%s Registration Failed with status %d. failure #%d\n",
sofia_private->gateway->name, status, ++sofia_private->gateway->failures);
break; break;
} }
if (ostate != sofia_private->gateway->state) { if (ostate != sofia_private->gateway->state) {