diff --git a/HISTORY b/HISTORY index 9c9f0df..a3eddea 100644 --- a/HISTORY +++ b/HISTORY @@ -1,5 +1,5 @@ -Version 1.44 2020-11-29 +Version 1.44 2020-12-06 * add test file src/tests/test_pthread_lock.c * add uniq_skiplist.[hc] * add function split_string_ex @@ -38,6 +38,7 @@ Version 1.44 2020-11-29 * fast_mblock.[hc]: support alloc elements limit * sockopt.[hc]: add function asyncconnectserverbyip * add locked_timer.[hc]: time wheel timer with lock + * tcp_quick_ack option for Linux Version 1.43 2019-12-25 * replace function call system to getExecResult, diff --git a/src/shared_func.c b/src/shared_func.c index 17e4db4..56dd6d5 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -2170,7 +2170,7 @@ int cmp_by_ip_addr_t(const void *p1, const void *p2) return memcmp((in_addr_t *)p1, (in_addr_t *)p2, sizeof(in_addr_t)); } -int parse_bytes(char *pStr, const int default_unit_bytes, int64_t *bytes) +int parse_bytes(const char *pStr, const int default_unit_bytes, int64_t *bytes) { char *pReservedEnd; diff --git a/src/shared_func.h b/src/shared_func.h index 2a87049..6b356b4 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -673,7 +673,7 @@ int cmp_by_ip_addr_t(const void *p1, const void *p2); * bytes: store the parsed bytes * return: error no , 0 success, != 0 fail */ -int parse_bytes(char *pStr, const int default_unit_bytes, int64_t *bytes); +int parse_bytes(const char *pStr, const int default_unit_bytes, int64_t *bytes); /** set rand seed * return: error no , 0 success, != 0 fail diff --git a/src/sockopt.c b/src/sockopt.c index c8a8c60..90f8d94 100644 --- a/src/sockopt.c +++ b/src/sockopt.c @@ -20,9 +20,6 @@ #include #include #include -#include -#include -#include #include #include #include @@ -86,6 +83,10 @@ #endif #endif +#ifdef OS_LINUX + bool g_tcp_quick_ack = false; +#endif + static bool try_again_when_interrupt = true; void tcp_set_try_again_when_interrupt(const bool value) @@ -93,6 +94,13 @@ void tcp_set_try_again_when_interrupt(const bool value) try_again_when_interrupt = value; } +void tcp_set_quick_ack(const bool value) +{ +#ifdef OS_LINUX + g_tcp_quick_ack = value; +#endif +} + int tcpgets(int sock, char* s, const int size, const int timeout) { int result; @@ -215,14 +223,15 @@ int tcprecvdata_ex(int sock, void *data, const int size, \ break; } + TCP_SET_QUICK_ACK(sock); left_bytes -= read_bytes; p += read_bytes; } if (count != NULL) - { - *count = size - left_bytes; - } + { + *count = size - left_bytes; + } return ret_code; } @@ -339,6 +348,7 @@ int tcprecvdata_nb_ms(int sock, void *data, const int size, \ read_bytes = recv(sock, p, left_bytes, 0); if (read_bytes > 0) { + TCP_SET_QUICK_ACK(sock); left_bytes -= read_bytes; p += read_bytes; continue; @@ -1620,9 +1630,6 @@ int tcpsendfile_ex(int sock, const char *filename, const int64_t file_offset, \ int tcpsetserveropt(int fd, const int timeout) { - int flags; - int result; - struct linger linger; struct timeval waittime; @@ -1665,22 +1672,7 @@ int tcpsetserveropt(int fd, const int timeout) __LINE__, errno, STRERROR(errno)); } - flags = 1; - if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, \ - (char *)&flags, sizeof(flags)) < 0) - { - logError("file: "__FILE__", line: %d, " \ - "setsockopt failed, errno: %d, error info: %s", \ - __LINE__, errno, STRERROR(errno)); - return errno != 0 ? errno : EINVAL; - } - - if ((result=tcpsetkeepalive(fd, 2 * timeout + 1)) != 0) - { - return result; - } - - return 0; + return tcpsetnodelay(fd, timeout); } int tcpsetkeepalive(int fd, const int idleSeconds) @@ -1834,14 +1826,15 @@ int tcpsetnodelay(int fd, const int timeout) } flags = 1; - if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, \ + if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flags, sizeof(flags)) < 0) { - logError("file: "__FILE__", line: %d, " \ - "setsockopt failed, errno: %d, error info: %s", \ + logError("file: "__FILE__", line: %d, " + "setsockopt failed, errno: %d, error info: %s", __LINE__, errno, STRERROR(errno)); return errno != 0 ? errno : EINVAL; } + TCP_SET_QUICK_ACK(fd); return 0; } diff --git a/src/sockopt.h b/src/sockopt.h index 4ad91b5..eff3e84 100644 --- a/src/sockopt.h +++ b/src/sockopt.h @@ -24,6 +24,9 @@ #include #include #include +#include +#include +#include #include "common_define.h" #define FC_NET_TYPE_NONE 0 @@ -80,10 +83,30 @@ typedef struct sockaddr_convert_s { #define SET_SOCKOPT_NOSIGPIPE(sock) #endif +#ifdef OS_LINUX +#define TCP_SET_QUICK_ACK(sock) \ + do { \ + int quick_ack = 1; \ + if (g_tcp_quick_ack && setsockopt(sock, IPPROTO_TCP, \ + TCP_QUICKACK, &quick_ack, sizeof(int)) < 0) \ + { \ + logError("file: "__FILE__", line: %d, " \ + "setsockopt failed, errno: %d, error info: %s", \ + __LINE__, errno, STRERROR(errno)); \ + } \ + } while (0) +#else +#define TCP_SET_QUICK_ACK(sock) +#endif + #ifdef __cplusplus extern "C" { #endif +#ifdef OS_LINUX + extern bool g_tcp_quick_ack; +#endif + typedef int (*getnamefunc)(int socket, struct sockaddr *address, \ socklen_t *address_len); @@ -632,6 +655,8 @@ static inline void tcp_dont_try_again_when_interrupt() tcp_set_try_again_when_interrupt(false); } +void tcp_set_quick_ack(const bool value); + int fc_get_net_type_by_name(const char *net_type); int fc_get_net_type_by_ip(const char *ip); diff --git a/src/tests/test_uniq_skiplist.c b/src/tests/test_uniq_skiplist.c index 5024c91..d81ad00 100644 --- a/src/tests/test_uniq_skiplist.c +++ b/src/tests/test_uniq_skiplist.c @@ -27,7 +27,7 @@ #define COUNT 1000000 #define LEVEL_COUNT 16 -#define MIN_ALLOC_ONCE 8 +#define MIN_ALLOC_ONCE 4 #define LAST_INDEX (COUNT - 1) static int *numbers;