当前位置:网站首页>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!!!
边栏推荐
- Solve 'tuple' object has no attribute 'lower‘
- Binary, hexadecimal, big end and small end
- 4个要点,助力产品管好项目
- 51 single chip microcomputer, some registers, some knowledge points
- Reproduction of an implant found by Kaspersky that writes shellcode into evenlog
- util. Collection and encapsulation of JS tool functions
- 森林的先序和中序遍历
- line-height小用
- Apache doris1.0 cluster setup, load balancing and parameter tuning
- 1.8 billion pixel Mars panorama Ultra HD released by NASA, very shocking
猜你喜欢
随机推荐
CSDN force value
DPVS-FullNAT模式部署篇
Hbuilderx uses the gaude map to obtain the current location
WordPress
录屏转gif的好用小工具ScreenToGif,免费又好用!
proxy
格式化编号,不够位数的补0,例如1:0001,25:0025
记录一下刷LeetCode瞬间有思路的一道简单题——剑指 Offer 09. 用两个栈实现队列
森林的先序和中序遍历
IDEA中如何生成get/set方法
C1. k-LCM (easy version)-Codeforces Round #708 (Div. 2)
Qtcreator formatting code
excel如何实现中文单词自动翻译成英文?这个公式教你了
Mutual conversion of float type data and datetime type data in sqlserver2008
1.8 billion pixel Mars panorama Ultra HD released by NASA, very shocking
The simplest screen recording to GIF gadget in history, licecap, can be tried if the requirements are not high
第五章 习题(124、678、15、19、22)【微机原理】【习题】
Download the latest V80 version of Google Chrome
Analysis and comprehensive summary of full type equivalent judgment in go
Share a downloaded osgeo4w64 Library Based on qgis3.10









