Muduo network programming example 6: Limit the maximum number of concurrent connections on the server

Source: Internet
Author: User

Why limit the number of concurrent connections?

On the one hand, we do not want service programs to be overloaded, and on the other hand, because file descriptor is a scarce resource. If file descriptor is exhausted, it is very tricky (with "malloc failure/new () to throw std :: bad_alloc ).

 

 

The acceptor of Muduo is implemented in this way. However, this method is not guaranteed to be correct under multiple threads and there will be race condition. (Question: What is race condition ?)

In fact, there is another relatively simple method: file descriptor is hard limit, we can set a slightly lower soft limit, if it exceeds soft limit, we will take the initiative to close the new connection, in this way, the boundary condition "file descriptor depletion" is avoided. For example, if the max file descriptor of the current process is 1024, we can enter the "reject new connections" status when the number of connections reaches 1000, which leaves us enough space to move.

Limit the number of concurrent connections in Muduo

The practice of limiting concurrent connections in Muduo is surprisingly simple. Taking the EchoServer that has appeared in Muduo network programming example 0: Preface as an example, you only need to add an int member to it to indicate the number of active connections. (For multi-threaded programs, muduo: AtomicInt32 should be used .)

Class EchoServer
{
Public:
EchoServer (muduo: net: EventLoop * loop,
Const muduo: net: InetAddress & listenAddr,
Int maxConnections );

Void start ();

Private:
Void onConnection (const muduo: net: TcpConnectionPtr & conn );

Void onMessage (const muduo: net: TcpConnectionPtr & conn,
Muduo: net: Buffer * buf,
Muduo: Timestamp time );

Muduo: net: EventLoop * loop _;
Muduo: net: TcpServer server _;
Int numConnected _; // shocould be atomic_int
Const int kMaxConnections;
};
Then, judge the number of active connections in EchoServer: onConnection (). If the maximum number of active connections is exceeded, kill the connection.

Void EchoServer: onConnection (const TcpConnectionPtr & conn)
{
LOG_INFO <"EchoServer-" <conn-> peerAddress (). toHostPort () <"->"
<Conn-> localAddress (). toHostPort () <"is"
<(Conn-> connected ()? "UP": "DOWN ");

If (conn-> connected ())
{
++ NumConnected _;
If (numConnected _> kMaxConnections)
{
Conn-> shutdown ();
}
}
Else
{
-- NumConnected _;
}
LOG_INFO <"numConnected =" <numConnected _;
}
This approach can actively prevent the use of file descriptor.

In addition, if there is a service with business logic, you can send a simple response before shutdown (), indicating that the load capacity of the Service Program is saturated, prompt the client to try the next available server (of course, the next available server address does not have to be given in this response, the client can go to the name service query), so that the client can quickly failover.

This article describes how to deal with idle connection Timeout: If a connection does not input data for a long time (several seconds), the connection is kicked off. There are many solutions. I use Time Wheel to solve them.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.