open-smf / connection-pool Goto Github PK
View Code? Open in Web Editor NEWA common connection pool based on Swoole is usually used as a database connection pool.
License: MIT License
A common connection pool based on Swoole is usually used as a database connection pool.
License: MIT License
目前看例子里的 Usage in Swoole Server
中,是通过绑定事件
$this->swoole->on('WorkerStart', $createPools);
$this->swoole->on('WorkerStop', $closePools);
$this->swoole->on('WorkerError', $closePools);
似乎自定义进程没有对应的事件。
通过 addProcess() 方法启动的自定义进程(常驻),如何使用连接池呢?
感谢
Hi !
Can somebody show me how to make a Connection Pool implementation that connects to PhpRedis Cluster ?
Let's say I have these Redis Cluster nodes:
192.168.10.11, 192.168.10.12 and 192.168.10.13.
192.168.10.11 is the master
I am using Swoole 5.0.1
Do I need to call \Swoole::enableCoroutine(); ?
Thanks !
On Line number 119 of "ConnectionPool.php" that lives in path "connection-pool/src/"
You return a new connection without pushing it into empty channel as below:
return $this->createConnection();
How about pushing this the new connection into channel (Pool) first, and then pop the connection, like below;
$connection = $this->createConnection();`
$ret = $this->pool->push($connection, static::CHANNEL_TIMEOUT);
if ($ret === false) {
$this->removeConnection($connection);
}
.. instead of returning the connection right away ?
Recommended Code with its full context, as below:
if ($this->pool->isEmpty()) {
// Create more connections
if ($this->connectionCount < $this->maxActive) {
$connection = $this->createConnection(); `
// Lets push new connection to the channel as below, because channel is initialized and still have space as condition ($this->connectionCount < $this->maxActive) has evaluated to true
$ret = $this->pool->push($connection, static::CHANNEL_TIMEOUT);
if ($ret === false) {
$this->removeConnection($connection);
}
}
}
$connection = $this->pool->pop($this->maxWaitTime);
你好
我目前有在使用slim4配上swoole 當作我的開發工具
近期想要implement db pool的部分
非常喜歡你寫的方式
想請問你 有沒有什麼方法將pool 的instance 靜態化
來去讓其他class 使用
因為原本slim4的db是屬於依賴注入的方式
但身為小白的我卻不知道該如何下手
是否能加上 ORM
Hi, we hosted a bunches of Wordpress site and I want to lessen the load of our DB server. Is it possible to use this package as standalone similar to https://smproxy.louislivi.com/#/en/
. And is it possible to separate the pool for read and write? If possible, kindly give a sample how to do that.
I am newbie regarding pool connection so bear with me. Thank you in advance.
If some error/exceptions occurs in borrow() -> createConnection(), variable $connectionCount
is incremented but no connection exist (can't be returned to pool), next borrow connection is trying to pop connection from channel but there is no connection ...
I would suggest same fix as in this forked lib, can create MR: eltaline@e5f8b2d
Q: Assume from code, there is no way init (or create connection) again when is closed ?!
Can this connection pool handle re-connecting
Ex:
- Connection to the mysql server established
- Commands can be sent correctly
- Network cable is unplugged/disconnected.
- Commands try to send, detect that the network disconnected, and tries to reconnect.
- Network cable is plugged back in/network goes up
- Pool eventually reconnects, and resumes operations correctly.
[2019-05-27 22:06:12 *28.7] ERROR zm_deactivate_swoole (ERROR 503): Fatal error: Uncaught Error: Call to undefined method stdClass::close() in /app/vendor/open-smf/connection-pool/src/Connectors/PhpRedisConnector.php:32
Stack trace:
#0 /app/vendor/open-smf/connection-pool/src/ConnectionPool.php(206): Smf\ConnectionPool\Connectors\PhpRedisConnector->disconnect(Object(stdClass))
#1 {main}
thrown in /app/vendor/open-smf/connection-pool/src/Connectors/PhpRedisConnector.php on line 32.
hi,
could this be made to work with openswoole (22.1.2)?
Thx.
#0 [8192]ErrorException in ConnectionPool.php line 264
Creation of dynamic property think\cache\driver\Redis::$__lat is deprecated
}
}
});
}
protected function createConnection()
{
$this->connectionCount++;
$connection = $this->connector->connect($this->connectionConfig);
$connection->{static::KEY_LAST_ACTIVE_TIME} = time();
return $connection;
}
protected function removeConnection($connection)
{
$this->connectionCount--;
Coroutine::create(function () use ($connection) {
try {
$this->connector->disconnect($connection);
PHP 8.2弃用了动态声明的类属性
:[Uncaught RuntimeException: Failed to connect the requested database: [2002] SQLSTATE[HY000] [2002] Operation timed out in //Connectors/PDOConnector.php:12
swoole 版本 4.5.9
测试工具ab 并发6000
不是数据库和swoole版本问题,用easyswoole测试没问题
环境
php 8.0.1
swoole 4.5.9
经过调试,貌似是Smf\ConnectionPool\ConnectionPool->init()里面go函数的问题
更改成这样可以响应:
public function init(): bool
{
if ($this->initialized) {
return false;
}
$this->initialized = true;
$this->pool = new Channel($this->maxActive);
$this->balancerTimerId = $this->startBalanceTimer($this->idleCheckInterval);
for ($i = 0; $i < $this->minActive; $i++) {
$connection = $this->createConnection();
$ret = $this->pool->push($connection, static::CHANNEL_TIMEOUT);
if ($ret === false) {
$this->removeConnection($connection);
}
}
return true;
}
在运行的过程中有时候会报错
PHP Fatal error: Swoole\Coroutine\Redis::__construct(): constructor can only be called once in /xxxx/swoole_api/vendor/open-smf/connection-pool/src/Connectors/CoroutineRedisConnector.php on line 11
swoole文档新版本中,Redis 客户端,MySQL 客户端,不推荐再继续使用,可以使用 PHP 原生的函数 + 一键协程化。
可以在原先协程客户端的方式,增加使用原生Redis,Mysql配置吗
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.