From 630a6a2af68a708d3fe4e85284ab604d56d3f889 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Sat, 7 May 2022 16:53:02 +0800 Subject: [PATCH] add function conn_pool_connect_server_ex1 to support service name --- HISTORY | 3 +++ src/connection_pool.c | 13 +++++++------ src/connection_pool.h | 29 +++++++++++++++++++++++++---- src/server_id_func.c | 24 ++++++++++++++---------- src/server_id_func.h | 21 +++++++++++++-------- 5 files changed, 62 insertions(+), 28 deletions(-) diff --git a/HISTORY b/HISTORY index f895b4f..1c115eb 100644 --- a/HISTORY +++ b/HISTORY @@ -1,4 +1,7 @@ +Version 1.58 2022-05-07 + * add function conn_pool_connect_server_ex1 to support service name + Version 1.57 2022-04-22 * add function fc_format_path * add functions: fc_get_path_child_count and fc_copy_file diff --git a/src/connection_pool.c b/src/connection_pool.c index db3e960..2a437a3 100644 --- a/src/connection_pool.c +++ b/src/connection_pool.c @@ -113,9 +113,9 @@ void conn_pool_disconnect_server(ConnectionInfo *pConnection) } } -int conn_pool_connect_server_ex(ConnectionInfo *conn, - const int connect_timeout, const char *bind_ipaddr, - const bool log_connect_error) +int conn_pool_connect_server_ex1(ConnectionInfo *conn, + const char *service_name, const int connect_timeout, + const char *bind_ipaddr, const bool log_connect_error) { int result; @@ -136,9 +136,10 @@ int conn_pool_connect_server_ex(ConnectionInfo *conn, if (log_connect_error) { logError("file: "__FILE__", line: %d, " - "connect to server %s:%u fail, errno: %d, " - "error info: %s", __LINE__, conn->ip_addr, - conn->port, result, STRERROR(result)); + "connect to %s%sserver %s:%u fail, errno: %d, " + "error info: %s", __LINE__, service_name != NULL ? + service_name : "", service_name != NULL ? " " : "", + conn->ip_addr, conn->port, result, STRERROR(result)); } close(conn->sock); diff --git a/src/connection_pool.h b/src/connection_pool.h index ea0c8c4..9b02b8b 100644 --- a/src/connection_pool.h +++ b/src/connection_pool.h @@ -204,15 +204,34 @@ void conn_pool_disconnect_server(ConnectionInfo *pConnection); * connect to the server * parameters: * pConnection: the connection +* service_name: the service name to log * connect_timeout: the connect timeout in seconds * bind_ipaddr: the ip address to bind, NULL or empty for any * log_connect_error: if log error info when connect fail * NOTE: pConnection->sock will be closed when it >= 0 before connect * return 0 for success, != 0 for error */ -int conn_pool_connect_server_ex(ConnectionInfo *pConnection, +int conn_pool_connect_server_ex1(ConnectionInfo *conn, + const char *service_name, const int connect_timeout, + const char *bind_ipaddr, const bool log_connect_error); +/** +* connect to the server +* parameters: +* pConnection: the connection +* connect_timeout: the connect timeout in seconds +* bind_ipaddr: the ip address to bind, NULL or empty for any +* log_connect_error: if log error info when connect fail +* NOTE: pConnection->sock will be closed when it >= 0 before connect +* return 0 for success, != 0 for error +*/ +static inline int conn_pool_connect_server_ex(ConnectionInfo *pConnection, const int connect_timeout, const char *bind_ipaddr, - const bool log_connect_error); + const bool log_connect_error) +{ + const char *service_name = NULL; + return conn_pool_connect_server_ex1(pConnection, service_name, + connect_timeout, bind_ipaddr, log_connect_error); +} /** * connect to the server @@ -225,8 +244,9 @@ int conn_pool_connect_server_ex(ConnectionInfo *pConnection, static inline int conn_pool_connect_server(ConnectionInfo *pConnection, const int connect_timeout) { + const char *service_name = NULL; const char *bind_ipaddr = NULL; - return conn_pool_connect_server_ex(pConnection, + return conn_pool_connect_server_ex1(pConnection, service_name, connect_timeout, bind_ipaddr, true); } @@ -240,9 +260,10 @@ static inline int conn_pool_connect_server(ConnectionInfo *pConnection, static inline int conn_pool_connect_server_anyway(ConnectionInfo *pConnection, const int connect_timeout) { + const char *service_name = NULL; const char *bind_ipaddr = NULL; pConnection->sock = -1; - return conn_pool_connect_server_ex(pConnection, + return conn_pool_connect_server_ex1(pConnection, service_name, connect_timeout, bind_ipaddr, true); } diff --git a/src/server_id_func.c b/src/server_id_func.c index d1220b8..2d3766b 100644 --- a/src/server_id_func.c +++ b/src/server_id_func.c @@ -1496,8 +1496,8 @@ void fc_server_to_log(FCServerConfig *ctx) } ConnectionInfo *fc_server_check_connect_ex(FCAddressPtrArray *addr_array, - const int connect_timeout, const char *bind_ipaddr, - const bool log_connect_error, int *err_no) + const char *service_name, const int connect_timeout, + const char *bind_ipaddr, const bool log_connect_error, int *err_no) { FCAddressInfo **current; FCAddressInfo **addr; @@ -1513,8 +1513,9 @@ ConnectionInfo *fc_server_check_connect_ex(FCAddressPtrArray *addr_array, return &(*current)->conn; } - if ((*err_no=conn_pool_connect_server_ex(&(*current)->conn, - connect_timeout, bind_ipaddr, log_connect_error)) == 0) + if ((*err_no=conn_pool_connect_server_ex1(&(*current)->conn, + service_name, connect_timeout, bind_ipaddr, + log_connect_error)) == 0) { return &(*current)->conn; } @@ -1528,8 +1529,8 @@ ConnectionInfo *fc_server_check_connect_ex(FCAddressPtrArray *addr_array, if (addr == current) { continue; } - if ((*err_no=conn_pool_connect_server_ex(&(*addr)->conn, - connect_timeout, bind_ipaddr, + if ((*err_no=conn_pool_connect_server_ex1(&(*addr)->conn, + service_name, connect_timeout, bind_ipaddr, log_connect_error)) == 0) { addr_array->index = addr - addr_array->addrs; @@ -1552,8 +1553,9 @@ void fc_server_disconnect(FCAddressPtrArray *addr_array) } int fc_server_make_connection_ex(FCAddressPtrArray *addr_array, - ConnectionInfo *conn, const int connect_timeout, - const char *bind_ipaddr, const bool log_connect_error) + ConnectionInfo *conn, const char *service_name, + const int connect_timeout, const char *bind_ipaddr, + const bool log_connect_error) { FCAddressInfo **current; FCAddressInfo **addr; @@ -1567,7 +1569,8 @@ int fc_server_make_connection_ex(FCAddressPtrArray *addr_array, current = addr_array->addrs + addr_array->index; *conn = (*current)->conn; conn->sock = -1; - if ((result=conn_pool_connect_server_ex(conn, connect_timeout, + if ((result=conn_pool_connect_server_ex1(conn, + service_name, connect_timeout, bind_ipaddr, log_connect_error)) == 0) { return 0; @@ -1585,7 +1588,8 @@ int fc_server_make_connection_ex(FCAddressPtrArray *addr_array, *conn = (*addr)->conn; conn->sock = -1; - if ((result=conn_pool_connect_server_ex(conn, connect_timeout, + if ((result=conn_pool_connect_server_ex1(conn, + service_name, connect_timeout, bind_ipaddr, log_connect_error)) == 0) { addr_array->index = addr - addr_array->addrs; diff --git a/src/server_id_func.h b/src/server_id_func.h index 8f19531..52a32bc 100644 --- a/src/server_id_func.h +++ b/src/server_id_func.h @@ -212,11 +212,13 @@ int fc_server_to_config_string(FCServerConfig *ctx, FastBuffer *buffer); void fc_server_to_log(FCServerConfig *ctx); ConnectionInfo *fc_server_check_connect_ex(FCAddressPtrArray *addr_array, - const int connect_timeout, const char *bind_ipaddr, - const bool log_connect_error, int *err_no); + const char *service_name, const int connect_timeout, + const char *bind_ipaddr, const bool log_connect_error, int *err_no); -#define fc_server_check_connect(addr_array, connect_timeout, err_no) \ - fc_server_check_connect_ex(addr_array, connect_timeout, NULL, true, err_no) +#define fc_server_check_connect(addr_array, service_name, \ + connect_timeout, err_no) \ + fc_server_check_connect_ex(addr_array, service_name, \ + connect_timeout, NULL, true, err_no) void fc_server_disconnect(FCAddressPtrArray *addr_array); @@ -224,11 +226,14 @@ const FCAddressInfo *fc_server_get_address_by_peer( FCAddressPtrArray *addr_array, const char *peer_ip); int fc_server_make_connection_ex(FCAddressPtrArray *addr_array, - ConnectionInfo *conn, const int connect_timeout, - const char *bind_ipaddr, const bool log_connect_error); + ConnectionInfo *conn, const char *service_name, + const int connect_timeout, const char *bind_ipaddr, + const bool log_connect_error); -#define fc_server_make_connection(addr_array, conn, connect_timeout) \ - fc_server_make_connection_ex(addr_array, conn, connect_timeout, NULL, true) +#define fc_server_make_connection(addr_array, \ + conn, service_name, connect_timeout) \ + fc_server_make_connection_ex(addr_array, conn, \ + service_name, connect_timeout, NULL, true) #ifdef __cplusplus }