Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
… into dev2
  • Loading branch information
inhere committed Apr 30, 2021
2 parents ee58145 + 33b3a4e commit c55c73c
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 15 deletions.
18 changes: 11 additions & 7 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ jobs:
strategy:
fail-fast: true # fast fail
matrix:
php: [7.1, 7.2, 7.3, 7.4]
php: [7.2, 7.3, 7.4]
os: [ubuntu-latest] # , macOS-latest, windows-latest
swoole-versions: [''] # latest
include:
- os: 'ubuntu-latest'
php: '7.1'
swoole-versions: '-4.5.10'

steps:
- uses: actions/checkout@v2
Expand All @@ -43,15 +48,14 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php}}
extensions: mbstring, dom, fileinfo, mysql, openssl, redis #optional, setup extensions
extensions: mbstring, dom, fileinfo, mysql, openssl, redis, swoole${{ matrix.swoole-versions }} #optional, setup extensions
ini-values: post_max_size=56M, short_open_tag=On #optional, setup php.ini configuration
coverage: none #optional, setup coverage driver: xdebug, none
pecl: true

- name: Install swoole extensions
timeout-minutes: 5
run: |
sudo pecl install -f swoole && php -m
- name: Display swoole extensions
timeout-minutes: 1
run: php --ri swoole && php --re swoole | grep class
# sudo pecl install -f swoole
# echo 'no' | pecl install -f redis
# sudo pecl update-channels && pecl install -f msgpack && pecl install -f igbinary && php -m

Expand Down
12 changes: 8 additions & 4 deletions run.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,14 @@

$status = 0;

Coroutine::set([
'log_level' => SWOOLE_LOG_INFO,
'trace_flags' => 0
]);
// fix: swoole >= 4.5.9
if (class_exists('Swoole\Coroutine', false)) {
Coroutine::set([
'log_level' => SWOOLE_LOG_INFO,
'trace_flags' => 0
]);
}

\Swoft\Co::run(function () {
// Status
global $status;
Expand Down
3 changes: 2 additions & 1 deletion src/http-server/test/testing/MockResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ public function cookie(
$domain = null,
$secure = null,
$httpOnly = null,
$samesite = null
$samesite = null,
$priority = null
) {
$result = \urlencode($name) . '=' . \urlencode($value);

Expand Down
35 changes: 35 additions & 0 deletions src/redis/src/Connection/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,41 @@ public function release(bool $force = false): void
parent::release($force);
}

/**
* @param int $iterator
* @param string|null $pattern
* @param int $count
* @return array
* @throws RedisException
*/
public function scan($iterator, string $pattern = null, int $count = 0): array
{
try {
// Before event
Swoft::trigger(RedisEvent::BEFORE_COMMAND, null, 'scan', [$iterator, $pattern, $count]);

Log::profileStart('redis.%s', 'scan');
$rest = $this->client->scan($iterator, $pattern, $count);
Log::profileEnd('redis.%s', 'scan');

// After event
Swoft::trigger(RedisEvent::AFTER_COMMAND, null, 'scan', [$iterator, $pattern, $count], ['cursor'=>$iterator,'result'=>$rest]);

// Release Connection
$this->release();
return ['cursor'=>$iterator,'result'=>$rest];
} catch (Throwable $e) {
if ($this->reconnect()) {
$rest = $this->client->scan($iterator, $pattern, $count);
return ['cursor'=>$iterator,'result'=>$rest];
}

throw new RedisException(
sprintf('Redis command reconnect error(%s)', $e->getMessage())
);
}
}

/**
* @param string $key
* @param array $keys
Expand Down
1 change: 1 addition & 0 deletions src/redis/src/Connection/ConnectionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function release(bool $final = false): void
$connection->release();
}
}
$this->unset($key);

if ($final) {
$finalKey = sprintf('%d', Co::tid());
Expand Down
58 changes: 58 additions & 0 deletions src/server/src/Context/PipeMessageContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* This file is part of Swoft.
*
* @link https://swoft.org
* @document https://swoft.org/docs
* @contact [email protected]
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/

namespace Swoft\Server\Context;

use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\Concern\PrototypeTrait;
use Swoft\Context\AbstractContext;

/**
* Class PipeMessageContext
* @package App\Context
*
* @Bean(scope=Bean::PROTOTYPE)
*/
class PipeMessageContext extends AbstractContext
{
use PrototypeTrait;

/**
* @param int $srcWorkerId
* @param $pipeMessage
* @return PipeMessageContext
*/
public static function new(int $srcWorkerId, $pipeMessage): self
{
$self = self::__instance();

$self->set('srcWorkerId', $srcWorkerId);
$self->set('pipeMessage', $pipeMessage);

return $self;
}

/**
* @return int
*/
public function getSrcWorkerId(): int
{
return $this->get('srcWorkerId');
}

/**
* @return mixed
*/
public function getPipeMessage()
{
return $this->get('pipeMessage');
}
}

27 changes: 25 additions & 2 deletions src/server/src/Swoole/PipeMessageListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@

use Swoft;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Context\Context;
use Swoft\Log\Error;
use Swoft\Log\Helper\CLog;
use Swoft\Server\Contract\PipeMessageInterface;
use Swoft\Server\ServerEvent;
use Swoft\SwoftEvent;
use Swoole\Server;
use Swoft\Server\Context\PipeMessageContext;
use Throwable;

/**
* Class PipeMessageListener
Expand All @@ -34,8 +39,26 @@ class PipeMessageListener implements PipeMessageInterface
*/
public function onPipeMessage(Server $server, int $srcWorkerId, $message): void
{
CLog::debug("PipeMessage: received pipe-message fromWID=$srcWorkerId message=$message");
Context::set(PipeMessageContext::new($srcWorkerId, $message));

Swoft::trigger(ServerEvent::PIPE_MESSAGE, $message, $srcWorkerId, $server);
CLog::debug("PipeMessage: received pipe-message fromWID={$srcWorkerId}}");

try {

Swoft::trigger(ServerEvent::PIPE_MESSAGE, $message, $srcWorkerId, $server);

} catch (Throwable $e) {

Error::log("PipeMessage handle fails: {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}");

} finally {

// Defer
Swoft::trigger(SwoftEvent::COROUTINE_DEFER);

// Destroy
Swoft::trigger(SwoftEvent::COROUTINE_COMPLETE);

}
}
}
3 changes: 2 additions & 1 deletion src/websocket-server/test/testing/MockHttpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public function cookie(
$domain = null,
$secure = null,
$httpOnly = null,
$samesite = null
$samesite = null,
$priority = null
) {
$result = \urlencode($name) . '=' . \urlencode($value);

Expand Down

0 comments on commit c55c73c

Please sign in to comment.