From 326b2ea49dab81ac5adb3f8e30d340943e13a602 Mon Sep 17 00:00:00 2001 From: Peter Lieven Date: Thu, 18 Oct 2012 12:18:40 +0200 Subject: [PATCH] Add backoff mechanism to iscsi_reconnect This patch adds a linear backoff mechanishm + jitter in case a reconnect fails. If there is a longer outage this is to avoid a large amount of simultaneous connects to the storage. --- lib/connect.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/connect.c b/lib/connect.c index 23ed62f..f82df28 100644 --- a/lib/connect.c +++ b/lib/connect.c @@ -209,6 +209,9 @@ int iscsi_reconnect(struct iscsi_context *old_iscsi) return 0; } + int retry = 0; + srand (time(NULL)^getpid()); + try_again: iscsi = iscsi_create_context(old_iscsi->initiator_name); @@ -234,7 +237,17 @@ try_again: if (iscsi_full_connect_sync(iscsi, iscsi->portal, iscsi->lun) != 0) { iscsi_destroy_context(iscsi); - sleep(1); + int backoff=retry; + if (backoff > 10) { + backoff+=rand()%10; + backoff-=5; + } + if (backoff > 30) { + backoff=30; + } + DPRINTF(iscsi,1,"reconnect try %d failed, waiting %d seconds",retry,backoff); + sleep(backoff); + retry++; goto try_again; }