From 3e3bcda2df735153b334ac228149ee3f18fdaa99 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Thu, 27 Feb 2020 15:31:44 +0800 Subject: [PATCH] add functions fc_floor_prime and fc_ceil_prime --- HISTORY | 3 +- src/connection_pool.c | 2 ++ src/fast_buffer.c | 10 +++--- src/fast_buffer.h | 11 ++++++- src/server_id_func.c | 49 ++++++++++++++++++++++++++-- src/server_id_func.h | 9 +++++- src/shared_func.c | 57 +++++++++++++++++++++++++++++++++ src/shared_func.h | 22 +++++++++++++ src/tests/test_server_id_func.c | 1 - 9 files changed, 153 insertions(+), 11 deletions(-) diff --git a/HISTORY b/HISTORY index fccf998..e64045a 100644 --- a/HISTORY +++ b/HISTORY @@ -1,5 +1,5 @@ -Version 1.44 2020-02-25 +Version 1.44 2020-02-27 * add test file src/tests/test_pthread_lock.c * add uniq_skiplist.[hc] * add function split_string_ex @@ -10,6 +10,7 @@ Version 1.44 2020-02-25 * add function fast_mpool_log_stats * add files: server_id_func.[hc] * common_blocked_queue support pop all nodes + * shared_func.[hc]: add functions fc_floor_prime and fc_ceil_prime Version 1.43 2019-12-25 * replace function call system to getExecResult, diff --git a/src/connection_pool.c b/src/connection_pool.c index b01be57..e9684fd 100644 --- a/src/connection_pool.c +++ b/src/connection_pool.c @@ -119,6 +119,8 @@ int conn_pool_connect_server_ex(ConnectionInfo *pConnection, { if ((result=socketBind2(domain, pConnection->sock, bind_ipaddr, 0)) != 0) { + close(pConnection->sock); + pConnection->sock = -1; return result; } } diff --git a/src/fast_buffer.c b/src/fast_buffer.c index 098c8a3..7efc974 100644 --- a/src/fast_buffer.c +++ b/src/fast_buffer.c @@ -42,17 +42,17 @@ void fast_buffer_destroy(FastBuffer *buffer) } } -int fast_buffer_check(FastBuffer *buffer, const int inc_len) +int fast_buffer_check_capacity(FastBuffer *buffer, const int capacity) { int alloc_size; char *buff; - if (buffer->alloc_size >= buffer->length + inc_len) + if (buffer->alloc_size >= capacity) { return 0; } alloc_size = buffer->alloc_size * 2; - while (alloc_size <= buffer->length + inc_len) + while (alloc_size <= capacity) { alloc_size *= 2; } @@ -97,7 +97,7 @@ int fast_buffer_append(FastBuffer *buffer, const char *format, ...) } else //maybe full, realloc and try again { - if ((result=fast_buffer_check(buffer, len)) == 0) + if ((result=fast_buffer_check(buffer, len + 1)) == 0) { va_start(ap, format); buffer->length += vsnprintf(buffer->data + buffer->length, @@ -120,7 +120,7 @@ int fast_buffer_append_buff(FastBuffer *buffer, const char *data, const int len) { return 0; } - if ((result=fast_buffer_check(buffer, len)) != 0) + if ((result=fast_buffer_check(buffer, len + 1)) != 0) { return result; } diff --git a/src/fast_buffer.h b/src/fast_buffer.h index da873f7..866b406 100644 --- a/src/fast_buffer.h +++ b/src/fast_buffer.h @@ -41,7 +41,16 @@ static inline void fast_buffer_reset(FastBuffer *buffer) void fast_buffer_destroy(FastBuffer *buffer); -int fast_buffer_check(FastBuffer *buffer, const int inc_len); +#define fast_buffer_check(buffer, inc_len) \ + fast_buffer_check_inc_size(buffer, inc_len) + +int fast_buffer_check_capacity(FastBuffer *buffer, const int capacity); + +static inline int fast_buffer_check_inc_size(FastBuffer *buffer, + const int inc_size) +{ + return fast_buffer_check_capacity(buffer, buffer->length + inc_size); +} int fast_buffer_append(FastBuffer *buffer, const char *format, ...); diff --git a/src/server_id_func.c b/src/server_id_func.c index d1e8d56..f0c3293 100644 --- a/src/server_id_func.c +++ b/src/server_id_func.c @@ -1476,7 +1476,7 @@ ConnectionInfo *fc_server_check_connect_ex(FCAddressPtrArray *addr_array, return &(*current)->conn; } - if ((*err_no= conn_pool_connect_server_ex(&(*current)->conn, + if ((*err_no=conn_pool_connect_server_ex(&(*current)->conn, connect_timeout, bind_ipaddr, log_connect_error)) == 0) { return &(*current)->conn; @@ -1491,7 +1491,7 @@ ConnectionInfo *fc_server_check_connect_ex(FCAddressPtrArray *addr_array, if (addr == current) { continue; } - if ((*err_no= conn_pool_connect_server_ex(&(*addr)->conn, + if ((*err_no=conn_pool_connect_server_ex(&(*addr)->conn, connect_timeout, bind_ipaddr, log_connect_error)) == 0) { @@ -1513,3 +1513,48 @@ void fc_server_disconnect(FCAddressPtrArray *addr_array) (*current)->conn.sock = -1; } } + +int fc_server_make_connection_ex(FCAddressPtrArray *addr_array, + ConnectionInfo *conn, const int connect_timeout, + const char *bind_ipaddr, const bool log_connect_error) +{ + FCAddressInfo **current; + FCAddressInfo **addr; + FCAddressInfo **end; + int result; + + if (addr_array->count <= 0) { + return ENOENT; + } + + current = addr_array->addrs + addr_array->index; + *conn = (*current)->conn; + conn->sock = -1; + if ((result=conn_pool_connect_server_ex(conn, connect_timeout, + bind_ipaddr, log_connect_error)) == 0) + { + return 0; + } + + if (addr_array->count == 1) { + return result; + } + + end = addr_array->addrs + addr_array->count; + for (addr=addr_array->addrs; addrconn; + conn->sock = -1; + if ((result=conn_pool_connect_server_ex(conn, connect_timeout, + bind_ipaddr, log_connect_error)) == 0) + { + addr_array->index = addr - addr_array->addrs; + return 0; + } + } + + return result; +} diff --git a/src/server_id_func.h b/src/server_id_func.h index 3a7bc10..f1ce7a8 100644 --- a/src/server_id_func.h +++ b/src/server_id_func.h @@ -180,9 +180,16 @@ ConnectionInfo *fc_server_check_connect_ex(FCAddressPtrArray *addr_array, #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); + +int fc_server_make_connection_ex(FCAddressPtrArray *addr_array, + ConnectionInfo *conn, 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) + #ifdef __cplusplus } #endif diff --git a/src/shared_func.c b/src/shared_func.c index 157b25c..23ba2aa 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -2947,3 +2947,60 @@ int fc_delete_file_ex(const char *filename, const char *caption) return result; } + +bool fc_is_prime(const int n) +{ + int loop; + int i; + + if (n <= 0) + { + return false; + } + + loop = lround(sqrt((double)n)); + for (i=2; i<=loop; i++) + { + if (n % i == 0) + { + return false; + } + } + + return true; +} + +int fc_floor_prime(const int n) +{ + int start; + int i; + + start = (n % 2 == 0 ? n - 1 : n); + for (i = start; i > 0; i -= 2) + { + if (fc_is_prime(i)) + { + return i; + } + } + + return 1; +} + +int fc_ceil_prime(const int n) +{ + int i; + + if (n <= 0) + { + return 1; + } + + i = (n % 2 == 0 ? n + 1 : n); + while (!fc_is_prime(i)) + { + i += 2; + } + + return i; +} diff --git a/src/shared_func.h b/src/shared_func.h index 5a7e501..c5f7d6d 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -889,6 +889,28 @@ static inline int fc_delete_file(const char *filename) return fc_delete_file_ex(filename, ""); } +/** if prime number + * parameters: + * n: the number to detect + * return: true for prime number, otherwise false +*/ +bool fc_is_prime(const int n); + + +/** find the largest prime number not greater than n + * parameters: + * n: the number to detect + * return: the largest prime number near n +*/ +int fc_floor_prime(const int n); + +/** find the smallest prime number not less than n + * parameters: + * n: the number to detect + * return: the smallest prime number near n +*/ +int fc_ceil_prime(const int n); + #ifdef __cplusplus } #endif diff --git a/src/tests/test_server_id_func.c b/src/tests/test_server_id_func.c index f84f519..285b04d 100644 --- a/src/tests/test_server_id_func.c +++ b/src/tests/test_server_id_func.c @@ -26,7 +26,6 @@ int main(int argc, char *argv[]) } log_init(); - if ((result=fc_server_load_from_file_ex(&ctx, config_filename, default_port, min_hosts_each_group, share_between_groups)) != 0)