How to speed up writing to tcp socket?

I am working on a web server that returns tiny JSON (about 200 bytes). Business logic spends about 2-3 microseconds, but writing to a socket takes about 25 microseconds. I use writefor a single buffer and writevfor multiple buffers.

I already disabled the Nagle algorithm by enabling TCP_NODELAY. Are there other ways to speed up recording?

Listening options for sockets:

......
if (listen(sfd, SOMAXCONN) == -1) { ... }

int val = true;
if (setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)) == -1) { ... }
if (setsockopt(sfd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &val, sizeof(val)) == -1) { ... }
if (setsockopt(sfd, IPPROTO_TCP, TCP_QUICKACK, &val, sizeof(val)) == -1) { ... }

auto flags = fcntl(sfd, F_GETFL, 0);
if (flags == -1) { ... }
flags |= O_NONBLOCK;
if (fcntl(sfd, F_SETFL, flags) == -1) { ... }
......

Options for accepted sockets:

......
int infd = accept(sfd, &in_addr, &in_len);
if (infd == -1) { ... }

auto flags = fcntl(infd, F_GETFL, 0);
if (flags == -1) { ... }
flags |= O_NONBLOCK;
if (fcntl(infd, F_SETFL, flags) == -1) { ... }

int val = true;
if (setsockopt(infd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)) == -1) { ... }
......

Thank you in advance!

+4
source share
1 answer

, , - . , " " (: ), . TCP-, , ... , UDP, , . TCP , , . , .

0

Source: https://habr.com/ru/post/1684437/


All Articles