[Asterisk-code-review] res xmpp: Use incremental backoff when a read error occurs (asterisk[14])

Sean Bright asteriskteam at digium.com
Thu Mar 23 10:04:07 CDT 2017


Sean Bright has uploaded a new change for review. ( https://gerrit.asterisk.org/5303 )

Change subject: res_xmpp: Use incremental backoff when a read error occurs
......................................................................

res_xmpp: Use incremental backoff when a read error occurs

If a read error occurs, we immediately attempt a reconnect without any
delay. Instead, let's sleep and backoff up to 60 seconds before we try
again.

ASTERISK-24712 #close
Reported by: Matthias Urlichs

Change-Id: I6fe10ef4734837727437beab715e336777f13f48
---
M res/res_xmpp.c
1 file changed, 21 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/03/5303/1

diff --git a/res/res_xmpp.c b/res/res_xmpp.c
index 32e1dd1..b7d3a86 100644
--- a/res/res_xmpp.c
+++ b/res/res_xmpp.c
@@ -3567,6 +3567,7 @@
 {
 	if ((client->thread != AST_PTHREADT_NULL) && !pthread_equal(pthread_self(), client->thread)) {
 		xmpp_client_change_state(client, XMPP_STATE_DISCONNECTING);
+		pthread_cancel(client->thread);
 		pthread_join(client->thread, NULL);
 		client->thread = AST_PTHREADT_NULL;
 	}
@@ -3746,11 +3747,26 @@
 	return IKS_OK;
 }
 
+static void sleep_with_backoff(unsigned int *sleep_time)
+{
+	/* We're OK with our thread dying here */
+	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
+
+	sleep(*sleep_time);
+	*sleep_time = MIN(60, *sleep_time * 2);
+
+	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
+}
+
 /*! \brief XMPP client connection thread */
 static void *xmpp_client_thread(void *data)
 {
 	struct ast_xmpp_client *client = data;
 	int res = IKS_NET_RWERR;
+	unsigned int sleep_time = 1;
+
+	/* We only allow cancellation while sleeping */
+	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
 
 	do {
 		if (client->state == XMPP_STATE_DISCONNECTING) {
@@ -3761,7 +3777,7 @@
 		if (res == IKS_NET_RWERR || client->timeout == 0) {
 			ast_debug(3, "Connecting client '%s'\n", client->name);
 			if ((res = xmpp_client_reconnect(client)) != IKS_OK) {
-				sleep(4);
+				sleep_with_backoff(&sleep_time);
 				res = IKS_NET_RWERR;
 			}
 			continue;
@@ -3800,6 +3816,8 @@
 			}
 		} else if (res == IKS_NET_RWERR) {
 			ast_log(LOG_WARNING, "JABBER: socket read error\n");
+			ast_xmpp_client_disconnect(client);
+			sleep_with_backoff(&sleep_time);
 		} else if (res == IKS_NET_NOSOCK) {
 			ast_log(LOG_WARNING, "JABBER: No Socket\n");
 		} else if (res == IKS_NET_NOCONN) {
@@ -3812,6 +3830,8 @@
 			ast_log(LOG_WARNING, "JABBER: Dropped?\n");
 		} else if (res == IKS_NET_UNKNOWN) {
 			ast_debug(5, "JABBER: Unknown\n");
+		} else if (res == IKS_OK) {
+			sleep_time = 1;
 		}
 
 	} while (1);

-- 
To view, visit https://gerrit.asterisk.org/5303
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6fe10ef4734837727437beab715e336777f13f48
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 14
Gerrit-Owner: Sean Bright <sean.bright at gmail.com>



More information about the asterisk-code-review mailing list