Compare commits
2 Commits
Release41-
...
multi-thre
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b1c4ad7afb | ||
|
|
07ee91dd2d |
@@ -3862,10 +3862,12 @@ static void *_dns_client_work(void *arg)
|
||||
sleep_time = 0;
|
||||
}
|
||||
}
|
||||
last = now;
|
||||
|
||||
if (now >= expect_time) {
|
||||
_dns_client_period_run();
|
||||
if (last != now) {
|
||||
_dns_client_period_run();
|
||||
}
|
||||
|
||||
sleep_time = sleep - (now - expect_time);
|
||||
if (sleep_time < 0) {
|
||||
sleep_time = 0;
|
||||
@@ -3873,6 +3875,7 @@ static void *_dns_client_work(void *arg)
|
||||
}
|
||||
expect_time += sleep;
|
||||
}
|
||||
last = now;
|
||||
|
||||
pthread_mutex_lock(&client.domain_map_lock);
|
||||
if (list_empty(&client.dns_request_list) && atomic_read(&client.run_period) == 0) {
|
||||
|
||||
110
src/dns_server.c
110
src/dns_server.c
@@ -62,6 +62,7 @@
|
||||
#define CACHE_AUTO_ENABLE_SIZE (1024 * 1024 * 128)
|
||||
#define EXPIRED_DOMAIN_PREFETCH_TIME (3600 * 8)
|
||||
#define DNS_MAX_DOMAIN_REFETCH_NUM 16
|
||||
#define DNS_WORKER_NUM 4
|
||||
|
||||
#define RECV_ERROR_AGAIN 1
|
||||
#define RECV_ERROR_OK 0
|
||||
@@ -252,9 +253,26 @@ struct dns_request {
|
||||
struct dns_request_pending_list *request_pending_list;
|
||||
};
|
||||
|
||||
struct dns_server_work_event {
|
||||
struct list_head list;
|
||||
struct dns_server_conn_head *conn;
|
||||
unsigned char inpacket[DNS_IN_PACKSIZE];
|
||||
int inpacket_len;
|
||||
struct sockaddr_storage local;
|
||||
socklen_t local_len;
|
||||
struct sockaddr_storage from;
|
||||
socklen_t from_len;
|
||||
};
|
||||
|
||||
/* dns server data */
|
||||
struct dns_server {
|
||||
atomic_t run;
|
||||
|
||||
pthread_t worker[DNS_WORKER_NUM];
|
||||
pthread_mutex_t worker_notify_lock;
|
||||
pthread_cond_t worker_notify_cond;
|
||||
struct list_head work_list;
|
||||
|
||||
int epoll_fd;
|
||||
int event_fd;
|
||||
struct list_head conn_list;
|
||||
@@ -4488,9 +4506,9 @@ errout:
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int _dns_server_recv(struct dns_server_conn_head *conn, unsigned char *inpacket, int inpacket_len,
|
||||
struct sockaddr_storage *local, socklen_t local_len, struct sockaddr_storage *from,
|
||||
socklen_t from_len)
|
||||
static int _dns_server_process_work(struct dns_server_conn_head *conn, unsigned char *inpacket, int inpacket_len,
|
||||
struct sockaddr_storage *local, socklen_t local_len, struct sockaddr_storage *from,
|
||||
socklen_t from_len)
|
||||
{
|
||||
int decode_len = 0;
|
||||
int ret = -1;
|
||||
@@ -4553,6 +4571,34 @@ errout:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int _dns_server_recv(struct dns_server_conn_head *conn, unsigned char *inpacket, int inpacket_len,
|
||||
struct sockaddr_storage *local, socklen_t local_len, struct sockaddr_storage *from,
|
||||
socklen_t from_len)
|
||||
{
|
||||
struct dns_server_work_event *event = NULL;
|
||||
event = (struct dns_server_work_event *)malloc(sizeof(struct dns_server_work_event));
|
||||
if (event == NULL) {
|
||||
tlog(TLOG_ERROR, "malloc failed.\n");
|
||||
return -1;
|
||||
}
|
||||
memset(event, 0, sizeof(struct dns_server_work_event));
|
||||
event->conn = conn;
|
||||
memcpy(event->inpacket, inpacket, inpacket_len);
|
||||
event->inpacket_len = inpacket_len;
|
||||
memcpy(&event->local, local, local_len);
|
||||
event->local_len = local_len;
|
||||
memcpy(&event->from, from, from_len);
|
||||
event->from_len = from_len;
|
||||
INIT_LIST_HEAD(&event->list);
|
||||
|
||||
pthread_mutex_lock(&server.worker_notify_lock);
|
||||
list_add_tail(&event->list, &server.work_list);
|
||||
pthread_mutex_unlock(&server.worker_notify_lock);
|
||||
pthread_cond_signal(&server.worker_notify_cond);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _dns_server_setup_server_query_options(struct dns_request *request,
|
||||
struct dns_server_query_option *server_query_option)
|
||||
{
|
||||
@@ -4774,7 +4820,7 @@ static int _dns_server_tcp_recv(struct dns_server_conn_tcp_client *tcpclient)
|
||||
if (errno == EAGAIN) {
|
||||
return RECV_ERROR_AGAIN;
|
||||
}
|
||||
|
||||
|
||||
if (errno == ECONNRESET) {
|
||||
return RECV_ERROR_CLOSE;
|
||||
}
|
||||
@@ -5137,7 +5183,7 @@ static void _dns_server_period_run(unsigned int msec)
|
||||
struct dns_request *tmp = NULL;
|
||||
LIST_HEAD(check_list);
|
||||
|
||||
if (msec % 10 == 0) {
|
||||
if ((msec % 10) == 0) {
|
||||
_dns_server_period_run_second();
|
||||
}
|
||||
|
||||
@@ -5198,6 +5244,34 @@ static void _dns_server_close_socket_server(void)
|
||||
}
|
||||
}
|
||||
|
||||
static void *_dns_server_worker(void *args)
|
||||
{
|
||||
struct dns_server_work_event *work_event = NULL;
|
||||
|
||||
while (atomic_read(&server.run)) {
|
||||
pthread_mutex_lock(&server.worker_notify_lock);
|
||||
if (list_empty(&server.work_list)) {
|
||||
pthread_cond_wait(&server.worker_notify_cond, &server.worker_notify_lock);
|
||||
}
|
||||
|
||||
work_event = list_first_entry_or_null(&server.work_list, struct dns_server_work_event, list);
|
||||
if (work_event) {
|
||||
list_del_init(&work_event->list);
|
||||
}
|
||||
pthread_mutex_unlock(&server.worker_notify_lock);
|
||||
|
||||
if (work_event == NULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
_dns_server_process_work(work_event->conn, work_event->inpacket, work_event->inpacket_len, &work_event->local,
|
||||
work_event->local_len, &work_event->from, work_event->from_len);
|
||||
free(work_event);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int dns_server_run(void)
|
||||
{
|
||||
struct epoll_event events[DNS_MAX_EVENTS + 1];
|
||||
@@ -5227,11 +5301,12 @@ int dns_server_run(void)
|
||||
expect_time -= cnt * sleep;
|
||||
sleep_time -= cnt * sleep;
|
||||
}
|
||||
last = now;
|
||||
|
||||
if (now >= expect_time) {
|
||||
msec++;
|
||||
_dns_server_period_run(msec);
|
||||
if (last != now) {
|
||||
_dns_server_period_run(msec);
|
||||
}
|
||||
sleep_time = sleep - (now - expect_time);
|
||||
if (sleep_time < 0) {
|
||||
sleep_time = 0;
|
||||
@@ -5250,6 +5325,7 @@ int dns_server_run(void)
|
||||
pthread_mutex_unlock(&server.request_list_lock);
|
||||
expect_time += sleep;
|
||||
}
|
||||
last = now;
|
||||
|
||||
num = epoll_wait(server.epoll_fd, events, DNS_MAX_EVENTS, sleep_time);
|
||||
if (num < 0) {
|
||||
@@ -5680,7 +5756,10 @@ int dns_server_init(void)
|
||||
}
|
||||
|
||||
pthread_mutex_init(&server.request_list_lock, NULL);
|
||||
pthread_mutex_init(&server.worker_notify_lock, NULL);
|
||||
pthread_cond_init(&server.worker_notify_cond, NULL);
|
||||
INIT_LIST_HEAD(&server.request_list);
|
||||
INIT_LIST_HEAD(&server.work_list);
|
||||
server.epoll_fd = epollfd;
|
||||
atomic_set(&server.run, 1);
|
||||
|
||||
@@ -5698,6 +5777,14 @@ int dns_server_init(void)
|
||||
goto errout;
|
||||
}
|
||||
|
||||
for (int i = 0; i < DNS_WORKER_NUM; i++) {
|
||||
ret = pthread_create(&server.worker[i], &attr, _dns_server_worker, NULL);
|
||||
if (ret != 0) {
|
||||
tlog(TLOG_ERROR, "create server work thread failed, %s\n", strerror(ret));
|
||||
goto errout;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
errout:
|
||||
atomic_set(&server.run, 0);
|
||||
@@ -5726,6 +5813,15 @@ void dns_server_exit(void)
|
||||
close(server.event_fd);
|
||||
server.event_fd = -1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < DNS_WORKER_NUM; i++) {
|
||||
if (server.worker[i]) {
|
||||
pthread_cond_broadcast(&server.worker_notify_cond);
|
||||
pthread_join(server.worker[i], NULL);
|
||||
server.worker[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
_dns_server_close_socket();
|
||||
_dns_server_cache_save();
|
||||
_dns_server_request_remove_all();
|
||||
|
||||
@@ -1780,10 +1780,11 @@ static void *_fast_ping_work(void *arg)
|
||||
sleep_time = 0;
|
||||
}
|
||||
}
|
||||
last = now;
|
||||
|
||||
if (now >= expect_time) {
|
||||
_fast_ping_period_run();
|
||||
if (last != now) {
|
||||
_fast_ping_period_run();
|
||||
}
|
||||
sleep_time = sleep - (now - expect_time);
|
||||
if (sleep_time < 0) {
|
||||
sleep_time = 0;
|
||||
@@ -1791,6 +1792,7 @@ static void *_fast_ping_work(void *arg)
|
||||
}
|
||||
expect_time += sleep;
|
||||
}
|
||||
last = now;
|
||||
|
||||
pthread_mutex_lock(&ping.map_lock);
|
||||
if (hash_empty(ping.addrmap)) {
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#define MAX_LINE_LEN 1024
|
||||
#define MAX_LINE_LEN 8192
|
||||
#define MAX_KEY_LEN 64
|
||||
#define CONF_INT_MAX (~(1 << 31))
|
||||
#define CONF_INT_MIN (1 << 31)
|
||||
|
||||
@@ -299,7 +299,7 @@ static int load_conf_printf(const char *file, int lineno, int ret)
|
||||
static int load_conf_file(const char *file, struct config_item *items, conf_error_handler handler)
|
||||
{
|
||||
FILE *fp = NULL;
|
||||
char line[MAX_LINE_LEN];
|
||||
char line[MAX_LINE_LEN + MAX_KEY_LEN];
|
||||
char key[MAX_KEY_LEN];
|
||||
char value[MAX_LINE_LEN];
|
||||
int filed_num = 0;
|
||||
@@ -309,6 +309,8 @@ static int load_conf_file(const char *file, struct config_item *items, conf_erro
|
||||
int ret = 0;
|
||||
int call_ret = 0;
|
||||
int line_no = 0;
|
||||
int line_len = 0;
|
||||
int read_len = 0;
|
||||
|
||||
if (handler == NULL) {
|
||||
handler = load_conf_printf;
|
||||
@@ -320,9 +322,17 @@ static int load_conf_file(const char *file, struct config_item *items, conf_erro
|
||||
}
|
||||
|
||||
line_no = 0;
|
||||
while (fgets(line, MAX_LINE_LEN, fp)) {
|
||||
while (fgets(line + line_len, MAX_LINE_LEN - line_len, fp)) {
|
||||
line_no++;
|
||||
filed_num = sscanf(line, "%63s %1023[^\r\n]s", key, value);
|
||||
read_len = strnlen(line + line_len, sizeof(line));
|
||||
if (read_len >= 2 && *(line + line_len + read_len - 2) == '\\') {
|
||||
line_len += read_len - 2;
|
||||
line[line_len] = '\0';
|
||||
continue;
|
||||
}
|
||||
line_len = 0;
|
||||
|
||||
filed_num = sscanf(line, "%63s %8192[^\r\n]s", key, value);
|
||||
if (filed_num <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -45,7 +45,6 @@
|
||||
#include <sys/types.h>
|
||||
#include <ucontext.h>
|
||||
|
||||
#define MAX_LINE_LEN 1024
|
||||
#define MAX_KEY_LEN 64
|
||||
#define SMARTDNS_PID_FILE "/var/run/smartdns.pid"
|
||||
#define TMP_BUFF_LEN_32 32
|
||||
|
||||
Reference in New Issue
Block a user