Use blocking select to monitor ports, no need to use a thread for each port

This commit is contained in:
lxsang 2020-09-23 13:10:54 +02:00
parent 9da33124bc
commit 708e492c49
4 changed files with 113 additions and 80 deletions

View File

@ -1,5 +1,5 @@
# initialise autoconf and set up some basic information about the program were packaging
AC_INIT([antd], [1.0.5b], [xsang.le@gmail.com])
AC_INIT([antd], [1.0.6b], [xsang.le@gmail.com])
# Were going to use automake for this project
AM_INIT_AUTOMAKE([subdir-objects])

BIN
dist/antd-1.0.6b.tar.gz vendored Normal file

Binary file not shown.

View File

@ -104,6 +104,8 @@ static int config_handler(void *conf, const char *section, const char *name,
//char * ppath = NULL;
if (MATCH("SERVER", "plugins"))
{
if (pconfig->plugins_dir)
free(pconfig->plugins_dir);
pconfig->plugins_dir = strdup(value);
if (stat(pconfig->plugins_dir, &st) == -1)
mkdirp(pconfig->plugins_dir, 0755);

89
httpd.c
View File

@ -8,6 +8,7 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <errno.h>
#include "http_server.h"
#include "lib/ini.h"
#include "lib/scheduler.h"
@ -163,26 +164,14 @@ static void stop_serve(int dummy)
sigprocmask(SIG_UNBLOCK, &mask, NULL);
}
static void *antd_monitor(port_config_t *pcnf)
static void antd_monitor(port_config_t *pcnf)
{
antd_task_t *task = NULL;
struct timeval timeout;
int client_sock = -1;
struct sockaddr_in client_name;
socklen_t client_name_len = sizeof(client_name);
char *client_ip = NULL;
config_t *conf = config();
LOG("Listening on port %d", pcnf->port);
while (scheduler.status)
{
if (conf->connection > conf->maxcon)
{
//ERROR("Reach max connection %d", conf->connection);
timeout.tv_sec = 0;
timeout.tv_usec = 10000; // 5 ms
select(0, NULL, NULL, NULL, &timeout);
continue;
}
if (pcnf->sock > 0)
{
client_sock = accept(pcnf->sock, (struct sockaddr *)&client_name, &client_name_len);
@ -226,7 +215,10 @@ static void *antd_monitor(port_config_t *pcnf)
{
client->ssl = (void *)SSL_new(ctx);
if (!client->ssl)
continue;
{
finish_request(request);
return;
}
SSL_set_fd((SSL *)client->ssl, client->sock);
// this can be used in the protocol select callback to
// set the protocol selected by the server
@ -252,8 +244,6 @@ static void *antd_monitor(port_config_t *pcnf)
}
}
}
return NULL;
}
static void client_statistic(int fd, void *user_data)
{
@ -305,10 +295,13 @@ static void client_statistic(int fd, void *user_data)
int main(int argc, char *argv[])
{
pthread_t monitor_th;
pthread_t sched_th;
// startup port
chain_t it;
struct timeval timeout;
port_config_t *pcnf;
fd_set master_set, working_set;
int status, maxfd = 0;
int nlisten = 0;
// load the config first
if (argc == 1)
@ -348,6 +341,7 @@ int main(int argc, char *argv[])
stop_serve(0);
exit(1);
}
FD_ZERO(&master_set);
for_each_assoc(it, conf->ports)
{
pcnf = (port_config_t *)it->value;
@ -356,17 +350,10 @@ int main(int argc, char *argv[])
pcnf->sock = startup(&pcnf->port);
if (pcnf->sock > 0)
{
if (pthread_create(&monitor_th, NULL, (void *(*)(void *))antd_monitor, (void *)pcnf) != 0)
{
ERROR("pthread_create: cannot create worker");
stop_serve(0);
exit(1);
}
else
{
// reclaim data when exit
pthread_detach(monitor_th);
}
set_nonblock(pcnf->sock);
FD_SET(pcnf->sock, &master_set);
LOG("Listening on port %d", pcnf->port);
maxfd = pcnf->sock > maxfd ? pcnf->sock : maxfd;
nlisten++;
}
else
@ -381,7 +368,51 @@ int main(int argc, char *argv[])
stop_serve(0);
exit(1);
}
antd_wait(&scheduler);
// Start scheduler
if (pthread_create(&sched_th, NULL, (void *(*)(void *))antd_wait, (void *)&scheduler) != 0)
{
ERROR("pthread_create: cannot start scheduler thread");
stop_serve(0);
exit(1);
}
else
{
// reclaim data when exit
pthread_detach(sched_th);
}
while (scheduler.status)
{
if (conf->connection > conf->maxcon)
{
//ERROR("Reach max connection %d", conf->connection);
timeout.tv_sec = 0;
timeout.tv_usec = 10000; // 5 ms
select(0, NULL, NULL, NULL, &timeout);
continue;
}
FD_ZERO(&working_set);
memcpy(&working_set, &master_set, sizeof(master_set));
// blocking select
status = select(maxfd + 1, &working_set, NULL, NULL, NULL);
if (status < 0)
{
ERROR("select() error: %s", strerror(errno));
break;
}
if (status == 0)
{
continue;
}
for_each_assoc(it, conf->ports)
{
pcnf = (port_config_t *)it->value;
if (pcnf && pcnf->sock > 0 && FD_ISSET(pcnf->sock, &working_set))
{
antd_monitor(pcnf);
}
}
}
stop_serve(0);
return (0);
}