Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use SOCK_NONBLOCK to reduce system calls for outgoing connections (#293)
What this PR mainly does: 1. Refactor the `anetCreateSocket()` to make it more generic for more socket arguments, and use `SOCK_NONBLOCK` if available, which will reduce two system calls (`F_GETFL` and `F_SETFL`) of enabling the non-blocking mode on each newly created socket. 2. Clean up the unused `anetUnixGenericConnect()` that calls `anetCreateSocket()`. `SOCK_NONBLOCK` for system call `socket()` is supported on most UNIX-like platforms (`linux`, `dragonfly`, `freebsd`, `netbsd`, `openbsd`, `solaris`, etc.). This improvement will significantly reduce the system calls considering how massively `anetTcpGenericConnect()` will be called when needed. As for the cleanup, `anetUnixGenericConnect` was introduced in c61e692 and the only reference back then was from the `createClient()` in `redis-benchmark.c` which had been removed in ec8f066 and made it the dead code. Most of that dead code was also cleaned up in f657315, and it seems that the `anetUnixGenericConnect` got left out. Therefore, I also cleaned it up, ***but I'm not so certain about doing this cleanup in this PR. Maybe you would prefer to do it in a separate PR?*** References: - [socket(2) on Linux](https://man7.org/linux/man-pages/man2/socket.2.html) - [socket(2) on FreeBSD](https://man.freebsd.org/cgi/man.cgi?socket(2)) - [socket(2) on DragonFly](https://man.dragonflybsd.org/?command=socket§ion=2) - [socket(2) on NetBSD](https://man.netbsd.org/socket.2) - [socket(2) on OpenBSD](https://man.openbsd.org/socket.2) - [socket(3c) on Solaris](https://docs.oracle.com/cd/E88353_01/html/E37843/socket-3c.html) - [socket(3socket) on illumos](https://illumos.org/man/3SOCKET/socket) --------- Signed-off-by: Andy Pan <i@andypan.me>
- Loading branch information