当前位置:网站首页>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!!!
边栏推荐
- 记录一下刷LeetCode瞬间有思路的一道简单题——剑指 Offer 09. 用两个栈实现队列
- Apache doris1.0 cluster setup, load balancing and parameter tuning
- Wireshark对IMAP抓包分析
- UE4 learning record 2 adding skeleton, skin and motion animation to characters
- Analysis on resource leakage /goroutine leakage / memory leakage /cpu full in go
- DPVS-FullNAT模式部署篇
- Px4 multi computer simulation (gazebo)
- php中使用Makefile编译protobuf协议文件
- 库项目和App项目中清单文件的包名不要相同
- 流数据
猜你喜欢

流数据

idea 查看单元测试覆盖率

Hbuilderx uses the gaude map to obtain the current location

解析产品开发失败的5个根本原因

Line height for small use

Reprint: detailed explanation of qtablewidget (style, right-click menu, header collapse, multiple selection, etc.)

如何设计产品的roadmap?

QT custom implemented calendar control

iomanip头文件在实战中的作用

先序线索二叉树
随机推荐
idea Kotlin版本升级
Wireshark对IMAP抓包分析
聊聊swoole或者php cli 进程如何热重启
CSDN force value
Screen recording to GIF is an easy-to-use gadget, screentogif, which is free and easy to use!
Run the dronekit flight control application on Shumei faction under px4-jmavsim software simulation environment
SSL/TLS、对称加密和非对称加密和TLSv1.3
Hibernate entity class curd, transaction operation summary
Use Baidu map API to set an overlay (infowindow) in the map to customize the window content
史上最简单的录屏转gif小工具LICEcap,要求不高可以试试
MySQL自定义函数实例
Analysis on resource leakage /goroutine leakage / memory leakage /cpu full in go
如何设计产品的roadmap?
php进程间传递文件描述符
IDEA中如何生成get/set方法
OpenResty篇01-入门简介和安装配置
今天说说String相关知识点
Binary, hexadecimal, big end and small end
DPVS-FullNAT模式管理篇
Share a downloaded osgeo4w64 Library Based on qgis3.10