Răsfoiți Sursa

dns_client: disable server when upstream returns failure

Nick Peng 2 ani în urmă
părinte
comite
ba2cad4b44
3 a modificat fișierele cu 33 adăugiri și 3 ștergeri
  1. 11 3
      src/dns_client.c
  2. 18 0
      src/http_parse.c
  3. 4 0
      src/http_parse.h

+ 11 - 3
src/dns_client.c

@@ -2614,6 +2614,7 @@ static int _dns_client_process_tcp_buff(struct dns_server_info *server_info)
 				tlog(TLOG_WARN, "http server query from %s:%d failed, server return http code : %d, %s",
 					 server_info->ip, server_info->port, http_head_get_httpcode(http_head),
 					 http_head_get_httpcode_msg(http_head));
+				server_info->prohibit = 1;
 				goto out;
 			}
 
@@ -3529,6 +3530,7 @@ static int _dns_client_send_packet(struct dns_query_struct *query, void *packet,
 	void *packet_data = NULL;
 	int packet_data_len = 0;
 	unsigned char packet_data_buffer[DNS_IN_PACKSIZE];
+	int prohibit_time = 60;
 
 	query->send_tick = get_tick_count();
 
@@ -3536,6 +3538,10 @@ static int _dns_client_send_packet(struct dns_query_struct *query, void *packet,
 	atomic_inc(&query->dns_request_sent);
 	for (i = 0; i < 2; i++) {
 		total_server = 0;
+		if (i == 1) {
+			prohibit_time = 5;
+		}
+
 		pthread_mutex_lock(&client.server_list_lock);
 		list_for_each_entry_safe(group_member, tmp, &query->server_group->head, list)
 		{
@@ -3544,11 +3550,15 @@ static int _dns_client_send_packet(struct dns_query_struct *query, void *packet,
 				if (server_info->is_already_prohibit == 0) {
 					server_info->is_already_prohibit = 1;
 					atomic_inc(&client.dns_server_prohibit_num);
+					time(&server_info->last_send);
+					time(&server_info->last_recv);
+					tlog(TLOG_INFO, "server %s not alive, prohibit", server_info->ip);
+					_dns_client_shutdown_socket(server_info);
 				}
 
 				time_t now = 0;
 				time(&now);
-				if ((now - 60 < server_info->last_send) && (now - 5 > server_info->last_recv)) {
+				if ((now - prohibit_time < server_info->last_send)) {
 					continue;
 				}
 				server_info->prohibit = 0;
@@ -3621,8 +3631,6 @@ static int _dns_client_send_packet(struct dns_query_struct *query, void *packet,
 				time(&now);
 				if (now - 10 > server_info->last_recv || send_err != ENOMEM) {
 					server_info->prohibit = 1;
-					tlog(TLOG_INFO, "server %s not alive, prohibit", server_info->ip);
-					_dns_client_shutdown_socket(server_info);
 				}
 
 				atomic_dec(&query->dns_request_sent);

+ 18 - 0
src/http_parse.c

@@ -134,6 +134,24 @@ struct http_head_fields *http_head_next_fields(struct http_head_fields *fields)
 	return next;
 }
 
+const char *http_head_fields_get_name(struct http_head_fields *fields)
+{
+	if (fields == NULL) {
+		return NULL;
+	}
+
+	return fields->name;
+}
+
+const char *http_head_fields_get_value(struct http_head_fields *fields)
+{
+	if (fields == NULL) {
+		return NULL;
+	}
+
+	return fields->value;
+}
+
 int http_head_lookup_fields(struct http_head_fields *fields, const char **name, const char **value)
 {
 	if (fields == NULL) {

+ 4 - 0
src/http_parse.h

@@ -67,6 +67,10 @@ struct http_head_fields *http_head_next_fields(struct http_head_fields *fields);
 
 const char *http_head_get_fields_value(struct http_head *http_head, const char *name);
 
+const char *http_head_fields_get_name(struct http_head_fields *fields);
+
+const char *http_head_fields_get_value(struct http_head_fields *fields);
+
 int http_head_lookup_fields(struct http_head_fields *fields, const char **name, const char **value);
 
 /*