Skip to content

Commit

Permalink
Fix #2134, improve setting parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
twose committed Nov 20, 2018
1 parent a4f53c6 commit 0d64a0d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 32 deletions.
11 changes: 3 additions & 8 deletions src/core/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 10 additions & 7 deletions src/network/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/network/thread_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
32 changes: 16 additions & 16 deletions swoole_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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))
Expand Down Expand Up @@ -2300,15 +2300,15 @@ 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
*/
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))
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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)
{
Expand All @@ -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))
Expand Down Expand Up @@ -2555,21 +2555,21 @@ 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
*/
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;
Expand Down

0 comments on commit 0d64a0d

Please sign in to comment.