From e275c3823b203c965bc114db7a6df927f220c79c Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Sat, 5 Mar 2016 15:40:36 +0100 Subject: [PATCH] make initial ubus connect delay small, use exponential backoff Signed-off-by: Felix Fietkau --- ubus.c | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/ubus.c b/ubus.c index 211a1ec..8d521ac 100644 --- a/ubus.c +++ b/ubus.c @@ -22,21 +22,38 @@ char *ubus_socket = NULL; static struct ubus_context *ctx; static struct uloop_timeout ubus_timer; +static int timeout; + +static void reset_timeout(void) +{ + timeout = 50; +} + +static void timeout_retry(void) +{ + uloop_timeout_set(&ubus_timer, timeout); + timeout *= 2; + if (timeout > 1000) + timeout = 1000; +} static void ubus_reconnect_cb(struct uloop_timeout *timeout) { - if (!ubus_reconnect(ctx, ubus_socket)) + if (!ubus_reconnect(ctx, ubus_socket)) { ubus_add_uloop(ctx); - else - uloop_timeout_set(timeout, 2000); + return; + } + + timeout_retry(); } static void ubus_disconnect_cb(struct ubus_context *ctx) { ubus_timer.cb = ubus_reconnect_cb; - uloop_timeout_set(&ubus_timer, 2000); + reset_timeout(); + timeout_retry(); } static void @@ -46,7 +63,7 @@ ubus_connect_cb(struct uloop_timeout *timeout) if (!ctx) { DEBUG(4, "Connection to ubus failed\n"); - uloop_timeout_set(&ubus_timer, 1000); + timeout_retry(); return; } @@ -56,6 +73,7 @@ ubus_connect_cb(struct uloop_timeout *timeout) watch_ubus(ctx); DEBUG(2, "Connected to ubus, id=%08x\n", ctx->local_id); + reset_timeout(); ubus_add_uloop(ctx); procd_state_ubus_connect(); } @@ -64,5 +82,6 @@ void procd_connect_ubus(void) { ubus_timer.cb = ubus_connect_cb; - uloop_timeout_set(&ubus_timer, 1000); + reset_timeout(); + timeout_retry(); } -- 2.11.0