Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Swoole server configuration options


May 14, 2021 Swoole



In swoole, a related property swoole_server a file can pass

$serv->set( $array configs );

functions to configure, and these configuration options make swoole more flexible. Example:

$serv = new swoole_server("0.0.0.0", 9501);
$serv->set(array(
    'worker_num' => 8,
    'max_request' => 10000,
    'max_conn' => 100000,
    'dispatch_mode' => 2,
    'debug_mode'=> 1
    'daemonize' => false,
));

The configuration options and related descriptions are as follows:

1.worker_num

Description: Specifies the number of worker processes that are started.
Description: swoole is the master-gt; n-worker mode, the more worker processes are turned on, the greater the server load capacity, but the corresponding server will have more memory. A t the same time, when there are too many worker processes, the overhead of switching between processes is even greater. Therefore, it is recommended to turn on 1-4 times the number of worker processes with cpu cores.
Example:

'worker_num' => 8

2.max_request

Description: The maximum number of tasks allowed to be processed per worker process.
Description: When this value is set, each worker process automatically restarts max_request processing the first request. The primary purpose of setting this value is to prevent memory overflows that can result from the worker process processing a large number of requests.
Example:

'max_request' => 10000

3.max_conn

Description: The maximum number of TCP connections allowed to be maintained by the server
Description: When this parameter is set, new connections are rejected when the number of connections already on the server reaches this value. In addition, the value of this parameter must not exceed the value of operating system ulimit -n, and this value should not be set too large, because swoole_server will request a large chunk of memory at once to hold the information for each connection.
Example:

'max_conn' => 10000

4.ipc_mode

Description: Set how processes communicate with each other.
Description: There are three modes of communication, the parameters are as follows:

  • 1
  • 2
  • 3 . . . .

Example:

'ipc_mode' => 1

5.dispatch_mode

Description: Specify a packet distribution policy.
Description: There are three modes with the following parameters:

  • 1 . . . . round robin mode, receiving a round robin assigned to each worker process
  • 2 fixed mode, assigning the worker according to the connected file descriptor. This ensures that data from the same connection is processed only by the same worker
  • In preeptive mode, the main process selects the delivery based on the worker's free and busy state and only delivers it to the idle worker

Example:

'dispatch_mode' => 2

6.task_worker_num

Description: The number of task processes turned on by the server.
Description: When this parameter is set, the server turns on the asynchronous task function. You can now use the task method to post asynchronous tasks.

After you set this parameter, you must swoole_server onTask/onFinish callback functions for the user, or the startup server will report an error.

Example:

'task_worker_num' => 8

7.task_max_request

Description: The maximum number of tasks allowed to be processed per task.
Description: Refer to max_request task_worker_num
Example:

'task_max_request' => 10000

8.task_ipc_mode

Description: Set how the task process communicates with the worker process.
Description: Refer to ipc_mode
Example:

'task_ipc_mode' => 2

9.daemonize

Description: The setup program enters the background and runs as a daemon.
Description: This must be enabled for long-running server-side programs. I f the daemon is not enabled, the program will be terminated when the ssh terminal exits. When daemon is enabled, standard inputs and outputs are redirected to log_file, and if log_file is not set, all outputs are discarded.
Example:

'daemonize' => 0

10.log_file

Description: Specify the log file path
Description: Exception information that occurs during the swoole run is recorded in this file. T he default is printed to the screen. Note log_file does not automatically slice a file, so you need to clean it up regularly.
Example:

'log_file' => '/data/log/swoole.log'

11.heartbeat_check_interval

Description: Set the heartbeat detection interval
Description: This option represents how often round, in seconds. Traversing all connections with each detection, forcing a connection to close if no data is sent during the interval (there will be an onClose callback).
Example:

'heartbeat_check_interval' => 60

12.heartbeat_idle_time

Description: Set the maximum idle time allowed for a connection.
Description: This parameter is used in conjunction heartbeat_check_interval the data. E ach time you traverse all connections, force the connection to close if heartbeat_idle_time no data is sent within the heartbeat_idle_time time. The default setting heartbeat_check_interval is 2.
Example:

'heartbeat_idle_time' => 600

13.open_eof_check

Description: Turn on the eof detection function
Description: Use package_eof with the . T his option detects data from client connections and delivers packets to the Worker process when the packet ends with the specified package_eof string, otherwise the packets are stitched until the cache overflows or timeouts. In the event of an error, the connection is determined to be malicious, and packets are dropped and forced to close the connection.

EOF detection does not look for eof strings from the middle of the data, so the Worker process may receive multiple packets at the same time and need to split the packets itself $data in the application layer code

Example:

'open_eof_check' => true

14.package_eof

Description: Set the EOF string
Description: package_eof allows only 8 bytes of strings to be passed in
Example:

'package_eof ' => '/r/n'

15.open_length_check

Description: Open package length detection
Description: The packet length detection provides the resolution of the fixed package head and package body this format protocol. When enabled, you can guarantee that the Worker process, onReceive, will receive a complete packet each time.
Example:

'open_length_check' => true

16.package_length_offset

Description: The first few bytes in the header begin to hold the length field
Description: Use with open_length_check to indicate the location of the length field.
Example:

'package_length_offset' => 5

17.package_body_offset

Description: The length is calculated from the first few bytes.
Description: Use with open_length_check to indicate the length of the head.
Example:

'package_body_offset' => 10

18.package_length_type

Description: Specifies the type of package length field
Description: Use with open_length_check, specify the type of length field, and the parameters are as follows:

  • 's' is used to int16_t order of machine bytes
  • 'S' is used uint16_t machine byte order
  • The 'n' is uint16_t the large-end byte sequence
  • The 'N' is uint32_t large-end byte order
  • 'L' is used uint32_t machine byte order
  • 'l' is the order of the machine bytes

Example:

'package_length_type' => 'N'

19.package_max_length

Description: Set the maximum packet size
Description: This value determines the size of the packet cache. I f the cached data exceeds this value, an error is thrown. Specific error handling is determined by the type of protocol resolution that is turned on.
Example:

'package_max_length' => 8192

20.open_cpu_affinity

Description: Enable CPU affinity settings
Description: In a multi-core hardware platform, enabling this feature binds the swoole's actor thread/worker process to a fixed core. You can avoid the runtime of a process/thread switching between multiple cores and increase the hit rate of CPU Cache.
Example:

'open_cpu_affinity' => true

21.open_tcp_nodelay

Description: Enable open_tcp_nodelay
Description: When the TCP connection is turned on, the Nagle merge algorithm is not turned off and immediately sent to the client connection. In some scenarios, such as http servers, you can improve responsiveness.
Example:

'open_tcp_nodelay' => true

22.tcp_defer_accept

Description: Enable tcp_defer_accept features
Description: After startup, accept is triggered only when a TCP connection has data to send.
Example:

'tcp_defer_accept' => true

23.ssl_cert_file and ssl_key_file

Description: Set up SSL tunnel encryption
Description: Set the value to a string of file names, specifying the path to the cert certificate and key.
Example:

'ssl_cert_file' => '/config/ssl.crt',
'ssl_key_file' => '/config//ssl.key',

24.open_tcp_keepalive

Description: Turn on the options for KEEP_ALIVE TCP
Description: Use TCP's built-in keep_alive properties to ensure that connections are not closed because they are idle for long hours.
Example:

'open_tcp_keepalive' => true

25.tcp_keepidle

Description: Specify the probe interval.
Description: With open_tcp_keepalive, probe if a connection does tcp_keepidle data within the data source.
Example:

'tcp_keepidle' => 600

26.tcp_keepinterval

Description: Specifies the packet interval at the time of the probe
Description: Use with open_tcp_keepalive system
Example:

'tcp_keepinterval' => 60

27.tcp_keepcount

Description: Specifies the number of attempts to probe
Description: With open_tcp_keepalive, if tcp_keepcount is still unsys response after two attempts, the connection is determined to be closed.
Example:

'tcp_keepcount' => 5

28.backlog

Description: Specifies the length of the Listen queue
Description: This parameter will determine how many connections are waiting for acept at the same time.
Example:

'backlog' => 128

29.reactor_num

Description: Specifies the number of Actor threads
Description: Set the number of event processing threads in the main process, the default will enable the same number of CPU cores, generally set to 1-4 times the CPU core, the maximum number of CPU cores.
Example:

'reactor_num' => 8

30.task_tmpdir

Description: Set up a temporary directory of data for task
Description: In swoole_server, if more than 8192 bytes of data is posted, temporary files are enabled to hold the data. The task_tmpdir here is to set the location where temporary files are saved.

Swoole-1.7.7 plus is required

Example:

'task_tmpdir' => '/tmp/task/'

Description

In addition to the 29 options above, there are a few that are not commonly used or that have been discarded. In the future, if there is actual demand, it will be added.