问题原文
原帖链接 · Jan Discart · CC BY-SA 4.0
I'm running an app-service on Azure with a PHP application and a MySQL in-app database on it. This app service has a lot of small requests coming in (i.e. to get or update a leaderboard for a game).
Often, I see the following warning coming by in the log file of the PHP application:
PHP Warning: mysqli_connect(): (HY000/2002): Only one usage of each socket address (protocol/network address/port) is normally permitted.
I receive several complaints from players that they fail to update their score on the leaderboards, which I connect to seeing this warning a lot in the logs. I traced the issue being that connections are being held in the TIME_WAIT state for too long on the level of the operating system, or that the range of ports that can be used is simply too small to handle more connections.
How can I adjust this on Azure so that this warning is either gone, or a lot less frequent? I use the close() function on my mysqli objects to clear the connection, but as I understand correctly, this doesn't immediately release the underlying resources in the OS.
Thanks in advance!
已采纳解答
原帖链接 · Piotr P. Karwasz · CC BY-SA 4.0
The problem is in the way TCP/IP connections close:
- You ask to close the connection (
FIN), - The server acknowledges it received your request (
ACK) and also asks to close its side (FIN), - You acknowledge (
ACK) that and wait around 4 minutes (TIME_WAITstate) to be sure that no packets sent by the server and delayed by the network will arrive. During this time you can not use the same port to connect to the server.
mysqli probably uses several source ports, but even if it used all of them, the limit is around 270 connections per second.
To solve your problem, enable persistent connections in PHP and don't close them. An old connection will be reused whenever possible.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
