diff --git a/src/core/base.c b/src/core/base.c index 4f0c0805bcf..0b0577bf9d2 100644 --- a/src/core/base.c +++ b/src/core/base.c @@ -96,22 +96,17 @@ void swoole_init(void) swMutex_create(&SwooleGS->lock_2, 1); swMutex_create(&SwooleG.lock, 0); -#ifdef _WIN32 SwooleG.max_sockets = 1024; -#else +#ifndef _WIN32 struct rlimit rlmt; if (getrlimit(RLIMIT_NOFILE, &rlmt) < 0) { swWarn("getrlimit() failed. Error: %s[%d]", strerror(errno), errno); - SwooleG.max_sockets = 1024; } else { - SwooleG.max_sockets = (uint32_t) rlmt.rlim_cur; - if (SwooleG.max_sockets > SW_MAX_CONNECTION) - { - SwooleG.max_sockets = SW_MAX_CONNECTION; - } + SwooleG.max_sockets = MAX((uint32_t) rlmt.rlim_cur, 1024); + SwooleG.max_sockets = MIN((uint32_t) rlmt.rlim_cur, SW_SESSION_LIST_SIZE); } #endif diff --git a/src/network/server.c b/src/network/server.c index 658b4511d6a..995a1ec3f5e 100644 --- a/src/network/server.c +++ b/src/network/server.c @@ -267,21 +267,24 @@ static int swServer_start_check(swServer *serv) { serv->reactor_num = serv->worker_num; } - if (SwooleG.max_sockets > 0 && serv->max_connection > SwooleG.max_sockets) + // max connections + uint32_t minimum_connection = (serv->worker_num + serv->task_worker_num) * 2 + 32; + if (serv->max_connection < minimum_connection) { - swWarn("serv->max_connection is exceed the maximum value[%d].", SwooleG.max_sockets); serv->max_connection = SwooleG.max_sockets; + swWarn("serv->max_connection must be bigger than %u, it's reset to %u", minimum_connection, SwooleG.max_sockets); } - if (serv->max_connection < (serv->worker_num + serv->task_worker_num) * 2 + 32) + else if (SwooleG.max_sockets > 0 && serv->max_connection > SwooleG.max_sockets) { - swWarn("serv->max_connection is too small."); serv->max_connection = SwooleG.max_sockets; + swWarn("serv->max_connection is exceed the maximum value, it's reset to %u.", SwooleG.max_sockets); } - if (serv->max_connection > SW_SESSION_LIST_SIZE) + else if (serv->max_connection > SW_SESSION_LIST_SIZE) { - swWarn("serv->max_connection is exceed the SW_SESSION_LIST_SIZE[%d].", SW_SESSION_LIST_SIZE); serv->max_connection = SW_SESSION_LIST_SIZE; + swWarn("serv->max_connection is exceed the SW_SESSION_LIST_SIZE, it's reset to %u.", SW_SESSION_LIST_SIZE); } + // package max length swListenPort *ls; LL_FOREACH(serv->listen_list, ls) { @@ -800,7 +803,7 @@ void swServer_init(swServer *serv) serv->dispatch_mode = SW_DISPATCH_FDMOD; serv->worker_num = SW_CPU_NUM; - serv->max_connection = SwooleG.max_sockets < SW_SESSION_LIST_SIZE ? SwooleG.max_sockets : SW_SESSION_LIST_SIZE; + serv->max_connection = MIN(SW_MAX_CONNECTION, SwooleG.max_sockets); serv->max_request = 0; serv->max_wait_time = SW_WORKER_MAX_WAIT_TIME; diff --git a/src/network/thread_pool.c b/src/network/thread_pool.c index adaa8795729..9f56e61bc64 100644 --- a/src/network/thread_pool.c +++ b/src/network/thread_pool.c @@ -42,7 +42,7 @@ int swThreadPool_create(swThreadPool *pool, int thread_num) return SW_ERR; } #else - int size = SwooleG.max_sockets >= SW_THREADPOOL_QUEUE_LEN ? SwooleG.max_sockets + 1 : SW_THREADPOOL_QUEUE_LEN; + int size = MAX(SwooleG.max_sockets + 1, SW_THREADPOOL_QUEUE_LEN); if (swRingQueue_init(&pool->queue, size) < 0) { return SW_ERR; diff --git a/swoole_server.c b/swoole_server.c index f2be7f9ecf0..4ad2b04c16e 100644 --- a/swoole_server.c +++ b/swoole_server.c @@ -2197,7 +2197,7 @@ PHP_METHOD(swoole_server, set) if (php_swoole_array_get_value(vht, "reactor_num", v)) { convert_to_long(v); - serv->reactor_num = (int) Z_LVAL_P(v); + serv->reactor_num = (uint16_t) Z_LVAL_P(v); if (serv->reactor_num <= 0) { serv->reactor_num = SwooleG.cpu_num; @@ -2207,7 +2207,7 @@ PHP_METHOD(swoole_server, set) if (php_swoole_array_get_value(vht, "worker_num", v)) { convert_to_long(v); - serv->worker_num = (int) Z_LVAL_P(v); + serv->worker_num = (uint16_t) Z_LVAL_P(v); if (serv->worker_num <= 0) { serv->worker_num = SwooleG.cpu_num; @@ -2228,7 +2228,7 @@ PHP_METHOD(swoole_server, set) if (php_swoole_array_get_value(vht, "max_coro_num", v) || php_swoole_array_get_value(vht, "max_coroutine", v)) { convert_to_long(v); - COROG.max_coro_num = (int) Z_LVAL_P(v); + COROG.max_coro_num = (uint32_t) Z_LVAL_P(v); if (COROG.max_coro_num <= 0) { COROG.max_coro_num = SW_DEFAULT_MAX_CORO_NUM; @@ -2253,7 +2253,7 @@ PHP_METHOD(swoole_server, set) if (php_swoole_array_get_value(vht, "dispatch_mode", v)) { convert_to_long(v); - serv->dispatch_mode = (int) Z_LVAL_P(v); + serv->dispatch_mode = (uint8_t) Z_LVAL_P(v); } //dispatch function if (php_swoole_array_get_value(vht, "dispatch_func", v)) @@ -2300,7 +2300,7 @@ PHP_METHOD(swoole_server, set) if (php_swoole_array_get_value(vht, "log_level", v)) { convert_to_long(v); - SwooleG.log_level = (int) Z_LVAL_P(v); + SwooleG.log_level = (uint8_t) Z_LVAL_P(v); } /** * for dispatch_mode = 1/3 @@ -2308,7 +2308,7 @@ PHP_METHOD(swoole_server, set) if (php_swoole_array_get_value(vht, "discard_timeout_request", v)) { convert_to_boolean(v); - serv->discard_timeout_request = Z_BVAL_P(v); + serv->discard_timeout_request = (uint32_t) Z_BVAL_P(v); } //onConnect/onClose event if (php_swoole_array_get_value(vht, "enable_unsafe_event", v)) @@ -2332,7 +2332,7 @@ PHP_METHOD(swoole_server, set) if (php_swoole_array_get_value(vht, "task_worker_num", v)) { convert_to_long(v); - serv->task_worker_num = (int) Z_LVAL_P(v); + serv->task_worker_num = (uint16_t) Z_LVAL_P(v); if (task_callbacks == NULL) { task_callbacks = swHashMap_new(1024, NULL); @@ -2373,7 +2373,7 @@ PHP_METHOD(swoole_server, set) if (php_swoole_array_get_value(vht, "task_ipc_mode", v)) { convert_to_long(v); - serv->task_ipc_mode = (int) Z_LVAL_P(v); + serv->task_ipc_mode = (uint8_t) Z_LVAL_P(v); } /** * Temporary file directory for task_worker @@ -2397,25 +2397,25 @@ PHP_METHOD(swoole_server, set) if (php_swoole_array_get_value(vht, "task_max_request", v)) { convert_to_long(v); - serv->task_max_request = (int) Z_LVAL_P(v); + serv->task_max_request = (uint16_t) Z_LVAL_P(v); } //max_connection if (php_swoole_array_get_value(vht, "max_connection", v) || php_swoole_array_get_value(vht, "max_conn", v)) { convert_to_long(v); - serv->max_connection = (int) Z_LVAL_P(v); + serv->max_connection = (uint32_t) Z_LVAL_P(v); } //heartbeat_check_interval if (php_swoole_array_get_value(vht, "heartbeat_check_interval", v)) { convert_to_long(v); - serv->heartbeat_check_interval = (int) Z_LVAL_P(v); + serv->heartbeat_check_interval = (uint16_t) Z_LVAL_P(v); } //heartbeat idle time if (php_swoole_array_get_value(vht, "heartbeat_idle_time", v)) { convert_to_long(v); - serv->heartbeat_idle_time = (int) Z_LVAL_P(v); + serv->heartbeat_idle_time = (uint16_t) Z_LVAL_P(v); if (serv->heartbeat_check_interval > serv->heartbeat_idle_time) { @@ -2431,7 +2431,7 @@ PHP_METHOD(swoole_server, set) if (php_swoole_array_get_value(vht, "max_request", v)) { convert_to_long(v); - serv->max_request = (int) Z_LVAL_P(v); + serv->max_request = (uint32_t) Z_LVAL_P(v); } //reload async if (php_swoole_array_get_value(vht, "reload_async", v)) @@ -2555,7 +2555,7 @@ PHP_METHOD(swoole_server, set) if (php_swoole_array_get_value(vht, "buffer_input_size", v)) { convert_to_long(v); - serv->buffer_input_size = (int) Z_LVAL_P(v); + serv->buffer_input_size = (uint32_t) Z_LVAL_P(v); } /** * buffer output size @@ -2563,13 +2563,13 @@ PHP_METHOD(swoole_server, set) if (php_swoole_array_get_value(vht, "buffer_output_size", v)) { convert_to_long(v); - serv->buffer_output_size = (int) Z_LVAL_P(v); + serv->buffer_output_size = (uint32_t) Z_LVAL_P(v); } //message queue key if (php_swoole_array_get_value(vht, "message_queue_key", v)) { convert_to_long(v); - serv->message_queue_key = (int) Z_LVAL_P(v); + serv->message_queue_key = (uint64_t) Z_LVAL_P(v); } zval *retval = NULL;