Merge branch 'master' into 3.0-merge

# Conflicts:
#	.github/workflows/test.yml
#	phpunit.xml
#	src/amqp/src/ConnectionFactory.php
#	src/amqp/src/IO/SwooleIO.php
This commit is contained in:
李铭昕 2021-12-30 19:04:00 +08:00
commit ad5afa9073
15 changed files with 346 additions and 74 deletions

View File

@ -6,7 +6,7 @@ on:
schedule:
- cron: '0 2 * * *'
env:
SW_VERSION: 'v4.8.4'
SW_VERSION: 'v4.8.5'
jobs:
database:
name: Test for Database

View File

@ -13,9 +13,11 @@ jobs:
strategy:
matrix:
os: [ ubuntu-latest ]
php-version: [ '8.0', '8.1' ]
sw-version: [ 'v4.5.11', 'v4.6.7', 'v4.7.1', 'v4.8.4', 'master' ]
php-version: [ '7.4', '8.0', '8.1' ]
sw-version: [ 'v4.5.11', 'v4.6.7', 'v4.7.1', 'v4.8.5', 'master' ]
exclude:
- php-version: '7.4'
sw-version: 'master'
- php-version: '8.1'
sw-version: 'v4.5.11'
- php-version: '8.1'

View File

@ -1,5 +1,19 @@
# v2.2.22 - TBD
## Fixed
- [#4399](https://github.com/hyperf/hyperf/pull/4399) Fixed bug that `Redis::scan` does not work when using redis cluster.
## Added
- [#4409](https://github.com/hyperf/hyperf/pull/4409) Added database handler for `session`.
- [#4411](https://github.com/hyperf/hyperf/pull/4411) Added `Hyperf\Tracer\Aspect\DbAspect` to log db records when using `hyperf/db`.
- [#4420](https://github.com/hyperf/hyperf/pull/4420) Support `SSL` for `Hyperf\Amqp\IO\SwooleIO`.
## Optimized
- [#4406](https://github.com/hyperf/hyperf/pull/4406) Adapt swoole 5.0 by removing swoole classes with `PSR-0`.
# v2.2.21 - 2021-12-20
## Fixed

View File

@ -8,6 +8,60 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src/amqp/src</directory>
<directory suffix=".php">./src/async-queue/src</directory>
<directory suffix=".php">./src/cache/src</directory>
<directory suffix=".php">./src/circuit-breaker/src</directory>
<directory suffix=".php">./src/command/src</directory>
<directory suffix=".php">./src/config/src</directory>
<directory suffix=".php">./src/config-center/src</directory>
<directory suffix=".php">./src/config-nacos/src</directory>
<directory suffix=".php">./src/constants/src</directory>
<directory suffix=".php">./src/consul/src</directory>
<directory suffix=".php">./src/coordinator/src</directory>
<directory suffix=".php">./src/crontab/src</directory>
<directory suffix=".php">./src/dag/src</directory>
<directory suffix=".php">./src/database/src</directory>
<directory suffix=".php">./src/db-connection/src</directory>
<directory suffix=".php">./src/di/src</directory>
<directory suffix=".php">./src/dispatcher/src</directory>
<directory suffix=".php">./src/elasticsearch/src</directory>
<directory suffix=".php">./src/etcd/src</directory>
<directory suffix=".php">./src/event/src</directory>
<directory suffix=".php">./src/grpc-client/src</directory>
<directory suffix=".php">./src/guzzle/src</directory>
<directory suffix=".php">./src/http-message/src</directory>
<directory suffix=".php">./src/http-server/src</directory>
<directory suffix=".php">./src/json-rpc/src</directory>
<directory suffix=".php">./src/kafka/src</directory>
<directory suffix=".php">./src/logger/src</directory>
<directory suffix=".php">./src/macroable/src</directory>
<directory suffix=".php">./src/model-cache/src</directory>
<directory suffix=".php">./src/nacos/src</directory>
<directory suffix=".php">./src/nats/src</directory>
<directory suffix=".php">./src/nsq/src</directory>
<directory suffix=".php">./src/paginator/src</directory>
<directory suffix=".php">./src/phar/src</directory>
<directory suffix=".php">./src/redis/src</directory>
<directory suffix=".php">./src/rpc/src</directory>
<directory suffix=".php">./src/rpc-client/src</directory>
<directory suffix=".php">./src/rpc-multiplex/src</directory>
<directory suffix=".php">./src/rpn/src</directory>
<directory suffix=".php">./src/scout/src</directory>
<directory suffix=".php">./src/server/src</directory>
<directory suffix=".php">./src/service-governance/src</directory>
<directory suffix=".php">./src/session/src</directory>
<directory suffix=".php">./src/snowflake/src</directory>
<directory suffix=".php">./src/task/src</directory>
<directory suffix=".php">./src/testing/src</directory>
<directory suffix=".php">./src/tracer/src</directory>
<directory suffix=".php">./src/utils/src</directory>
<directory suffix=".php">./src/websocket-client/src</directory>
<directory suffix=".php">./src/websocket-server/src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Tests">
<directory suffix="Test.php">./src/amqp/tests</directory>
@ -82,58 +136,4 @@
<directory suffix="Test.php">./src/websocket-server/tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src/amqp/src</directory>
<directory suffix=".php">./src/async-queue/src</directory>
<directory suffix=".php">./src/cache/src</directory>
<directory suffix=".php">./src/circuit-breaker/src</directory>
<directory suffix=".php">./src/command/src</directory>
<directory suffix=".php">./src/config/src</directory>
<directory suffix=".php">./src/config-center/src</directory>
<directory suffix=".php">./src/config-nacos/src</directory>
<directory suffix=".php">./src/constants/src</directory>
<directory suffix=".php">./src/consul/src</directory>
<directory suffix=".php">./src/coordinator/src</directory>
<directory suffix=".php">./src/crontab/src</directory>
<directory suffix=".php">./src/dag/src</directory>
<directory suffix=".php">./src/database/src</directory>
<directory suffix=".php">./src/db-connection/src</directory>
<directory suffix=".php">./src/di/src</directory>
<directory suffix=".php">./src/dispatcher/src</directory>
<directory suffix=".php">./src/elasticsearch/src</directory>
<directory suffix=".php">./src/etcd/src</directory>
<directory suffix=".php">./src/event/src</directory>
<directory suffix=".php">./src/grpc-client/src</directory>
<directory suffix=".php">./src/guzzle/src</directory>
<directory suffix=".php">./src/http-message/src</directory>
<directory suffix=".php">./src/http-server/src</directory>
<directory suffix=".php">./src/json-rpc/src</directory>
<directory suffix=".php">./src/kafka/src</directory>
<directory suffix=".php">./src/logger/src</directory>
<directory suffix=".php">./src/macroable/src</directory>
<directory suffix=".php">./src/model-cache/src</directory>
<directory suffix=".php">./src/nacos/src</directory>
<directory suffix=".php">./src/nats/src</directory>
<directory suffix=".php">./src/nsq/src</directory>
<directory suffix=".php">./src/paginator/src</directory>
<directory suffix=".php">./src/phar/src</directory>
<directory suffix=".php">./src/redis/src</directory>
<directory suffix=".php">./src/rpc/src</directory>
<directory suffix=".php">./src/rpc-client/src</directory>
<directory suffix=".php">./src/rpc-multiplex/src</directory>
<directory suffix=".php">./src/rpn/src</directory>
<directory suffix=".php">./src/scout/src</directory>
<directory suffix=".php">./src/server/src</directory>
<directory suffix=".php">./src/service-governance/src</directory>
<directory suffix=".php">./src/session/src</directory>
<directory suffix=".php">./src/snowflake/src</directory>
<directory suffix=".php">./src/task/src</directory>
<directory suffix=".php">./src/testing/src</directory>
<directory suffix=".php">./src/tracer/src</directory>
<directory suffix=".php">./src/utils/src</directory>
<directory suffix=".php">./src/websocket-client/src</directory>
<directory suffix=".php">./src/websocket-server/src</directory>
</whitelist>
</filter>
</phpunit>

View File

@ -18,6 +18,7 @@ return [
'user' => env('AMQP_USER', 'guest'),
'password' => env('AMQP_PASSWORD', 'guest'),
'vhost' => env('AMQP_VHOST', '/'),
'open_ssl' => false,
'concurrent' => [
'limit' => 1,
],

View File

@ -22,13 +22,15 @@ class IOFactory implements IOFactoryInterface
{
$host = $config['host'] ?? 'localhost';
$port = $config['port'] ?? 5672;
$openSSL = $config['open_ssl'] ?? false;
return match (Constant::ENGINE) {
'Swoole' => new SwooleIO(
$host,
$port,
$params->getConnectionTimeout(),
$params->getReadWriteTimeout()
$params->getReadWriteTimeout(),
$openSSL
),
default => throw new NotSupportedException()
};

View File

@ -36,10 +36,7 @@ class SwooleIO extends AbstractIO
*/
protected $heartbeat;
/**
* @var null|Socket
*/
private $sock;
private ?Socket $sock = null;
/**
* @throws \InvalidArgumentException when readWriteTimeout argument does not 2x the heartbeat
@ -48,7 +45,8 @@ class SwooleIO extends AbstractIO
string $host,
int $port,
protected int $connectionTimeout,
protected int $readWriteTimeout = 3
protected int $readWriteTimeout = 3,
protected bool $openSSL = false
) {
$this->host = $host;
$this->port = $port;
@ -111,6 +109,11 @@ class SwooleIO extends AbstractIO
protected function makeClient()
{
$sock = new Socket(AF_INET, SOCK_STREAM, 0);
if ($this->openSSL === true) {
$sock->setProtocol(['open_ssl' => true]);
}
if (! $sock->connect($this->host, $this->port, $this->connectionTimeout)) {
throw new AMQPRuntimeException(
sprintf('Error Connecting to server: %s ', $sock->errMsg),

View File

@ -1846,7 +1846,7 @@ class Builder
/**
* Execute a query for a single record by ID.
*
* @param int $id
* @param mixed $id
* @param array $columns
* @return mixed|static
*/

View File

@ -23,7 +23,7 @@ use Psr\Container\ContainerInterface;
* DB Helper.
* @method static Builder table(string $table)
* @method static Expression raw($value)
* @method static selectOne(string $query, array $bindings = [], bool $useReadPdo = true)
* @method static mixed selectOne(string $query, array $bindings = [], bool $useReadPdo = true)
* @method static array select(string $query, array $bindings = [], bool $useReadPdo = true)
* @method static Generator cursor(string $query, array $bindings = [], bool $useReadPdo = true)
* @method static bool insert(string $query, array $bindings = [])

View File

@ -18,6 +18,7 @@ use Hyperf\GrpcClient\Exception\GrpcClientException;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\ChannelPool;
use InvalidArgumentException;
use Swoole\Http2\Response;
/**
* @method int send(Request $request)
@ -91,7 +92,7 @@ class BaseClient
* @param Message $argument The argument to the method
* @param callable $deserialize A function that deserializes the response
* @throws GrpcClientException
* @return array|\Google\Protobuf\Internal\Message[]|\swoole_http2_response[]
* @return array|\Google\Protobuf\Internal\Message[]|Response[]
*/
protected function _simpleRequest(
string $method,

View File

@ -13,23 +13,23 @@ namespace Hyperf\Redis;
trait ScanCaller
{
public function scan(&$cursor, $pattern = null, $count = 0)
public function scan(&$cursor, ...$arguments)
{
return $this->__call('scan', [&$cursor, $pattern, $count]);
return $this->__call('scan', array_merge([&$cursor], $arguments));
}
public function hScan($key, &$cursor, $pattern = null, $count = 0)
public function hScan($key, &$cursor, ...$arguments)
{
return $this->__call('hScan', [$key, &$cursor, $pattern, $count]);
return $this->__call('hScan', array_merge([$key, &$cursor], $arguments));
}
public function zScan($key, &$cursor, $pattern = null, $count = 0)
public function zScan($key, &$cursor, ...$arguments)
{
return $this->__call('zScan', [$key, &$cursor, $pattern, $count]);
return $this->__call('zScan', array_merge([$key, &$cursor], $arguments));
}
public function sScan($key, &$cursor, $pattern = null, $count = 0)
public function sScan($key, &$cursor, ...$arguments)
{
return $this->__call('sScan', [$key, &$cursor, $pattern, $count]);
return $this->__call('sScan', array_merge([$key, &$cursor], $arguments));
}
}

View File

@ -12,6 +12,8 @@ declare(strict_types=1);
namespace Hyperf\Session;
use Hyperf\Contract\SessionInterface;
use Hyperf\Session\Handler\DatabaseHandler;
use Hyperf\Session\Handler\DatabaseHandlerFactory;
use Hyperf\Session\Handler\FileHandler;
use Hyperf\Session\Handler\FileHandlerFactory;
use Hyperf\Session\Handler\RedisHandler;
@ -31,6 +33,7 @@ class ConfigProvider
],
'dependencies' => [
FileHandler::class => FileHandlerFactory::class,
DatabaseHandler::class => DatabaseHandlerFactory::class,
RedisHandler::class => RedisHandlerFactory::class,
SessionInterface::class => SessionProxy::class,
],

View File

@ -0,0 +1,136 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Session\Handler;
use Carbon\Carbon;
use Hyperf\Database\Query\Builder;
use Hyperf\DbConnection\Db;
use Hyperf\Utils\Arr;
use Hyperf\Utils\InteractsWithTime;
use SessionHandlerInterface;
class DatabaseHandler implements SessionHandlerInterface
{
use InteractsWithTime;
/**
* @var string
*/
protected $connection;
/**
* @var string
*/
protected $table;
/**
* @var int
*/
protected $minutes;
public function __construct(string $connection, string $table, int $minutes)
{
$this->table = $table;
$this->minutes = $minutes;
$this->connection = $connection;
}
public function open($savePath, $sessionName)
{
return true;
}
public function close()
{
return true;
}
public function read($sessionId)
{
$session = (object) $this->getQuery()->find($sessionId);
if (isset($session->last_activity)
&& $session->last_activity < Carbon::now()->subMinutes($this->minutes)->getTimestamp()) {
return '';
}
if (isset($session->payload)) {
return base64_decode($session->payload);
}
return '';
}
public function write($sessionId, $data)
{
$payload = $this->getDefaultPayload($data);
if ($this->getQuery()->find($sessionId)) {
$this->performUpdate($sessionId, $payload);
} else {
$this->performInsert($sessionId, $payload);
}
return true;
}
public function destroy($sessionId)
{
$this->getQuery()->where('id', $sessionId)->delete();
return true;
}
public function gc($lifetime)
{
return (bool) $this->getQuery()
->where('last_activity', '<=', $this->currentTime() - $lifetime)
->delete();
}
/**
* Perform an insert operation on the session ID.
*/
protected function performInsert(string $sessionId, array $payload): bool
{
return $this->getQuery()->insert(Arr::set($payload, 'id', $sessionId));
}
/**
* Perform an update operation on the session ID.
*/
protected function performUpdate(string $sessionId, array $payload): int
{
return $this->getQuery()->where('id', $sessionId)->update($payload);
}
/**
* Get the default payload for the session.
*
* @param string $data
*/
protected function getDefaultPayload($data): array
{
return [
'payload' => base64_encode($data),
'last_activity' => $this->currentTime(),
];
}
/**
* Get a fresh query builder instance for the table.
*/
protected function getQuery(): Builder
{
return Db::connection($this->connection)->table($this->table);
}
}

View File

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Session\Handler;
use Hyperf\Contract\ConfigInterface;
use Psr\Container\ContainerInterface;
class DatabaseHandlerFactory
{
public function __invoke(ContainerInterface $container)
{
$config = $container->get(ConfigInterface::class);
$connection = $config->get('session.options.connection');
$table = $config->get('session.options.table');
$minutes = $config->get('session.options.lifetime', 1200);
return new DatabaseHandler($connection, $table, $minutes);
}
}

View File

@ -0,0 +1,83 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Tracer\Aspect;
use Hyperf\DB\DB;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Hyperf\Tracer\SpanStarter;
use Hyperf\Tracer\SpanTagManager;
use Hyperf\Tracer\SwitchManager;
use OpenTracing\Tracer;
class DbAspect extends AbstractAspect
{
use SpanStarter;
/**
* @var array
*/
public $classes = [
DB::class . '::__call',
];
/**
* @var array
*/
public $annotations = [];
/**
* @var Tracer
*/
private $tracer;
/**
* @var SwitchManager
*/
private $switchManager;
/**
* @var SpanTagManager
*/
private $spanTagManager;
public function __construct(Tracer $tracer, SwitchManager $switchManager, SpanTagManager $spanTagManager)
{
$this->tracer = $tracer;
$this->switchManager = $switchManager;
$this->spanTagManager = $spanTagManager;
}
/**
* @return mixed return the value from process method of ProceedingJoinPoint, or the value that you handled
*/
public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
if ($this->switchManager->isEnable('db') === false) {
return $proceedingJoinPoint->process();
}
$arguments = $proceedingJoinPoint->arguments['keys'];
$span = $this->startSpan('Db' . '::' . $arguments['name']);
$span->setTag($this->spanTagManager->get('db', 'db.query'), json_encode($arguments['arguments']));
try {
$result = $proceedingJoinPoint->process();
} catch (\Throwable $e) {
$span->setTag('error', true);
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
throw $e;
} finally {
$span->finish();
}
return $result;
}
}