Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common problems of PHP/Swoole persistent connection

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

PHP/Swoole long connection common problems, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Connection failure problem

Examples

Among them, the common errors reported by Redis are:

Configuration item: timeout

Error message: Error while reading line from the server

Redis can be configured to close the connection if the client does not send data to the Redis server after a few seconds.

Common errors in MySQL:

Configuration items: wait_timeout & interactive_timeout

Error message: has gone away

Like Redis servers, MySQL periodically cleans up useless connections.

How to solve

1. Reconnect when you use it

2. Send the heartbeat regularly to maintain the connection

Reconnect when you use it

The advantage is simplicity, while the disadvantage is the problem of short connection.

Regularly send a heartbeat to maintain a connection

Recommend.

How to maintain a long connection

Tcp_keepalive implemented in tcp protocol

The underlying operating system provides a set of keepalive configurations for tcp:

Tcp_keepalive_time (integer; default: 7200; since Linux 2.2) The number of seconds a connection needs to be idle before TCPbegins sending out keep-alive probes. Keep-alives are sent onlywhen the SO_KEEPALIVE socket option is enabled. The defaultvalue is 7200 seconds (2 hours). An idle connection isterminated after approximately an additional 11 minutes (9probes an interval of 75 seconds apart) when keep-alive isenabled. Note that underlying connection tracking mechanisms andapplication timeouts may be much shorter. Tcp_keepalive_intvl (integer; default: 75; since Linux 2.4) The number of seconds between TCP keep-alive probes.tcp_keepalive_probes (integer; default: 9; since Linux 2.2) The maximum number of TCP keep-alive probes to send beforegiving up and killing the connection if no response is obtainedfrom the other end.8

The Swoole underlying layer opens up these configurations, such as:

? php$server = new\ Swoole\ Server ('127.0.0.1, 6666, SWOOLE_PROCESS); $server- > set ([' worker_num' = > 1 tcp_keepalive_intvl'tcp_keepcount' / tcp_keepalive_intvl'tcp_keepcount' = > 5, / / tcp_keepalive_probes])

Where:

'open_tcp_keepalive' = > 1, / / master switch, which is used to turn on tcp_keepalive'tcp_keepidle' = > 4, / / the policy for detecting / / detecting when there is no data transmission in 4s is as follows:' tcp_keepinterval' = > 1, / / 1s probe, that is, send a packet to the client every 1s (and then the client may return an ack packet if the server receives the ack packet Then it means that the connection is alive) 'tcp_keepcount' = > 5, / / the number of probes. If the client has not returned the ack packet after more than 5 times, then close this connection

Let's have a field test. The server script is as follows:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report