limit number of connection at a time

This commit is contained in:
Xuan Sang LE 2018-03-08 11:39:44 +01:00
parent 55d8dd1d56
commit d39832b981
3 changed files with 22 additions and 2 deletions

18
httpd.c
View File

@ -82,6 +82,9 @@ static int config_handler(void* conf, const char* section, const char* name,
pconfig->htdocs = strdup(value); pconfig->htdocs = strdup(value);
} else if(MATCH("SERVER", "tmpdir")) { } else if(MATCH("SERVER", "tmpdir")) {
pconfig->tmpdir = strdup(value); pconfig->tmpdir = strdup(value);
}
else if(MATCH("SERVER", "maxcon")) {
pconfig->maxcon = atoi(value);
} }
else if(MATCH("SERVER", "backlog")) { else if(MATCH("SERVER", "backlog")) {
pconfig->backlog = atoi(value); pconfig->backlog = atoi(value);
@ -143,6 +146,8 @@ void load_config(const char* file)
server_config.backlog = 100; server_config.backlog = 100;
server_config.rules = list_init(); server_config.rules = list_init();
server_config.handlers = dict(); server_config.handlers = dict();
server_config.maxcon = 1000;
server_config.connection = 0;
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
server_config.usessl = 0; server_config.usessl = 0;
server_config.sslcert = "cert.pem"; server_config.sslcert = "cert.pem";
@ -165,6 +170,7 @@ void load_config(const char* file)
void stop_serve(int dummy) { void stop_serve(int dummy) {
list_free(&(server_config.rules)); list_free(&(server_config.rules));
freedict(server_config.handlers); freedict(server_config.handlers);
LOG("Unclosed connection: %d\n", server_config.connection);
unload_all_plugin(); unload_all_plugin();
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
SSL_CTX_free(ctx); SSL_CTX_free(ctx);
@ -206,6 +212,11 @@ int main(int argc, char* argv[])
while (1) while (1)
{ {
if( server_config.connection >= server_config.maxcon )
{
LOG("Too many unclosed connection (%d). Wait for it\n", server_config.connection);
continue;
}
antd_client_t* client = (antd_client_t*)malloc(sizeof(antd_client_t)); antd_client_t* client = (antd_client_t*)malloc(sizeof(antd_client_t));
client_sock = accept(server_sock,(struct sockaddr *)&client_name,&client_name_len); client_sock = accept(server_sock,(struct sockaddr *)&client_name,&client_name_len);
if (client_sock == -1) if (client_sock == -1)
@ -214,7 +225,8 @@ int main(int argc, char* argv[])
continue; continue;
} }
/* accept_request(client_sock); */ /* accept_request(client_sock); */
server_config.connection++;
//LOG("Unclosed connection: %d\n", server_config.connection);
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
client->ssl = NULL; client->ssl = NULL;
if(server_config.usessl == 1) if(server_config.usessl == 1)
@ -224,13 +236,17 @@ int main(int argc, char* argv[])
if (SSL_accept((SSL*)client->ssl) <= 0) { if (SSL_accept((SSL*)client->ssl) <= 0) {
ERR_print_errors_fp(stderr); ERR_print_errors_fp(stderr);
antd_close(client);
continue; continue;
} }
} }
#endif #endif
client->sock = client_sock; client->sock = client_sock;
if (pthread_create(&newthread , NULL,(void *(*)(void *))accept_request, (void *)client) != 0) if (pthread_create(&newthread , NULL,(void *(*)(void *))accept_request, (void *)client) != 0)
{
perror("pthread_create"); perror("pthread_create");
antd_close(client);
}
else else
{ {
//reclaim the stack data when thread finish //reclaim the stack data when thread finish

View File

@ -121,6 +121,8 @@ int antd_close(void* src)
#endif #endif
//printf("Close sock %d\n", source->sock); //printf("Close sock %d\n", source->sock);
int ret = close(source->sock); int ret = close(source->sock);
server_config.connection--;
LOG("Remaining connection %d\n", server_config.connection);
free(src); free(src);
return ret; return ret;
} }

View File

@ -39,13 +39,15 @@ typedef struct {
list rules; list rules;
dictionary handlers; dictionary handlers;
int backlog; int backlog;
int maxcon;
int connection;
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
int usessl; int usessl;
char* sslcert; char* sslcert;
char* sslkey; char* sslkey;
#endif #endif
}config_t; }config_t;
extern config_t server_config;
typedef struct{ typedef struct{
int sock; int sock;
void* ssl; void* ssl;