unix: optimize uv__tcp_keepalive cpp directives (#4275)
Reduce the amount of code being compiled and trim trailing whitespace in passing.
This commit is contained in:
parent
a407b232f0
commit
a7cbda92b6
@ -470,11 +470,12 @@ int uv__tcp_keepalive(int fd, int on, unsigned int delay) {
|
|||||||
if (delay == 0)
|
if (delay == 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
#ifdef __sun
|
||||||
/* The implementation of TCP keep-alive on Solaris/SmartOS is a bit unusual
|
/* The implementation of TCP keep-alive on Solaris/SmartOS is a bit unusual
|
||||||
* compared to other Unix-like systems.
|
* compared to other Unix-like systems.
|
||||||
* Thus, we need to specialize it on Solaris. */
|
* Thus, we need to specialize it on Solaris.
|
||||||
#ifdef __sun
|
*
|
||||||
/* There are two keep-alive mechanisms on Solaris:
|
* There are two keep-alive mechanisms on Solaris:
|
||||||
* - By default, the first keep-alive probe is sent out after a TCP connection is idle for two hours.
|
* - By default, the first keep-alive probe is sent out after a TCP connection is idle for two hours.
|
||||||
* If the peer does not respond to the probe within eight minutes, the TCP connection is aborted.
|
* If the peer does not respond to the probe within eight minutes, the TCP connection is aborted.
|
||||||
* You can alter the interval for sending out the first probe using the socket option TCP_KEEPALIVE_THRESHOLD
|
* You can alter the interval for sending out the first probe using the socket option TCP_KEEPALIVE_THRESHOLD
|
||||||
@ -505,15 +506,15 @@ int uv__tcp_keepalive(int fd, int on, unsigned int delay) {
|
|||||||
#if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL) && defined(TCP_KEEPCNT)
|
#if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL) && defined(TCP_KEEPCNT)
|
||||||
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(idle)))
|
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(idle)))
|
||||||
return UV__ERR(errno);
|
return UV__ERR(errno);
|
||||||
|
|
||||||
intvl = idle/3;
|
intvl = idle/3;
|
||||||
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &intvl, sizeof(intvl)))
|
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &intvl, sizeof(intvl)))
|
||||||
return UV__ERR(errno);
|
return UV__ERR(errno);
|
||||||
|
|
||||||
cnt = 3;
|
cnt = 3;
|
||||||
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &cnt, sizeof(cnt)))
|
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &cnt, sizeof(cnt)))
|
||||||
return UV__ERR(errno);
|
return UV__ERR(errno);
|
||||||
return 0;
|
#else
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Fall back to the first implementation of tcp-alive mechanism for older Solaris,
|
/* Fall back to the first implementation of tcp-alive mechanism for older Solaris,
|
||||||
* simulate the tcp-alive mechanism on other platforms via `TCP_KEEPALIVE_THRESHOLD` + `TCP_KEEPALIVE_ABORT_THRESHOLD`.
|
* simulate the tcp-alive mechanism on other platforms via `TCP_KEEPALIVE_THRESHOLD` + `TCP_KEEPALIVE_ABORT_THRESHOLD`.
|
||||||
*/
|
*/
|
||||||
@ -528,14 +529,15 @@ int uv__tcp_keepalive(int fd, int on, unsigned int delay) {
|
|||||||
int time_to_abort = intvl * cnt;
|
int time_to_abort = intvl * cnt;
|
||||||
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE_ABORT_THRESHOLD, &time_to_abort, sizeof(time_to_abort)))
|
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE_ABORT_THRESHOLD, &time_to_abort, sizeof(time_to_abort)))
|
||||||
return UV__ERR(errno);
|
return UV__ERR(errno);
|
||||||
|
|
||||||
return 0;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#else /* !defined(__sun) */
|
||||||
|
|
||||||
#ifdef TCP_KEEPIDLE
|
#ifdef TCP_KEEPIDLE
|
||||||
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &delay, sizeof(delay)))
|
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &delay, sizeof(delay)))
|
||||||
return UV__ERR(errno);
|
return UV__ERR(errno);
|
||||||
#elif defined(TCP_KEEPALIVE)
|
#elif defined(TCP_KEEPALIVE)
|
||||||
|
/* Darwin/macOS uses TCP_KEEPALIVE in place of TCP_KEEPIDLE. */
|
||||||
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &delay, sizeof(delay)))
|
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &delay, sizeof(delay)))
|
||||||
return UV__ERR(errno);
|
return UV__ERR(errno);
|
||||||
#endif
|
#endif
|
||||||
@ -552,6 +554,7 @@ int uv__tcp_keepalive(int fd, int on, unsigned int delay) {
|
|||||||
return UV__ERR(errno);
|
return UV__ERR(errno);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#endif /* !defined(__sun) */
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user