add function conn_pool_connect_server_ex1 to support service name

vote_node
YuQing 2022-05-07 16:53:02 +08:00
parent 23628e85f2
commit 630a6a2af6
5 changed files with 62 additions and 28 deletions

View File

@ -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 Version 1.57 2022-04-22
* add function fc_format_path * add function fc_format_path
* add functions: fc_get_path_child_count and fc_copy_file * add functions: fc_get_path_child_count and fc_copy_file

View File

@ -113,9 +113,9 @@ void conn_pool_disconnect_server(ConnectionInfo *pConnection)
} }
} }
int conn_pool_connect_server_ex(ConnectionInfo *conn, int conn_pool_connect_server_ex1(ConnectionInfo *conn,
const int connect_timeout, const char *bind_ipaddr, const char *service_name, const int connect_timeout,
const bool log_connect_error) const char *bind_ipaddr, const bool log_connect_error)
{ {
int result; int result;
@ -136,9 +136,10 @@ int conn_pool_connect_server_ex(ConnectionInfo *conn,
if (log_connect_error) if (log_connect_error)
{ {
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"connect to server %s:%u fail, errno: %d, " "connect to %s%sserver %s:%u fail, errno: %d, "
"error info: %s", __LINE__, conn->ip_addr, "error info: %s", __LINE__, service_name != NULL ?
conn->port, result, STRERROR(result)); service_name : "", service_name != NULL ? " " : "",
conn->ip_addr, conn->port, result, STRERROR(result));
} }
close(conn->sock); close(conn->sock);

View File

@ -204,15 +204,34 @@ void conn_pool_disconnect_server(ConnectionInfo *pConnection);
* connect to the server * connect to the server
* parameters: * parameters:
* pConnection: the connection * pConnection: the connection
* service_name: the service name to log
* connect_timeout: the connect timeout in seconds * connect_timeout: the connect timeout in seconds
* bind_ipaddr: the ip address to bind, NULL or empty for any * bind_ipaddr: the ip address to bind, NULL or empty for any
* log_connect_error: if log error info when connect fail * log_connect_error: if log error info when connect fail
* NOTE: pConnection->sock will be closed when it >= 0 before connect * NOTE: pConnection->sock will be closed when it >= 0 before connect
* return 0 for success, != 0 for error * 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 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 * 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, static inline int conn_pool_connect_server(ConnectionInfo *pConnection,
const int connect_timeout) const int connect_timeout)
{ {
const char *service_name = NULL;
const char *bind_ipaddr = 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); 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, static inline int conn_pool_connect_server_anyway(ConnectionInfo *pConnection,
const int connect_timeout) const int connect_timeout)
{ {
const char *service_name = NULL;
const char *bind_ipaddr = NULL; const char *bind_ipaddr = NULL;
pConnection->sock = -1; pConnection->sock = -1;
return conn_pool_connect_server_ex(pConnection, return conn_pool_connect_server_ex1(pConnection, service_name,
connect_timeout, bind_ipaddr, true); connect_timeout, bind_ipaddr, true);
} }

View File

@ -1496,8 +1496,8 @@ void fc_server_to_log(FCServerConfig *ctx)
} }
ConnectionInfo *fc_server_check_connect_ex(FCAddressPtrArray *addr_array, ConnectionInfo *fc_server_check_connect_ex(FCAddressPtrArray *addr_array,
const int connect_timeout, const char *bind_ipaddr, const char *service_name, const int connect_timeout,
const bool log_connect_error, int *err_no) const char *bind_ipaddr, const bool log_connect_error, int *err_no)
{ {
FCAddressInfo **current; FCAddressInfo **current;
FCAddressInfo **addr; FCAddressInfo **addr;
@ -1513,8 +1513,9 @@ ConnectionInfo *fc_server_check_connect_ex(FCAddressPtrArray *addr_array,
return &(*current)->conn; return &(*current)->conn;
} }
if ((*err_no=conn_pool_connect_server_ex(&(*current)->conn, if ((*err_no=conn_pool_connect_server_ex1(&(*current)->conn,
connect_timeout, bind_ipaddr, log_connect_error)) == 0) service_name, connect_timeout, bind_ipaddr,
log_connect_error)) == 0)
{ {
return &(*current)->conn; return &(*current)->conn;
} }
@ -1528,8 +1529,8 @@ ConnectionInfo *fc_server_check_connect_ex(FCAddressPtrArray *addr_array,
if (addr == current) { if (addr == current) {
continue; continue;
} }
if ((*err_no=conn_pool_connect_server_ex(&(*addr)->conn, if ((*err_no=conn_pool_connect_server_ex1(&(*addr)->conn,
connect_timeout, bind_ipaddr, service_name, connect_timeout, bind_ipaddr,
log_connect_error)) == 0) log_connect_error)) == 0)
{ {
addr_array->index = addr - addr_array->addrs; 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, int fc_server_make_connection_ex(FCAddressPtrArray *addr_array,
ConnectionInfo *conn, const int connect_timeout, ConnectionInfo *conn, const char *service_name,
const char *bind_ipaddr, const bool log_connect_error) const int connect_timeout, const char *bind_ipaddr,
const bool log_connect_error)
{ {
FCAddressInfo **current; FCAddressInfo **current;
FCAddressInfo **addr; FCAddressInfo **addr;
@ -1567,7 +1569,8 @@ int fc_server_make_connection_ex(FCAddressPtrArray *addr_array,
current = addr_array->addrs + addr_array->index; current = addr_array->addrs + addr_array->index;
*conn = (*current)->conn; *conn = (*current)->conn;
conn->sock = -1; 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) bind_ipaddr, log_connect_error)) == 0)
{ {
return 0; return 0;
@ -1585,7 +1588,8 @@ int fc_server_make_connection_ex(FCAddressPtrArray *addr_array,
*conn = (*addr)->conn; *conn = (*addr)->conn;
conn->sock = -1; 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) bind_ipaddr, log_connect_error)) == 0)
{ {
addr_array->index = addr - addr_array->addrs; addr_array->index = addr - addr_array->addrs;

View File

@ -212,11 +212,13 @@ int fc_server_to_config_string(FCServerConfig *ctx, FastBuffer *buffer);
void fc_server_to_log(FCServerConfig *ctx); void fc_server_to_log(FCServerConfig *ctx);
ConnectionInfo *fc_server_check_connect_ex(FCAddressPtrArray *addr_array, ConnectionInfo *fc_server_check_connect_ex(FCAddressPtrArray *addr_array,
const int connect_timeout, const char *bind_ipaddr, const char *service_name, const int connect_timeout,
const bool log_connect_error, int *err_no); const char *bind_ipaddr, const bool log_connect_error, int *err_no);
#define fc_server_check_connect(addr_array, connect_timeout, err_no) \ #define fc_server_check_connect(addr_array, service_name, \
fc_server_check_connect_ex(addr_array, connect_timeout, NULL, true, err_no) 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); 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); FCAddressPtrArray *addr_array, const char *peer_ip);
int fc_server_make_connection_ex(FCAddressPtrArray *addr_array, int fc_server_make_connection_ex(FCAddressPtrArray *addr_array,
ConnectionInfo *conn, const int connect_timeout, ConnectionInfo *conn, const char *service_name,
const char *bind_ipaddr, const bool log_connect_error); const int connect_timeout, const char *bind_ipaddr,
const bool log_connect_error);
#define fc_server_make_connection(addr_array, conn, connect_timeout) \ #define fc_server_make_connection(addr_array, \
fc_server_make_connection_ex(addr_array, conn, connect_timeout, NULL, true) conn, service_name, connect_timeout) \
fc_server_make_connection_ex(addr_array, conn, \
service_name, connect_timeout, NULL, true)
#ifdef __cplusplus #ifdef __cplusplus
} }