Skip to content

Commit

Permalink
http response 分段发送
Browse files Browse the repository at this point in the history
改进db cache连接池
  • Loading branch information
yunwuxin committed Jan 14, 2020
1 parent 321a1b7 commit 300242e
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 50 deletions.
1 change: 0 additions & 1 deletion src/Sandbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ public function clear($snapshot = true)

Context::clear();
$this->setInstance($this->getBaseApp());
gc_collect_cycles();
}

public function getApplication()
Expand Down
18 changes: 17 additions & 1 deletion src/concerns/InteractsWithHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,22 @@ protected function sendResponse(Response $res, \think\Response $response, Cookie

$content = $response->getContent();

$res->end($content);
$this->sendByChunk($res, $content);
}

protected function sendByChunk(Response $res, $content)
{
$chunkSize = 8192;

if (strlen($content) <= $chunkSize) {
$res->end($content);
return;
}

foreach (str_split($content, $chunkSize) as $chunk) {

This comment has been minimized.

Copy link
@NHZEX
$res->write($chunk);
}

$res->end();
}
}
20 changes: 12 additions & 8 deletions src/concerns/InteractsWithPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,25 @@ protected function getPoolConnection($name)
}
}

return $this->buildPoolConnection($connection, $pool);
return $this->wrapProxy($pool, $connection);
}

abstract protected function buildPoolConnection($connection, Channel $pool);
protected function wrapProxy(Channel $pool, $connection)
{
defer(function () use ($pool, $connection) {
//自动归还
if (!$pool->isFull()) {
$pool->push($connection, 0.001);
}
});

return $connection;
}

abstract protected function createPoolConnection(string $name);

abstract protected function getPoolMaxActive($name): int;

abstract protected function getPoolMaxWaitTime($name): int;

public function __destruct()
{
foreach ($this->pools as $pool) {
$pool->close();
}
}
}
2 changes: 1 addition & 1 deletion src/concerns/InteractsWithPools.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected function preparePools()
$pools = $this->getPools();

foreach ($this->getConfig('pool', []) as $name => $config) {
$type = Arr::get($config, 'type');
$type = Arr::pull($config, 'type');
if ($type && is_subclass_of($type, ConnectorInterface::class)) {
$pool = new ConnectionPool(
Pool::pullPoolConfig($config),
Expand Down
6 changes: 5 additions & 1 deletion src/concerns/InteractsWithServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ protected function setSwooleServerListeners()
protected function addHotUpdateProcess()
{
$process = new Process(function () {
$watcher = new FileWatcher($this->getConfig('hot_update.include', []), $this->getConfig('hot_update.exclude', []), $this->getConfig('hot_update.name', []));
$watcher = new FileWatcher(
$this->getConfig('hot_update.include', []),
$this->getConfig('hot_update.exclude', []),
$this->getConfig('hot_update.name', [])
);

$watcher->watch(function () {
$this->getServer()->reload();
Expand Down
7 changes: 0 additions & 7 deletions src/pool/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

namespace think\swoole\pool;

use Swoole\Coroutine\Channel;
use think\swoole\concerns\InteractsWithPool;
use think\swoole\coroutine\Context;
use think\swoole\pool\cache\Store;

class Cache extends \think\Cache
{
Expand Down Expand Up @@ -35,11 +33,6 @@ protected function driver(string $name = null)
});
}

protected function buildPoolConnection($connection, Channel $pool)
{
return new Store($connection, $pool);
}

protected function createPoolConnection(string $name)
{
return $this->createDriver($name);
Expand Down
11 changes: 2 additions & 9 deletions src/pool/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace think\swoole\pool;

use Swoole\Coroutine\Channel;
use think\Config;
use think\db\ConnectionInterface;
use think\swoole\concerns\InteractsWithPool;
use think\swoole\coroutine\Context;
use think\swoole\pool\db\Connection;

/**
* Class Db
Expand All @@ -31,8 +29,8 @@ protected function getPoolMaxWaitTime($name): int
/**
* 创建数据库连接实例
* @access protected
* @param string|null $name 连接标识
* @param bool $force 强制重新连接
* @param string|null $name 连接标识
* @param bool $force 强制重新连接
* @return ConnectionInterface
*/
protected function instance(string $name = null, bool $force = false): ConnectionInterface
Expand All @@ -50,11 +48,6 @@ protected function instance(string $name = null, bool $force = false): Connectio
});
}

protected function buildPoolConnection($connection, Channel $pool)
{
return new Connection($connection, $pool);
}

protected function createPoolConnection(string $name)
{
return $this->createConnection($name);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace think\swoole\concerns;
namespace think\swoole\pool;

use RuntimeException;
use Swoole\Coroutine\Channel;

trait InteractsWithPoolConnector
abstract class Proxy
{
protected $handler;

Expand Down
8 changes: 3 additions & 5 deletions src/pool/cache/Store.php → src/pool/proxy/Cache.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
<?php

namespace think\swoole\pool\cache;
namespace think\swoole\pool\proxy;

use Psr\SimpleCache\CacheInterface;
use think\contract\CacheHandlerInterface;
use think\swoole\concerns\InteractsWithPoolConnector;
use think\swoole\pool\Proxy;

/**
* Class Store
* @package think\swoole\pool\cache
*
* @property CacheHandlerInterface|CacheInterface $handler
*/
class Store implements CacheHandlerInterface, CacheInterface
class Cache extends Proxy implements CacheHandlerInterface, CacheInterface
{
use InteractsWithPoolConnector;

/**
* @inheritDoc
*/
Expand Down
29 changes: 14 additions & 15 deletions src/pool/db/Connection.php → src/pool/proxy/Db.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
<?php

namespace think\swoole\pool\db;
namespace think\swoole\pool\proxy;

use Psr\SimpleCache\CacheInterface;
use think\db\BaseQuery;
use think\db\ConnectionInterface;
use think\DbManager;
use think\swoole\concerns\InteractsWithPoolConnector;
use think\swoole\pool\Proxy;

/**
* Class Connection
* @package think\swoole\pool\db
* @property ConnectionInterface $handler
*/
class Connection implements ConnectionInterface
class Db extends Proxy implements ConnectionInterface
{
use InteractsWithPoolConnector;

/**
* 获取当前连接器类对应的Query类
Expand All @@ -30,7 +29,7 @@ public function getQueryClass(): string
/**
* 连接数据库方法
* @access public
* @param array $config 接参数
* @param array $config 接参数
* @param integer $linkNum 连接序号
* @return mixed
*/
Expand Down Expand Up @@ -106,8 +105,8 @@ public function select(BaseQuery $query): array
/**
* 插入记录
* @access public
* @param BaseQuery $query 查询对象
* @param boolean $getLastInsID 返回自增主键
* @param BaseQuery $query 查询对象
* @param boolean $getLastInsID 返回自增主键
* @return mixed
*/
public function insert(BaseQuery $query, bool $getLastInsID = false)
Expand All @@ -118,8 +117,8 @@ public function insert(BaseQuery $query, bool $getLastInsID = false)
/**
* 批量插入记录
* @access public
* @param BaseQuery $query 查询对象
* @param mixed $dataSet 数据集
* @param BaseQuery $query 查询对象
* @param mixed $dataSet 数据集
* @return integer
* @throws \Exception
* @throws \Throwable
Expand Down Expand Up @@ -154,9 +153,9 @@ public function delete(BaseQuery $query): int
/**
* 得到某个字段的值
* @access public
* @param BaseQuery $query 查询对象
* @param string $field 字段名
* @param mixed $default 默认值
* @param BaseQuery $query 查询对象
* @param string $field 字段名
* @param mixed $default 默认值
* @return mixed
*/
public function value(BaseQuery $query, string $field, $default = null)
Expand All @@ -167,9 +166,9 @@ public function value(BaseQuery $query, string $field, $default = null)
/**
* 得到某个列的数组
* @access public
* @param BaseQuery $query 查询对象
* @param string $column 字段名 多个字段用逗号分隔
* @param string $key 索引
* @param BaseQuery $query 查询对象
* @param string $column 字段名 多个字段用逗号分隔
* @param string $key 索引
* @return array
*/
public function column(BaseQuery $query, string $column, string $key = ''): array
Expand Down

0 comments on commit 300242e

Please sign in to comment.