From 8dd6a05d387e83ce7376257e45b647472f2b50d2 Mon Sep 17 00:00:00 2001 From: Xinwei Xiong <3293172751@qq.com> Date: Sun, 8 Oct 2023 10:59:15 +0800 Subject: [PATCH] fix: Implement Enhanced File Descriptor Limit Handling in Shell Script (#1160) Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> --- scripts/install/openim-msggateway.sh | 4 ++-- scripts/install/openim-msgtransfer.sh | 4 ++-- scripts/lib/util.sh | 32 +++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/scripts/install/openim-msggateway.sh b/scripts/install/openim-msggateway.sh index c51c20646..612b7ae16 100755 --- a/scripts/install/openim-msggateway.sh +++ b/scripts/install/openim-msggateway.sh @@ -18,11 +18,11 @@ set -o errexit set +o nounset set -o pipefail -ulimit -n 200000 - OPENIM_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")"/../.. && pwd -P) [[ -z ${COMMON_SOURCED} ]] && source "${OPENIM_ROOT}"/scripts/install/common.sh +openim::util::set_max_fd 200000 + SERVER_NAME="openim-msggateway" function openim::msggateway::start() diff --git a/scripts/install/openim-msgtransfer.sh b/scripts/install/openim-msgtransfer.sh index 428f6b64e..51ac78850 100755 --- a/scripts/install/openim-msgtransfer.sh +++ b/scripts/install/openim-msgtransfer.sh @@ -18,11 +18,11 @@ set -o errexit set +o nounset set -o pipefail -ulimit -n 200000 - OPENIM_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")"/../.. && pwd -P) [[ -z ${COMMON_SOURCED} ]] && source "${OPENIM_ROOT}"/scripts/install/common.sh +openim::util::set_max_fd 200000 + SERVER_NAME="openim-msgtransfer" function openim::msgtransfer::start() diff --git a/scripts/lib/util.sh b/scripts/lib/util.sh index 2455f91dc..0ad386e1a 100755 --- a/scripts/lib/util.sh +++ b/scripts/lib/util.sh @@ -1250,6 +1250,38 @@ function openim::util::gencpu() { echo $cpu_count } +function openim::util::set_max_fd() { + local desired_fd=$1 + local max_fd_limit + + # Check if we're not on cygwin or darwin. + if [ "$(uname -s | tr '[:upper:]' '[:lower:]')" != "cygwin" ] && [ "$(uname -s | tr '[:upper:]' '[:lower:]')" != "darwin" ]; then + # Try to get the hard limit. + max_fd_limit=$(ulimit -H -n) + if [ $? -eq 0 ]; then + # If desired_fd is 'maximum' or 'max', set it to the hard limit. + if [ "$desired_fd" = "maximum" ] || [ "$desired_fd" = "max" ]; then + desired_fd="$max_fd_limit" + fi + + # Check if desired_fd is less than or equal to max_fd_limit. + if [ "$desired_fd" -le "$max_fd_limit" ]; then + ulimit -n "$desired_fd" + if [ $? -ne 0 ]; then + echo "Warning: Could not set maximum file descriptor limit to $desired_fd" + fi + else + echo "Warning: Desired file descriptor limit ($desired_fd) is greater than the hard limit ($max_fd_limit)" + fi + else + echo "Warning: Could not query the maximum file descriptor hard limit." + fi + else + echo "Warning: Not attempting to modify file descriptor limit on Cygwin or Darwin." + fi +} + + function openim::util::gen_os_arch() { # Get the current operating system and architecture OS=$(uname -s | tr '[:upper:]' '[:lower:]')