problem background
Use swoole4.8 version as websocket server
nginx proxy swoole service
Problem recurrence
Enable websocket service
heartbeat_check_interval is set to 60
heartbeat_idle_time is not set
Digression: heartbeat_check_interval indicates how often to rotate, in seconds, if the connection is within 120 seconds (the default is twice the interval when heartbeat_idle_time is not set), no data is sent to the server, the connection will be forcibly closed. If not configured, heartbeat will not be enabled, and the configuration is disabled by default.
The server does not actively send heartbeat packets to the client, but passively waits for the client to send a heartbeat. The heartbeat_check on the server side is only to detect the last time the connection sent data. If the limit is exceeded, the connection will be cut off.
The connection cut off by heartbeat detection will still trigger the onClose event callback
The test found that there is a chance that the connection will be closed when there is a heartbeat setting
problem causes
The investigation found that when nginx sets the reverse proxy, keepalive_timeout = 60 is set (if no data is received within 60 seconds, it will actively disconnect. If there is data transmission within 60 seconds , the timing will be reset after the last data transmission is completed. time ) and proxy_read_timeout = 60 (after the connection is successful _ waiting for the back-end server response time _ has actually entered the back-end queue for processing, which can also be said to be the time for the back-end server to process the request), the same as the heartbeat detection time, there are probability of close
In addition, proxy_send_timeout also needs attention (the back-end server data return time _ means that the back-end server must transmit all data within the specified time)
solution