Simplify the timeout logic. Make comments more clear. - geomyidae - A small C-based gopherd. HTML git clone git://bitreich.org/geomyidae/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/geomyidae/ DIR Log DIR Files DIR Refs DIR Tags DIR README DIR LICENSE --- DIR commit f6d49dbdcefb8ff41d19939401efe8d11054e592 DIR parent c8c0cd126424dfd23ff7df6d60f32e1bb18deff6 HTML Author: Christoph Lohmann <20h@r-36.net> Date: Sun, 18 Feb 2018 11:23:48 +0100 Simplify the timeout logic. Make comments more clear. Diffstat: M ind.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) --- DIR diff --git a/ind.c b/ind.c @@ -78,29 +78,32 @@ pendingbytes(int sock) void waitforpendingbytes(int sock) { - int npending, opending, tries; + int npending, opending, trytime; - npending = opending = tries = 0; + npending = opending = 0; + trytime = 10; /* - * Wait until there is nothing pending or the connection stalled for - * 30 seconds. + * Wait until there is nothing pending or the connection stalled + * (nothing was sent) for around 40 seconds. Beware, trytime is + * an exponential wait. */ - while ((npending = pendingbytes(sock)) > 0 && tries < 30000000) { + while ((npending = pendingbytes(sock)) > 0 && trytime < 20000000) { if (opending != 0) { if (opending != npending) { - tries = 0; + trytime = 10; } else { - if (tries == 0) { - tries = 1; - } else { - tries += tries; - } + /* + * Exponentially increase the usleep + * waiting time to not waste CPU + * resources. + */ + trytime += trytime; } } opending = npending; - usleep(tries); + usleep(trytime); } }