add function fc_server_check_connect_ex

pull/37/head
YuQing 2020-02-20 16:03:28 +08:00
parent 89ad53974f
commit 6e5511614d
3 changed files with 68 additions and 1 deletions

View File

@ -1,5 +1,5 @@
Version 1.44 2020-02-19
Version 1.44 2020-02-20
* add test file src/tests/test_pthread_lock.c
* add uniq_skiplist.[hc]
* add function split_string_ex

View File

@ -1451,3 +1451,59 @@ void fc_server_to_log(FCServerConfig *ctx)
fc_server_log_groups(ctx);
fc_server_log_servers(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)
{
FCAddressInfo **current;
FCAddressInfo **addr;
FCAddressInfo **end;
if (addr_array->count <= 0) {
*err_no = ENOENT;
return NULL;
}
current = addr_array->addrs + addr_array->index;
if ((*current)->conn.sock >= 0) {
return &(*current)->conn;
}
if ((*err_no= conn_pool_connect_server_ex(&(*current)->conn,
connect_timeout, bind_ipaddr, log_connect_error)) == 0)
{
return &(*current)->conn;
}
if (addr_array->count == 1) {
return NULL;
}
end = addr_array->addrs + addr_array->count;
for (addr=addr_array->addrs; addr<end; addr++) {
if (addr == current) {
continue;
}
if ((*err_no= conn_pool_connect_server_ex(&(*addr)->conn,
connect_timeout, bind_ipaddr,
log_connect_error)) == 0)
{
addr_array->index = addr - addr_array->addrs;
return &(*addr)->conn;
}
}
return NULL;
}
void fc_server_disconnect(FCAddressPtrArray *addr_array)
{
FCAddressInfo **current;
current = addr_array->addrs + addr_array->index;
if ((*current)->conn.sock >= 0) {
close((*current)->conn.sock);
(*current)->conn.sock = -1;
}
}

View File

@ -33,6 +33,7 @@ typedef struct {
typedef struct {
int alloc;
int count;
int index;
FCAddressInfo **addrs;
} FCAddressPtrArray;
@ -172,6 +173,16 @@ 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);
#define fc_server_check_connect(addr_array, connect_timeout, err_no) \
fc_server_check_connect_ex(addr_array, connect_timeout, NULL, true, err_no)
void fc_server_disconnect(FCAddressPtrArray *addr_array);
#ifdef __cplusplus
}
#endif