# swoole_server->set
swoole_server->set函数用于设置swoole_server运行时的各项参数。服务器启动后通过$serv->setting来访问set函数设置的参数数组。
原型:
~~~
void swoole_server->set(array $setting);
void swoole_server_set(swoole_server $server, array $setting);
~~~
示例:
~~~
$serv->set(array(
'timeout' => 2.5, //select and epoll_wait timeout.
'poll_thread_num' => 2, //reactor thread num
'writer_num' => 2, //writer thread num
'worker_num' => 4, //worker process num
'backlog' => 128, //listen backlog
'max_request' => 50,
'dispatch_mode'=>1,
));
~~~
> swoole_server::set只能在swoole_server::start前调用
### 最大连接
max_conn => 10000, 此参数用来设置Server最大允许维持多少个tcp连接。超过此数量后,新进入的连接将被拒绝。
> 此参数不要调整的过大,根据机器内存的实际情况来设置。Swoole会根据此数值一次性分配一块大内存来保存Connection信息
### 守护进程化
daemonize => 1,加入此参数后,执行php server.php将转入后台作为守护进程运行
### reactor线程数
reactor_num => 2,通过此参数来调节poll线程的数量,以充分利用多核
> reactor_num和writer_num默认设置为CPU核数
1.6.9之前版本请使用 poll_thread_num => 2来设置
### Writer线程数
writer_num => 2,通过此参数来调节Write线程的数量,以充分利用多核。在Swoole里对SOCKET的读写是分开的,IO_Read在reactor线程中完成,IO_Write在writer线程中完成
> 1.6.12+撤销了Writer线程,与Reactor线程做了合并
### worker进程数
worker_num => 4,设置启动的worker进程数量。swoole采用固定worker进程的模式。
PHP代码中是全异步非阻塞,worker_num配置为CPU核数的1-4倍即可。如果是同步阻塞,worker_num配置为100或者更高,具体要看每次请求处理的耗时和操作系统负载状况。
> 当设定的worker进程数小于reactor线程数时,会自动调低reactor线程的数量
### max_request
max_request => 2000,此参数表示worker进程在处理完n次请求后结束运行。manager会重新创建一个worker进程。此选项用来防止worker进程内存溢出。
> PHP代码也可以使用memory_get_usage来检测进程的内存占用情况,发现接近memory_limit时,调用exit()退出进程。manager进程会回收此进程,然后重新启动一个新的Worker进程。
onConnect/onClose不增加计数
~~~
max_request => 0
~~~
设置为0表示不自动重启。在Worker进程中需要保存连接信息的服务,需要设置为0.
### Listen队列长度
backlog => 128,此参数将决定最多同时有多少个待accept的连接,swoole本身accept效率是很高的,基本上不会出现大量排队情况。
### CPU亲和设置
open_cpu_affinity => 1 ,启用CPU亲和设置
### TCP_NoDelay启用
open_tcp_nodelay => 1 ,启用tcp_nodelay
### TCP_DEFER_ACCEPT
tcp_defer_accept => 5,此参数设定一个秒数,当客户端连接连接到服务器时,在约定秒数内并不会触发accept,直到有数据发送,或者超时时才会触发。
### 日志文件路径
log_file => '/data/log/swoole.log', 指定swoole错误日志文件。在swoole运行期发生的异常信息会记录到这个文件中。默认会打印到屏幕。
> 此配置仅在swoole-1.5.8以上版本中可用
### 数据buffer
buffer主要是用于检测数据是否完整,如果不完整swoole会继续等待新的数据到来。直到收到完整的一个请求,才会一次性发送给worker进程。这时onReceive会收到一个超过SW_BUFFER_SIZE,
小于$serv->setting['package_max_length']的数据。
目前仅提供了EOF检测、固定包头长度检测2种buffer模式。
open_eof_check => true打开buffer
package_eof => "\r\n\r\n" 设置EOF
### 心跳检测机制
heartbeat_check_interval => 30 //每隔多少秒检测一次,单位秒,Swoole会轮询所有TCP连接,将超过心跳时间的连接关闭掉
heartbeat_idle_time => 60 //TCP连接的最大闲置时间,单位s , 如果某fd最后一次发包距离现在的时间超过heartbeat_idle_time会把这个连接关闭。
> 心跳检测特性需要swoole-1.6.10+
heartbeat_idle_time必须大于或等于heartbeat_check_interval
### worker进程数据包分配模式
dispatch_mode = 1 //1平均分配,2按FD取摸固定分配,3抢占式分配,默认为取摸(dispatch=2)
> 抢占式分配,每次都是空闲的worker进程获得数据。很合适SOA/RPC类的内部服务框架
当选择为dispatch=3抢占模式时,worker进程内发生onConnect/onReceive/onClose/onTimer会将worker进程标记为忙,不再接受新的请求。reactor会将新请求投递给其他状态为闲的worker进程
如果希望每个连接的数据分配给固定的worker进程,dispatch_mode需要设置为2
| Swoole流程 |
|-----|

- Swoole
- 入门指引
- 环境依赖
- 编译安装
- 编译参数
- 常见错误
- 版本更新记录
- 1.7.12
- 1.7.13
- 1.7.14
- 1.7.15
- 1.7.16
- 1.7.17
- 开发者列表
- 新特性使用
- 1.7.16 使用迭代器遍历Server所有连接
- 1.7.5 在Server中使用swoole_table
- 1.7.5 swoole_client支持sendfile接口
- 1.7.4 SSL隧道加密TCP-Server
- 1.7.4 task进程中使用毫秒定时器
- 1.7.3 固定包头+包体协议自动分包
- 1.7.3 onTask直接return取代finish函数
- 1.7.2 swoole_process多进程模块的使用
- 1.7.2 task进程使用消息队列
- 项目路线图
- 提交错误报告
- 内核参数调整
- 周边相关项目
- 常见问题
- 升级swoole版本的常见问题
- 生成可分发的二进制swoole版本
- Connection refused是怎么回事
- Resource temporarily unavailable [11]
- Cannot assign requested address [99]
- pcre.h: No such file or directory
- 在phpinfo中有在php-m中没有
- swoole与node.js相比有哪些优势
- swoole与golang相比有哪些优势
- my_global.h: No such file or directory
- Server
- 函数列表
- swoole_server::__construct
- swoole_server::set
- swoole_server::on
- swoole_server::addListener
- swoole_server::addProcess
- swoole_server::listen
- swoole_server::handler
- swoole_server::start
- swoole_server::reload
- swoole_server::shutdown
- swoole_server::addtimer
- swoole_server::deltimer
- swoole_server::tick
- swoole_server::after
- swoole_server::clearTimer
- swoole_server::close
- swoole_server::send
- swoole_server::sendfile
- swoole_server::sendto
- swoole_server::sendwait
- swoole_server::sendMessage
- swoole_server::connection_info
- swoole_server::connection_list
- swoole_server::bind
- swoole_server::stats
- swoole_server::task
- swoole_server::taskwait
- swoole_server::finish
- swoole_server::heartbeat
- swoole_get_mysqli_sock
- swoole_set_process_name
- swoole_version
- swoole_strerror
- swoole_errno
- swoole_get_local_ip
- 属性列表
- swoole_server::$setting
- swoole_server::$master_pid
- swoole_server::$manager_pid
- swoole_server::$worker_id
- swoole_server::$worker_pid
- swoole_server::$taskworker
- swoole_server::$connections
- 配置选项
- reactor_num
- worker_num
- max_request
- max_conn (max_connection)
- task_worker_num
- task_ipc_mode
- task_max_request
- task_tmpdir
- dispatch_mode
- message_queue_key
- daemonize
- backlog
- log_file
- heartbeat_check_interval
- heartbeat_idle_time
- open_eof_check
- open_eof_split
- package_eof
- open_length_check
- package_max_length
- open_cpu_affinity
- cpu_affinity_ignore
- open_tcp_nodelay
- tcp_defer_accept
- ssl_cert_file
- user
- group
- chroot
- 事件回调函数
- onStart
- onShutdown
- onWorkerStart
- onWorkerStop
- onTimer
- onConnect
- onReceive
- onClose
- onTask
- onFinish
- onPipeMessage
- onWorkerError
- onManagerStart
- onManagerStop
- 高级特性
- 改变Worker进程的用户/组
- 回调函数中的from_id和fd
- Buffer和EOF_Check的使用
- Worker与Reactor通信模式
- TCP-Keepalive死连接检测
- TCP服务器心跳维持方案
- 多端口监听的使用
- 捕获Server运行期致命错误
- swoole_server的3种运行模式介绍
- swoole_server中对象的4层生命周期
- 在worker进程内监听一个Server端口
- 常见问题
- 为什么不要send完后立即close
- 如何在回调函数中访问外部的变量
- swoole_server中内存管理机制
- 是否可以共用1个redis或mysql连接
- 关于onConnect/onReceive/onClose顺序
- 压力测试
- Nginx/Golang/Swoole/Node.js的性能对比
- 并发10万TCP连接的测试
- 预定义常量
- php.ini选项
- Client
- 方法列表
- swoole_client::__construct
- swoole_client::set
- swoole_client::on
- swoole_client::connect
- swoole_client::isConnected
- swoole_client::getsockname
- swoole_client::getpeername
- swoole_client::send
- swoole_client::sendto
- swoole_client::sendfile
- swoole_client::recv
- swoole_client::close
- 属性列表
- swoole_client::$errCode
- swoole_client::$sock
- 并行
- swoole_client_select
- TCP客户端异步连接
- SWOOLE_KEEP建立TCP长连接
- Process
- 方法列表
- swoole_process::__construct
- swoole_process::start
- swoole_process::name
- swoole_process::exec
- swoole_process::write
- swoole_process::read
- swoole_process::useQueue
- swoole_process::push
- swoole_process::pop
- swoole_process::close
- swoole_process::exit
- swoole_process::kill
- swoole_process::wait
- swoole_process::daemon
- swoole_process::signal
- AsyncIO
- 异步文件系统IO
- swoole_async_readfile
- swoole_async_writefile
- swoole_async_read
- swoole_async_write
- swoole_async_dns_lookup
- EventLoop
- swoole_event_add
- swoole_event_set
- swoole_event_del
- swoole_event_exit
- swoole_event_wait
- swoole_event_write
- 异步毫秒定时器
- swoole_timer_add
- swoole_timer_del
- swoole_timer_tick
- swoole_timer_after
- swoole_timer_clear
- Memory
- Lock
- swoole_lock::__construct
- swoole_lock::lock
- swoole_lock::trylock
- swoole_lock::unlock
- swoole_lock::lock_read
- swoole_lock::trylock_read
- Buffer
- swoole_buffer::__construct
- swoole_buffer::append
- swoole_buffer::substr
- swoole_buffer::clear
- swoole_buffer::expand
- swoole_buffer::write
- Table
- swoole_table::__construct
- swoole_table::column
- swoole_table::create
- swoole_table::set
- swoole_table::incr
- swoole_table::decr
- swoole_table::get
- swoole_table::del
- swoole_table::lock
- swoole_table::unlock
- 常量列表
- HttpServer
- swoole_http_server
- swoole_http_server::on
- swoole_http_server::start
- swoole_http_server::setGlobal
- swoole_http_request
- swoole_http_request::$header
- swoole_http_request::$server
- swoole_http_request::$get
- swoole_http_request::$post
- swoole_http_request::$cookie
- swoole_http_request::$files
- swoole_http_request::rawContent
- swoole_http_response
- swoole_http_response::header
- swoole_http_response::cookie
- swoole_http_response::status
- swoole_http_response::gzip
- swoole_http_response::write
- swoole_http_response::end
- 常见问题
- CURL发送POST请求服务器端超时
- WebSocket
- 回调函数
- onHandShake
- onOpen
- onMessage
- 函数列表
- swoole_websocket_server::push
- 预定义常量
- 高级
- Swoole的实现
- Reactor线程
- Manager进程
- Worker进程
- Reactor、Worker、Task的关系
- Task/Finish特性的用途
- C/C++开发者如何使用Swoole
- 在php-fpm或apache中使用swoole在php-fpm或apache中使用swoole
- Swoole异步与同步的选择
- TCP/UDP压测工具
- swoole服务器如何做到无人值守100
- MySQL的连接池、异步、断线重连
- 其他
- Swoole社区
- 加入Swoole开发组
- 附录:Linux信号列表
- 附录:Linux错误信息(errno)列表
- 附录:TCP连接的状态
- 附录:tcpdump抓包工具的使用
- 附录:strace工具的使用
- 附录:编译PHP扩展的相关工具
- 备用:已移除的历史特性
- onMasterConnect
- onMasterClose
- 历史:版本更新记录
- v1.5
- v1.6
- v1.7
- v1.7.5
- v1.7.6
- v1.7.7
- v1.7.8
- v1.7.9
- v1.7.10
- v1.7.11
- Swoole-framework
- 开发指南
- 安装Swoole框架和扩展
- 数据库Model类
- model::get
- model::set
- model::del
- model::put
- model::gets
- model::sets
- model::dels
- model::all
- model::count
- model::exists
- model($model_name)
- table($table_name)
- 数据库ORM接口
- Socket网络开发
- TCP服务器
- Web服务器
- WebSocket
- Nginx+Swoole服务器配置
- Apache+Swoole服务器配置
- 控制器Controller
- 命名空间
- 文件上传组件
- Redis
- Database
- Swoole\Database::insert
- 框架规范
- 目录规范
- 自定义路由
- URL映射规则
- 示例程序
- 服务器端程序(Server)
- http_server
- app_server
- soa_server
- websocket_server
- comet_server
- 配置文件
