当前位置:网站首页>On the quantity control mechanism of swoole collaboration creation in production environment
On the quantity control mechanism of swoole collaboration creation in production environment
2022-06-25 23:48:00 【ndrandy】
stay swoole In official documents , It is mentioned that “ Co process overhead ”. A simple reference is as follows :
- stay
PHP-7.2The bottom layer of the version will be assigned8KOfstackTo store the variables of the coroutine ,zvalThe size is16 byte, therefore8KOfstackIt can be saved up to512A variable . The memory consumption of the coroutine stack exceeds8KafterZendVMIt will automatically expand .
PHP-7.1、PHP-7.0It will be assigned by default256KStack memory
This article takes php7.2 For example , Because the author currently uses php The version is also 7.2.
In order to verify php7.2 in , Whether to create a collaboration by default 8K, You can write a demo Just test it
<?php
$begin_memory = memory_get_usage();
go(function () {
co::sleep(10);
echo co::getCid() . PHP_EOL;
});
var_dump(memory_get_usage() - $begin_memory); // Output int(8640), The unit is byte, Namely 8K about .Now that you create a collaboration , Default assignment 8K Memory , Suppose that the code we execute in the coroutine uses stack memory , No more than 8K, that 1G About how many coroutines can be created in the memory of ?1G= 1024MB = (1024 * 1024)KB, max_coroutine_num = 1024 * 1024 / 8 = 130072. in other words 1G Memory can create 10w+ The number of trips . Considerable . But the reality , Stack memory may exceed .
I currently have a business scenario , from redis In line pop data , Then open the process to write mysql, We know mysql Your writing speed lags behind redis A lot , When the business peak ,redis When the amount of queue data increases dramatically ,mysql After reaching the bottleneck , The number of collaborative processes will be overstocked . The peak period lasts to a certain extent , In the end, there will be insufficient memory resources , As a result, a new collaboration cannot be created , At this time swoole Will trigger :PHP Warning: PHP Warning: go(): exceed max number of coroutine ...
So here's an experience : In the production and consumption model , The best of swoole The quantity of collaboration process creation is controlled
swoole Co process control scheme :
1、 rely on swoole At the bottom max_coroutine To configure . Give an example demo
<?php
set_error_handler(function () {
throw new Exception("error happen\n", 500);
});
co::set(['max_coroutine' => 1]);
try {
for ($i = 0; $i < 2; $i++) {
go(function () {
co::sleep(10);
});
}
} catch (\Throwable $e) {
if ($e->getCode() == 500) {
echo " The number of processes exceeds the limit !" . $e->getMessage();
}
}2、 Create your own Context Manage and count the number of collaboration processes currently being executed , More than the specified amount , Throw an exception or customize the handling
<?php
define("APP_MAX_COROUTINE", 10);
$co_ctx = [];
go(function () use ($co_ctx) {
if (count($co_ctx) >= APP_MAX_COROUTINE) {
// Limit exceeded , sign out
return;
}
$cid = co::getCid();
defer(function () use ($cid, $co_ctx) {
if (isset($co_ctx[$cid])) {
unset($co_ctx[$cid]);
}
});
$co_ctx[$cid] = $cid;
//begin business ...
});
End of discussion , What's wrong , Your smile ! thks!!!
边栏推荐
- Qtcreator formatting code
- C. Fibonacci Words-April Fools Day Contest 2021
- 谈一谈PHP变量或参数的Copy On Write机制
- Hibernate architecture introduction and environment construction (very detailed)
- 流数据
- Apache Doris1.0版本集群搭建、负载均衡与参数调优
- Gradle的环境安装与配置
- YUV444、YUV422、YUV420、YUV420P、YUV420SP、YV12、YU12、NV12、NV21
- How does excel translate Chinese words into English automatically? This formula teaches you
- MySQL InnoDB lock knowledge points
猜你喜欢

Extraction system apk

一文讲透研发效能!您关心的问题都在

如何进行流程创新,以最经济的方式提升产品体验?

第六章 习题(678)【微机原理】【习题】

第五章 习题(124、678、15、19、22)【微机原理】【习题】

实例:用C#.NET手把手教你做微信公众号开发(21)--使用微信支付线上收款:H5方式

YUV444、YUV422、YUV420、YUV420P、YUV420SP、YV12、YU12、NV12、NV21

1.8 billion pixel Mars panorama Ultra HD released by NASA, very shocking

SSM整合学习笔记(主要是思路)

Style setting when there is a separator in the qcombobox drop-down menu
随机推荐
One article explains R & D efficiency! Your concerns are
聊聊swoole或者php cli 进程如何热重启
SSM整合学习笔记(主要是思路)
Hbuilderx uses the gaude map to obtain the current location
Jenkins 发布PHP项目代码
解析产品开发失败的5个根本原因
The package name of the manifest file in the library project and the app project are not the same
phoenix索引
Bi-sql stored procedure (I)
1.8 billion pixel Mars panorama Ultra HD released by NASA, very shocking
B. Box Fitting-CodeCraft-21 and Codeforces Round #711 (Div. 2)
IDEA中如何生成get/set方法
Qlabel text scrolling horizontally
Uni app -- listen for the exit of the return key
Screen recording to GIF is an easy-to-use gadget, screentogif, which is free and easy to use!
Hibernate architecture introduction and environment construction (very detailed)
213.打家劫舍 II
MySQL InnoDB lock knowledge points
记录一下刷LeetCode瞬间有思路的一道简单题——剑指 Offer 09. 用两个栈实现队列
Hibernate core api/ configuration file / L1 cache details