From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from integral2.. (unknown [125.160.108.65]) by gnuweeb.org (Postfix) with ESMTPSA id DBC307F9AE; Sun, 26 Jun 2022 05:57:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1656223023; bh=aJ7lJJNOip+CvoJbfXqqVJbYtSb1qMAzeEfckmAa9t8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Zy3QC/RpRZV77P8vguveXb0elh+7hHS6B+qlKdITU2uoMjr9MesScEBzV06eWAx++ YO7QJ54sFqAyAgD5RjD85RkiYXKy5cnG52zyB2ewLW27PvPWsPD/iWJ+eH58KP4VB2 jVyXHdR20NHbqBugC9TVEvzVEKGDgl98Ho3gOSUM5Uwo6LxZhTCa8ml7WgmSK7q1gS 0mZ9APgSjpHFyRBRRVx2ozSuCUCyIj++ficjdGV2xHN+YrrzAanOH7GNffwkt9Z0Wr nV5IqlQoJZ94ZeuZHEvmxJJCSIME25/xDljvR9u8rcnbpYjryukNugPcPy88SEOqrd t5PvkYmFjzEug== From: Ammar Faizi To: GNU/Weeb Mailing List Cc: Ammar Faizi , Arthur Lapz , Sprite , Yonle Subject: [PATCH gwhttpd 6/7] gwhttpd: Avoid endless busy spinning on `send()` Date: Sun, 26 Jun 2022 12:56:33 +0700 Message-Id: <20220626055634.676175-7-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220626055634.676175-1-ammarfaizi2@gnuweeb.org> References: <20220626055634.676175-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: When we fail to send() due to EAGAIN, we retry the send(). However, if we are spinning on this retry for so long, this will eat CPU cycle and slow down the entire application. We should stop retrying at some point. Add a loop counter and return -ENETDOWN if we are failing too many times in the send() retry loop cycle. Signed-off-by: Ammar Faizi --- gwhttpd.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/gwhttpd.cpp b/gwhttpd.cpp index fbe219d..73a1d84 100644 --- a/gwhttpd.cpp +++ b/gwhttpd.cpp @@ -312,10 +312,14 @@ static int handle_new_client(struct server_state *state) static ssize_t send_to_client(struct client_sess *sess, const char *buf, size_t len) { + constexpr uint32_t max_try = 10; + uint32_t try_count = 0; ssize_t ret; int tmp; repeat: + if (unlikely(try_count++ >= max_try)) + return -ENETDOWN; ret = send(sess->fd, buf, len, MSG_DONTWAIT); if (unlikely(ret < 0)) { tmp = errno; -- Ammar Faizi