当前位置:网站首页>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!!!
边栏推荐
猜你喜欢
随机推荐
1.8 billion pixel Mars panorama Ultra HD released by NASA, very shocking
php性能优化
权限设计=功能权限+数据权限
mysql5.7版本在配置文件my.ini[mysqld]加上skip-grant-tables后无法启动
The package name of the manifest file in the library project and the app project are not the same
Uniapp - call payment function: Alipay
DPVS-FullNAT模式管理篇
idea 查看单元测试覆盖率
第五章 习题(124、678、15、19、22)【微机原理】【习题】
IDEA中如何生成get/set方法
line-height小用
UE4 learning record 2 adding skeleton, skin and motion animation to characters
解析产品开发失败的5个根本原因
期末复习【机器学习】
SVN
Go language escape analysis complete record
Ad20 learning notes II
平衡二叉树AVL
Unable to start debugging. Unexpected GDB output from command “-environment -cd xxx“ No such file or
Kotlin null pointer bug









