Skip to content

Commit

Permalink
For #820, fix utest failed.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Mar 26, 2017
1 parent e3526c0 commit 7d618fe
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 79 deletions.
112 changes: 67 additions & 45 deletions trunk/src/app/srs_app_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3540,6 +3540,21 @@ int SrsConfig::check_config()
{
int ret = ERROR_SUCCESS;

if ((ret = check_normal_config()) != ERROR_SUCCESS) {
return ret;
}

if ((ret = check_number_connections()) != ERROR_SUCCESS) {
return ret;
}

return ret;
}

int SrsConfig::check_normal_config()
{
int ret = ERROR_SUCCESS;

srs_trace("srs checking config...");

////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -3659,51 +3674,6 @@ int SrsConfig::check_config()
}
}

////////////////////////////////////////////////////////////////////////
// check max connections
////////////////////////////////////////////////////////////////////////
if (get_max_connections() <= 0) {
ret = ERROR_SYSTEM_CONFIG_INVALID;
srs_error("directive max_connections invalid, max_connections=%d, ret=%d", get_max_connections(), ret);
return ret;
}

// check max connections of system limits
if (true) {
int nb_consumed_fds = (int)get_listens().size();
if (!get_http_api_listen().empty()) {
nb_consumed_fds++;
}
if (!get_http_stream_listen().empty()) {
nb_consumed_fds++;
}
if (get_log_tank_file()) {
nb_consumed_fds++;
}
// 0, 1, 2 for stdin, stdout and stderr.
nb_consumed_fds += 3;

int nb_connections = get_max_connections();
int nb_total = nb_connections + nb_consumed_fds;

int max_open_files = (int)sysconf(_SC_OPEN_MAX);
int nb_canbe = max_open_files - nb_consumed_fds - 1;

// for each play connections, we open a pipe(2fds) to convert SrsConsumver to io,
// refine performance, @see: https://github.com/ossrs/srs/issues/194
if (nb_total >= max_open_files) {
ret = ERROR_SYSTEM_CONFIG_INVALID;
srs_error("invalid max_connections=%d, required=%d, system limit to %d, "
"total=%d(max_connections=%d, nb_consumed_fds=%d), ret=%d. "
"you can change max_connections from %d to %d, or "
"you can login as root and set the limit: ulimit -HSn %d",
nb_connections, nb_total + 100, max_open_files,
nb_total, nb_connections, nb_consumed_fds,
ret, nb_connections, nb_canbe, nb_total + 100);
return ret;
}
}

////////////////////////////////////////////////////////////////////////
// check heartbeat
////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -4097,6 +4067,58 @@ int SrsConfig::check_config()
return ret;
}

int SrsConfig::check_number_connections()
{
int ret = ERROR_SUCCESS;

////////////////////////////////////////////////////////////////////////
// check max connections
////////////////////////////////////////////////////////////////////////
if (get_max_connections() <= 0) {
ret = ERROR_SYSTEM_CONFIG_INVALID;
srs_error("directive max_connections invalid, max_connections=%d, ret=%d", get_max_connections(), ret);
return ret;
}

// check max connections of system limits
if (true) {
int nb_consumed_fds = (int)get_listens().size();
if (!get_http_api_listen().empty()) {
nb_consumed_fds++;
}
if (!get_http_stream_listen().empty()) {
nb_consumed_fds++;
}
if (get_log_tank_file()) {
nb_consumed_fds++;
}
// 0, 1, 2 for stdin, stdout and stderr.
nb_consumed_fds += 3;

int nb_connections = get_max_connections();
int nb_total = nb_connections + nb_consumed_fds;

int max_open_files = (int)sysconf(_SC_OPEN_MAX);
int nb_canbe = max_open_files - nb_consumed_fds - 1;

// for each play connections, we open a pipe(2fds) to convert SrsConsumver to io,
// refine performance, @see: https://github.com/ossrs/srs/issues/194
if (nb_total >= max_open_files) {
ret = ERROR_SYSTEM_CONFIG_INVALID;
srs_error("invalid max_connections=%d, required=%d, system limit to %d, "
"total=%d(max_connections=%d, nb_consumed_fds=%d), ret=%d. "
"you can change max_connections from %d to %d, or "
"you can login as root and set the limit: ulimit -HSn %d",
nb_connections, nb_total + 100, max_open_files,
nb_total, nb_connections, nb_consumed_fds,
ret, nb_connections, nb_canbe, nb_total + 100);
return ret;
}
}

return ret;
}

int SrsConfig::parse_buffer(SrsConfigBuffer* buffer)
{
int ret = ERROR_SUCCESS;
Expand Down
3 changes: 3 additions & 0 deletions trunk/src/app/srs_app_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,9 @@ class SrsConfig
* check the parsed config.
*/
virtual int check_config();
protected:
virtual int check_normal_config();
virtual int check_number_connections();
protected:
/**
* parse config from the buffer.
Expand Down
35 changes: 1 addition & 34 deletions trunk/src/utest/srs_utest_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ int MockSrsConfig::parse(string buf)
return ret;
}

return check_config();
return check_normal_config();
}

#ifdef ENABLE_UTEST_CONFIG
Expand Down Expand Up @@ -829,39 +829,6 @@ VOID TEST(ConfigMainTest, CheckConf_srs_log_file)
}
}

VOID TEST(ConfigMainTest, CheckConf_max_connections)
{
if (true) {
MockSrsConfig conf;
EXPECT_TRUE(ERROR_SUCCESS == conf.parse(_MIN_OK_CONF"max_connections 1000;"));
}

if (true) {
MockSrsConfig conf;
EXPECT_TRUE(ERROR_SUCCESS != conf.parse(_MIN_OK_CONF"max_connectionss 1000;"));
}

if (true) {
MockSrsConfig conf;
EXPECT_TRUE(ERROR_SUCCESS != conf.parse(_MIN_OK_CONF"max_connections 0;"));
}

if (true) {
MockSrsConfig conf;
EXPECT_TRUE(ERROR_SUCCESS != conf.parse(_MIN_OK_CONF"max_connections 1000000;"));
}

if (true) {
MockSrsConfig conf;
EXPECT_TRUE(ERROR_SUCCESS != conf.parse(_MIN_OK_CONF"max_connections -1;"));
}

if (true) {
MockSrsConfig conf;
EXPECT_TRUE(ERROR_SUCCESS != conf.parse(_MIN_OK_CONF"max_connections -1024;"));
}
}

VOID TEST(ConfigMainTest, CheckConf_daemon)
{
if (true) {
Expand Down

0 comments on commit 7d618fe

Please sign in to comment.