log IPv6 address and port gracefully

use_iouring V1.0.71
YuQing 2023-12-25 17:01:43 +08:00
parent 7816a28c53
commit 02f4659a32
3 changed files with 29 additions and 13 deletions

View File

@ -221,11 +221,12 @@ int conn_pool_connect_server_ex1(ConnectionInfo *conn,
{ {
if (log_connect_error) if (log_connect_error)
{ {
FC_FORMAT_IP_ADDRESS(conn->ip_addr, new_ip_addr);
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"connect to %s%sserver %s:%u fail, errno: %d, " "connect to %s%sserver %s:%u fail, errno: %d, "
"error info: %s", __LINE__, service_name != NULL ? "error info: %s", __LINE__, service_name != NULL ?
service_name : "", service_name != NULL ? " " : "", service_name : "", service_name != NULL ? " " : "",
conn->ip_addr, conn->port, result, STRERROR(result)); new_ip_addr, conn->port, result, STRERROR(result));
} }
close(conn->sock); close(conn->sock);
@ -255,9 +256,10 @@ int conn_pool_async_connect_server_ex(ConnectionInfo *conn,
result = asyncconnectserverbyip(conn->sock, conn->ip_addr, conn->port); result = asyncconnectserverbyip(conn->sock, conn->ip_addr, conn->port);
if (!(result == 0 || result == EINPROGRESS)) if (!(result == 0 || result == EINPROGRESS))
{ {
FC_FORMAT_IP_ADDRESS(conn->ip_addr, new_ip_addr);
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"connect to server %s:%u fail, errno: %d, " "connect to server %s:%u fail, errno: %d, "
"error info: %s", __LINE__, conn->ip_addr, "error info: %s", __LINE__, new_ip_addr,
conn->port, result, STRERROR(result)); conn->port, result, STRERROR(result));
close(conn->sock); close(conn->sock);
conn->sock = -1; conn->sock = -1;
@ -319,12 +321,13 @@ static ConnectionInfo *get_connection(ConnectionPool *cp,
if ((cp->max_count_per_entry > 0) && if ((cp->max_count_per_entry > 0) &&
(cm->total_count >= cp->max_count_per_entry)) (cm->total_count >= cp->max_count_per_entry))
{ {
FC_FORMAT_IP_ADDRESS(conn->ip_addr, new_ip_addr);
*err_no = ENOSPC; *err_no = ENOSPC;
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"connections: %d of %s%sserver %s:%u exceed limit: %d", "connections: %d of %s%sserver %s:%u exceed limit: %d",
__LINE__, cm->total_count, service_name != NULL ? __LINE__, cm->total_count, service_name != NULL ?
service_name : "", service_name != NULL ? " " : "", service_name : "", service_name != NULL ? " " : "",
conn->ip_addr, conn->port, cp->max_count_per_entry); new_ip_addr, conn->port, cp->max_count_per_entry);
pthread_mutex_unlock(&cm->lock); pthread_mutex_unlock(&cm->lock);
return NULL; return NULL;
} }
@ -529,18 +532,20 @@ static int close_connection(ConnectionPool *cp, ConnectionInfo *conn,
pthread_mutex_unlock(&cp->lock); pthread_mutex_unlock(&cp->lock);
if (cm == NULL) if (cm == NULL)
{ {
logError("file: "__FILE__", line: %d, " \ FC_FORMAT_IP_ADDRESS(conn->ip_addr, new_ip_addr);
"hash entry of server %s:%u not exist", __LINE__, \ logError("file: "__FILE__", line: %d, "
conn->ip_addr, conn->port); "hash entry of server %s:%u not exist",
__LINE__, new_ip_addr, conn->port);
return ENOENT; return ENOENT;
} }
node = (ConnectionNode *)((char *)conn - sizeof(ConnectionNode)); node = (ConnectionNode *)((char *)conn - sizeof(ConnectionNode));
if (node->manager != cm) if (node->manager != cm)
{ {
logError("file: "__FILE__", line: %d, " \ FC_FORMAT_IP_ADDRESS(conn->ip_addr, new_ip_addr);
"manager of server entry %s:%u is invalid!", \ logError("file: "__FILE__", line: %d, "
__LINE__, conn->ip_addr, conn->port); "manager of server entry %s:%u is invalid!",
__LINE__, new_ip_addr, conn->port);
return EINVAL; return EINVAL;
} }

View File

@ -812,7 +812,7 @@ int setsockaddrbyip(const char *ip, const uint16_t port,
if (is_ipv6_addr(ip)) if (is_ipv6_addr(ip))
{ {
convert->len = sizeof(convert->sa.addr6); convert->len = sizeof(convert->sa.addr6);
if (strchr(ip, '#') != NULL) if (strchr(ip, '%') != NULL)
{ {
struct addrinfo hints, *res; struct addrinfo hints, *res;
@ -873,8 +873,8 @@ int connectserverbyip(int sock, const char *server_ip, const uint16_t server_por
return 0; return 0;
} }
int connectserverbyip_nb_ex(int sock, const char *server_ip, \ int connectserverbyip_nb_ex(int sock, const char *server_ip,
const uint16_t server_port, const int timeout, \ const uint16_t server_port, const int timeout,
const bool auto_detect) const bool auto_detect)
{ {
int result; int result;
@ -1069,10 +1069,11 @@ int socketClientEx2(int af, const char *server_ip,
server_port, timeout, auto_detect); server_port, timeout, auto_detect);
if (*err_no != 0) if (*err_no != 0)
{ {
FC_FORMAT_IP_ADDRESS(server_ip, new_ip_addr);
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"connect to %s:%u fail, " "connect to %s:%u fail, "
"errno: %d, error info: %s", "errno: %d, error info: %s",
__LINE__, server_ip, server_port, __LINE__, new_ip_addr, server_port,
*err_no, STRERROR(*err_no)); *err_no, STRERROR(*err_no));
close(sock); close(sock);
return -4; return -4;

View File

@ -52,6 +52,16 @@
#define FAST_WRITE_BUFF_SIZE (256 * 1024) #define FAST_WRITE_BUFF_SIZE (256 * 1024)
#define FC_FORMAT_IP_ADDRESS(old_ip_addr, new_ip_addr) \
char new_ip_addr[IP_ADDRESS_SIZE + 2]; \
do { \
if (is_ipv6_addr(old_ip_addr)) { \
sprintf(new_ip_addr, "[%s]", old_ip_addr); \
} else { \
strcpy(new_ip_addr, old_ip_addr); \
} \
} while (0)
typedef struct fast_if_config { typedef struct fast_if_config {
char name[IF_NAMESIZE]; //if name char name[IF_NAMESIZE]; //if name
char mac[64]; char mac[64];