Skip to content

Commit

Permalink
Fixed remove serv->connect_count, replace with maxfd
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Jun 9, 2014
1 parent 3a60cb7 commit 96ec134
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 35 deletions.
5 changes: 3 additions & 2 deletions examples/server/echo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
$serv = new swoole_server("0.0.0.0", 9501);
$serv->set(array(
//'tcp_defer_accept' => 5,
'worker_num' => 1,
'worker_num' => 2,
//'daemonize' => true,
//'log_file' => '/tmp/swoole.log'
));
$serv->on('timer', function($serv, $interval) {
echo "onTimer: $interval\n";
});
$serv->on('start', function($serv) {
$serv->addtimer(1000);
//$serv->addtimer(1000);
});
$serv->on('workerStart', function($serv, $worker_id) {
//echo "server start\n";
//if($worker_id == 0) $serv->addtimer(1000);
});
$serv->on('connect', function ($serv, $fd, $from_id){
Expand Down
4 changes: 0 additions & 4 deletions include/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,6 @@ struct swServer_s
*/
uint32_t max_conn;

/**
* connect count
*/
uint32_t connect_count;
/**
* worker process max request
*/
Expand Down
15 changes: 4 additions & 11 deletions src/network/Connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ SWINLINE void swConnection_close(swServer *serv, int fd, int notify)
swReactor *reactor;
swEvent notify_ev;

if(conn == NULL)
if (conn == NULL)
{
swWarn("[Master]connection not found. fd=%d|max_fd=%d", fd, swServer_get_maxfd(serv));
swWarn("[Reactor]connection not found. fd=%d|max_fd=%d", fd, swServer_get_maxfd(serv));
return;
}

Expand Down Expand Up @@ -145,14 +145,6 @@ SWINLINE int swServer_new_connection(swServer *serv, swEvent *ev)
{
swServer_set_maxfd(serv, conn_fd);

/**
* Correction of the number of connections
*/
if (serv->connect_count > conn_fd)
{
serv->connect_count = conn_fd;
}

#ifdef SW_CONNECTION_LIST_EXPAND
//新的fd超过了最大fd

Expand All @@ -172,6 +164,7 @@ SWINLINE int swServer_new_connection(swServer *serv, swEvent *ev)
}
}
#endif

}

connection = &(serv->connection_list[conn_fd]);
Expand Down Expand Up @@ -294,7 +287,6 @@ SWINLINE void swConnection_clear_string_buffer(swConnection *conn)

int swConnection_send_in_buffer(swConnection *conn)
{
swServer *serv = SwooleG.serv;
swFactory *factory = SwooleG.factory;
swEventData _send;

Expand All @@ -306,6 +298,7 @@ int swConnection_send_in_buffer(swConnection *conn)

#ifdef SW_USE_RINGBUFFER

swServer *serv = SwooleG.serv;
swMemoryPool *pool = serv->reactor_threads[conn->from_id].pool;
swPackage package;

Expand Down
2 changes: 0 additions & 2 deletions src/network/ReactorProcess.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,6 @@ static int swReactorProcess_onClose(swReactor *reactor, swEvent *event)
{
serv->onClose(serv, event->fd, event->from_id);
}

serv->connect_count--;
return SW_OK;
}

19 changes: 6 additions & 13 deletions src/network/Server.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ static int swServer_master_onClose(swReactor *reactor, swEvent *event)
{
fd = queue[i];
conn = swServer_get_connection(serv, fd);
serv->connect_count--;

if (serv->onMasterClose != NULL)
{
Expand All @@ -83,13 +82,6 @@ static int swServer_master_onClose(swReactor *reactor, swEvent *event)
for (; serv->connection_list[find_max_fd].active == 0 && find_max_fd > swServer_get_minfd(serv); find_max_fd--);
swServer_set_maxfd(serv, find_max_fd);

/**
* Correction of the number of connections
*/
if (serv->connect_count > find_max_fd)
{
serv->connect_count = find_max_fd;
}
swTrace("set_maxfd=%d|close_fd=%d", find_max_fd, fd);
}
}
Expand Down Expand Up @@ -166,12 +158,13 @@ int swServer_master_onAccept(swReactor *reactor, swEvent *event)
return SW_OK;
}
}
swTrace("[Master]accept.event->fd=%d|event->from_id=%d|conn=%d", event->fd, event->from_id, new_fd);

swTrace("[Master] Accept new connection. maxfd=%d|reactor_id=%d|conn=%d", swServer_get_maxfd(serv), reactor->id, new_fd);

//too many connection
if (serv->connect_count >= serv->max_conn)
if (swServer_get_maxfd(serv) >= serv->max_conn)
{
swWarn("Too many connections [now: %d].", serv->connect_count);
swWarn("Too many connections [now: %d].", swServer_get_maxfd(serv));
close(new_fd);
return SW_OK;
}
Expand All @@ -191,6 +184,7 @@ int swServer_master_onAccept(swReactor *reactor, swEvent *event)
int keep_idle = serv->tcp_keepidle;
int keep_interval = serv->tcp_keepinterval;
int keep_count = serv->tcp_keepcount;

#ifdef TCP_KEEPIDLE
setsockopt(new_fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepalive , sizeof(keepalive));
setsockopt(new_fd, IPPROTO_TCP, TCP_KEEPIDLE, (void*)&keep_idle , sizeof(keep_idle));
Expand All @@ -214,6 +208,7 @@ int swServer_master_onAccept(swReactor *reactor, swEvent *event)
swServer_reactor_schedule(serv);
}
#endif

connEv.type = SW_EVENT_CONNECT;
connEv.from_id = reactor_id;
connEv.fd = new_fd;
Expand All @@ -235,8 +230,6 @@ int swServer_master_onAccept(swReactor *reactor, swEvent *event)
}
else
{
serv->connect_count++;

if(serv->onMasterConnect != NULL)
{
serv->onMasterConnect(serv, new_fd, reactor_id);
Expand Down
8 changes: 5 additions & 3 deletions swoole.c
Original file line number Diff line number Diff line change
Expand Up @@ -1153,15 +1153,15 @@ PHP_FUNCTION(swoole_server_handler)
"onManagerStart",
"onManagerStop",
};
for(i=0; i<PHP_SERVER_CALLBACK_NUM; i++)
for (i=0; i<PHP_SERVER_CALLBACK_NUM; i++)
{
if(strncasecmp(callback[i], ha_name, len) == 0)
{
ret = php_swoole_set_callback(i, cb TSRMLS_CC);
break;
}
}
if(ret < 0)
if (ret < 0)
{
php_error_docref(NULL TSRMLS_CC, E_ERROR, "swoole_server_handler: unkown handler[%s].", ha_name);
}
Expand Down Expand Up @@ -1217,7 +1217,8 @@ PHP_FUNCTION(swoole_server_on)
"managerStart",
"managerStop",
};
for(i=0; i<PHP_SERVER_CALLBACK_NUM; i++)

for (i=0; i<PHP_SERVER_CALLBACK_NUM; i++)
{
if(strncasecmp(callback[i], ha_name, len) == 0)
{
Expand Down Expand Up @@ -1274,6 +1275,7 @@ PHP_FUNCTION(swoole_server_close)
serv->onClose(serv, ev.fd, ev.from_id);
RETURN_TRUE;
}

RETURN_FALSE;
}

Expand Down

0 comments on commit 96ec134

Please sign in to comment.