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
* add function fc_format_path
* 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,
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);

View File

@ -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);
}

View File

@ -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;

View File

@ -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
}