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 [36.81.65.188]) by gnuweeb.org (Postfix) with ESMTPSA id 76FD37E377; Fri, 8 Jul 2022 12:10:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1657282252; bh=aJ7lJJNOip+CvoJbfXqqVJbYtSb1qMAzeEfckmAa9t8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pfKx9Q0FzFJ1ZkMvddKu9SuoHVnUy3H1HgmDZAcXkX1syW55l+784fnyn+GkHiJbC X5oZrLdIKhiPSW4vdn5tgJ+pvOZXm4Tb58WHGt9TByRtc6bv7WbVKaz1d+DtCgkKSf 5OrsB5Sha2AfZFEllBynAtWJfASTNpIJhnd3LYWVMqAzxg30D5nTVHXQgen7P/0z51 7zTDmxdcmISAoPD+PSRt2Cx6f8kiyruwMYE06xOYqYIvfHmYOaw7ZcqAzrF5r3OmK5 nJ5fSRhxXl79TAuin9dmfHX2cdgXATidtR4Ae0/Xyg4wl44wXWWrtYeepX9z4e0lgE jaLjlQxGvopbg== From: Ammar Faizi To: GNU/Weeb Mailing List Cc: Ammar Faizi , Alviro Iskandar Setiawan , Arthur Lapz , Fernanda Ma'rouf , Sprite , Yonle Subject: [PATCH gwhttpd 06/14] gwhttpd: Avoid endless busy spinning on `send()` Date: Fri, 8 Jul 2022 19:10:17 +0700 Message-Id: <20220708121025.926162-7-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220708121025.926162-1-ammarfaizi2@gnuweeb.org> References: <20220708121025.926162-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