tcp_quick_ack option for Linux

pull/37/head
YuQing 2020-12-06 16:59:09 +08:00
parent ff7109fcd4
commit 0cd65f088c
6 changed files with 51 additions and 32 deletions

View File

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

View File

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

View File

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

View File

@ -20,9 +20,6 @@
#include <string.h>
#include <errno.h>
#include <time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <fcntl.h>
#include <sys/ioctl.h>
@ -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;
}

View File

@ -24,6 +24,9 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#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);

View File

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