Open WALL-E opened 7 years ago
服务端代码
#!/usr/bin/python
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
s.bind(("0.0.0.0", 1919))
s.listen(1)
while 1:
clientsock, clientaddr = s.accept()
print "Got connection from", clientsock.getpeername()
clientsock.close()
连接4个终端,可以登录同一个IP地址。
- 【终端1】 172.28.32.101
- 【终端2】 172.28.32.101
- 【终端3】 172.28.32.101
- 【终端4】 172.28.32.101
./serv.py
for i in {1..30}; do sock 172.28.32.101 1919; done
结论:
[root@vagrant-172-28-32-101 ~]# ss -ltnp|more
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:* users:(("sshd",pid=1009,fd=3))
LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=1787,fd=13))
LISTEN 0 1 *:1919 *:* users:(("serv.py",pid=2534,fd=3))
LISTEN 0 1 *:1919 *:* users:(("serv.py",pid=2532,fd=3))
LISTEN 0 1 *:1919 *:* users:(("serv.py",pid=2530,fd=3))
LISTEN 0 128 :::22 :::* users:(("sshd",pid=1009,fd=4))
LISTEN 0 100 ::1:25 :::* users:(("master",pid=1787,fd=14))
2013-01-23 13:44:10 -0500 Merge branch 'soreuseport'
The TCP implementation has a problem in that the request sockets for a listener are attached to a listener socket. If a SYN is received, a listener socket is chosen and request structure is created (SYN-RECV state). If the subsequent ack in 3WHS does not match the same port by so_reusport, the connection state is not found (reset) and the request structure is orphaned. This scenario would occur when the number of listener sockets bound to a port changes (new ones are added, or old ones closed). We are looking for a solution to this, maybe allow multiple sockets to share the same request table...
目前,目前不知道是否已经修复,只能下载内核代码看看啦
git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
Solve #11
Resthub生产环境可以使用reuseport指令,出现风险为小概率事件。
SO_REUSEPORT
This socket option allows multiple sockets to listen on the same IP address and port combination. The kernel then load balances incoming connections across the sockets.
SO_REUSEADDR
KERNEL_SOURCE/include/net/inet_hashtables.h