04-problems that may occur when ports are frequently opened or disabled in Windows
Zheng Yi 20100810 is affiliated with section 07. Miscellaneous
Lao Zhao wrote an article about the problems that occur when Windows frequently opens and closes ports. he tested the MongoDB Database Service in cent OS using TCP connections from Windows Web Server 2008 R2 and ran out of socket connection resources.ProgramReport "operations on sockets cannot be performed because the system buffer space is insufficient or the queue is full ." Error.
Socket connection resources are exhausted, which is common in Windows server. If the user program is well written, it is generally caused by some default parameters set by Microsoft (or other software vendors.
I have previously written an article titled 02-twisted about the long link of Web server socket. | 07. miscellaneous | Python describes another common problem of Windows Server: In the python development language, compile a web server using the twisted framework to receive Google Reader pushed by the hub server of Google pubsubhubbub.ArticleWhen sharing information, the file descriptor reaches the upper limit. This mode is characterized by using twisted. Web. server to connect to the pubsubhubbub hub server. Both parties support reuse of connections. It is reasonable to say that since long connections are maintained and socket connections are reused, this"Too plugin file descriptors in select"Error. Here, the knowledge point is that in windows, the select module file descriptor is a maximum of 512, and in Linux, the limit is 32767. If the limit is exceeded, an exception similar to the above occurs.
Lao Zhao's article pointed out an interesting knowledge point: the temporary port number can be assigned a range,
Definition of temporary port number range
When a client program (such as a browser) initializes a connection to connect to a remote service (such as a website), the client opens an "Ephemeral (temporary)" port, generally, the port number is randomly allocated. You can use the Microsoft tool tcpview.
When this process assigns a port number for the outbound connection, the allocable range of the port number is related to the operating system. One of the main influencing factors isMaxuserportIn the Registry's HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \ Tcpip \ Parameters.
The following table lists the port ranges that can be allocated to different Windows operating systems:
Operating System |
Maxuserport Numeric Value Description |
Port range, if maxuserport Not Defined |
Port range, if maxuserport Defined |
Minimum value |
Maximum Value |
Windows NT/2000/XP/2003 |
Ending Port |
From 1025 to 5000 |
1025 to [maxuserport] |
5000 |
65535 |
Windows 2000/XP/2003 with kb951748/kb951746 |
Ending Port |
From 49152 to 65535 |
1025 to [maxuserport] |
5000 |
65535 |
For Windows Vista/2008 |
Number of Ports |
From 49152 to 65535 |
49152 to [49152 + maxuserport −1] |
255 |
16384 bytes † |
Table 1 port range
Of course, if you install some Microsoft services on the server, the maxuserport parameter will be automatically modified. For example, Small Business Server 2000/2003 will be changed to 60000, and ISA server will be changed to 65535, the Exchange Server 2003 is changed to 60000.
After reading this table, you will know:
1. Do not use a port number smaller than 1025;
2. Try to use the systemTemporary port range (beyond the ephemeral port range)For example, if you define the port number to be opened by your application as 8000, rather than 5000.
3. Reuse connections.
After checking my laptop (Windows XP) and server (Windows 2003), The maxuserport parameter is not set in the registry. Therefore, the port range is the default value. on the server, the allocable temporary ports are from 1025 to 5000.
Back to the problem of opening too many file descriptors,
Failed to reuse port + keep persistent connection effective
If there are enough Google Reader users to listen to and these users share articles in a centralized manner, Google pubsubhubub hub server will push the data at an extremely fast speed, in this case, if the socket port fails to be reused and the persistent connection policy takes effect, the file descriptor opened on the port listened by the twisted web server quickly burst.
Solution:
1. Refer to 02-twisted to build a long socket connection for Web Server | 07. Miscellaneous | python,
Twisted. web. server. the initialization function of the site class has an optional parameter timeout. Its default value is 60*60*12, that is, 12 hours. It aims to automatically close idle ports when timeout occurs. In our scenario, the timeout time is too long, so many socket connections in the established State will accumulate.
Therefore, we can reduce it to 15 minutes (or shorter) to automatically disable connections that are not reused as soon as possible. Run the following command at the beginning:
Reactor. listentcp (8080, site (mywebresource. Setup (), Timeout = 60*15))
You can.
2. Refer to 03-pubsubhubbub and twisted persistent connections capabilities | 07. miscellaneous | python, after receiving the data pushed by the other server, it is asynchronously thrown to another method for parsing and processing. The render_post method immediately returns the not_done_yet flag, and the asynchronous data processing is successful, and then finish the current request.
Reference resources:
1. Problems with Windows ports frequently opened and closed;
2. Choosing a TCP port for a network service;
3. 02-twisted: long socket connection problem for Web Server construction | 07. Miscellaneous | Python;
4. Persistent connections capabilities of 03-pubsubhubbub and twisted | 07. Miscellaneous | Python
5. TCP-Wait Status and its impact on busy servers.