Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.

Commit 9184fea

Browse files
authored
Merge pull request #230 from swooletw/develop
Develop
2 parents e0b9167 + 4b1d510 commit 9184fea

File tree

11 files changed

+510
-359
lines changed

11 files changed

+510
-359
lines changed

.travis.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ sudo: false
44

55
matrix:
66
include:
7-
- php: 7.2
8-
env: FRAMEWORK_VERSION=laravel/framework:5.3.*
97
- php: 7.2
108
env: FRAMEWORK_VERSION=laravel/framework:5.4.*
119
- php: 7.2
@@ -14,8 +12,8 @@ matrix:
1412
env: FRAMEWORK_VERSION=laravel/framework:5.6.*
1513
- php: 7.2
1614
env: FRAMEWORK_VERSION=laravel/framework:5.7.*
17-
- php: 7.2
18-
env: FRAMEWORK_VERSION=laravel/lumen-framework:5.3.*
15+
# - php: 7.2
16+
# env: FRAMEWORK_VERSION=laravel/framework:5.8.*
1917
- php: 7.2
2018
env: FRAMEWORK_VERSION=laravel/lumen-framework:5.4.*
2119
- php: 7.2

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ This package provides a high performance HTTP server to speed up your Laravel/Lu
1212

1313
| PHP | Laravel | Lumen | Swoole |
1414
|:-------:|:-------:|:-----:|:-------:|
15-
| >=7.2 | ~5.3 | ~5.3 | >=4.0.0 |
15+
| >=7.2 | ~5.4 | ~5.4 | >=4.0.0 |
1616

1717
## Features
1818

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
],
2323
"require": {
2424
"php": "^7.2",
25-
"illuminate/console": "~5.3",
26-
"illuminate/contracts": "~5.3",
27-
"illuminate/http": "~5.3",
28-
"illuminate/support": "~5.3",
25+
"illuminate/console": "~5.4",
26+
"illuminate/contracts": "~5.4",
27+
"illuminate/http": "~5.4",
28+
"illuminate/support": "~5.4",
2929
"predis/predis": "^1.1"
3030
},
3131
"require-dev": {
32-
"laravel/lumen-framework": "~5.3",
32+
"laravel/lumen-framework": "~5.4",
3333
"phpunit/phpunit": "^7.5",
3434
"phpunit/php-code-coverage": "^6.1",
3535
"php-coveralls/php-coveralls": "^2.1",

src/Concerns/InteractsWithWebsocket.php

Lines changed: 12 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
namespace SwooleTW\Http\Concerns;
44

55
use Throwable;
6-
use Illuminate\Support\Arr;
76
use Illuminate\Pipeline\Pipeline;
87
use SwooleTW\Http\Server\Sandbox;
9-
use SwooleTW\Http\Websocket\Push;
108
use SwooleTW\Http\Websocket\Parser;
9+
use SwooleTW\Http\Websocket\Pusher;
1110
use SwooleTW\Http\Websocket\Websocket;
1211
use SwooleTW\Http\Transformers\Request;
1312
use SwooleTW\Http\Server\Facades\Server;
@@ -158,7 +157,7 @@ public function onClose($server, $fd, $reactorId)
158157
*/
159158
protected function isWebsocketPushPacket($packet)
160159
{
161-
if ( !is_array($packet)) {
160+
if (! is_array($packet)) {
162161
return false;
163162
}
164163

@@ -176,25 +175,11 @@ protected function isWebsocketPushPacket($packet)
176175
*/
177176
public function pushMessage($server, array $data)
178177
{
179-
$push = Push::new($data);
180-
$payload = $this->payloadParser->encode($push->getEvent(), $push->getMessage());
181-
182-
// attach sender if not broadcast
183-
if (! $push->isBroadcast() && $push->getSender() && ! $push->hasOwnDescriptor()) {
184-
$push->addDescriptor($push->getSender());
185-
}
186-
187-
// check if to broadcast all clients
188-
if ($push->isBroadcastToAllDescriptors()) {
189-
$push->mergeDescriptor($this->filterWebsocket($server->connections));
190-
}
191-
192-
// push message to designated fds
193-
foreach ($push->getDescriptors() as $descriptor) {
194-
if ($server->exist($descriptor) || ! $push->isBroadcastToDescriptor((int) $descriptor)) {
195-
$server->push($descriptor, $payload, $push->getOpcode());
196-
}
197-
}
178+
$pusher = Pusher::make($data, $server);
179+
$pusher->push($this->payloadParser->encode(
180+
$pusher->getEvent(),
181+
$pusher->getMessage()
182+
));
198183
}
199184

200185
/**
@@ -244,25 +229,8 @@ protected function prepareWebsocket()
244229
*/
245230
protected function isServerWebsocket(int $fd): bool
246231
{
247-
$info = $this->container->make(Server::class)->connection_info($fd);
248-
249-
return Arr::has($info, 'websocket_status') && Arr::get($info, 'websocket_status');
250-
}
251-
252-
/**
253-
* Returns all descriptors that are websocket
254-
*
255-
* @param array $descriptors
256-
*
257-
* @return array
258-
*/
259-
protected function filterWebsocket(array $descriptors): array
260-
{
261-
$callback = function ($descriptor) {
262-
return $this->isServerWebsocket($descriptor);
263-
};
264-
265-
return collect($descriptors)->filter($callback)->toArray();
232+
return $this->container->make(Server::class)
233+
->connection_info($fd)['websocket_status'] ?? false;
266234
}
267235

268236
/**
@@ -364,41 +332,21 @@ protected function loadWebsocketRoutes()
364332
return require $routePath;
365333
}
366334

367-
/**
368-
* Normalize data for message push.
369-
*
370-
* @param array $data
371-
*
372-
* @return array
373-
*/
374-
public function normalizePushData(array $data)
375-
{
376-
$opcode = Arr::get($data, 'opcode', 1);
377-
$sender = Arr::get($data, 'sender', 0);
378-
$fds = Arr::get($data, 'fds', []);
379-
$broadcast = Arr::get($data, 'broadcast', false);
380-
$assigned = Arr::get($data, 'assigned', false);
381-
$event = Arr::get($data, 'event', null);
382-
$message = Arr::get($data, 'message', null);
383-
384-
return [$opcode, $sender, $fds, $broadcast, $assigned, $event, $message];
385-
}
386-
387335
/**
388336
* Indicates if the payload is websocket push.
389337
*
390338
* @param mixed $payload
391339
*
392340
* @return boolean
393341
*/
394-
protected function isWebsocketPushPayload($payload): bool
342+
public function isWebsocketPushPayload($payload): bool
395343
{
396344
if (! is_array($payload)) {
397345
return false;
398346
}
399347

400348
return $this->isServerWebsocket
401-
&& array_key_exists('action', $payload)
402-
&& $payload['action'] === Websocket::PUSH_ACTION;
349+
&& ($payload['action'] ?? null) === Websocket::PUSH_ACTION
350+
&& array_key_exists('data', $payload);
403351
}
404352
}

src/HttpServiceProvider.php

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace SwooleTW\Http;
44

5-
use Illuminate\Support\Arr;
65
use SwooleTW\Http\Helpers\FW;
76
use Illuminate\Queue\QueueManager;
87
use Illuminate\Contracts\Http\Kernel;
@@ -177,17 +176,17 @@ protected function registerDatabaseDriver()
177176
{
178177
$this->app->extend(DatabaseManager::class, function (DatabaseManager $db) {
179178
$db->extend('mysql-coroutine', function ($config, $name) {
180-
$config = $this->getMergedDatabaseConfig($config, $name);
179+
$config['name'] = $name;
181180

182181
$connection = new MySqlConnection(
183-
$this->getNewMySqlConnection($config),
184-
Arr::get($config, 'database'),
185-
Arr::get($config, 'prefix'),
182+
$this->getNewMySqlConnection($config, 'write'),
183+
$config['database'],
184+
$config['prefix'],
186185
$config
187186
);
188187

189-
if (Arr::has($config, 'read')) {
190-
$connection->setReadPdo($this->getNewMySqlConnection($config));
188+
if (isset($config['read'])) {
189+
$connection->setReadPdo($this->getNewMySqlConnection($config, 'read'));
191190
}
192191

193192
return $connection;
@@ -197,33 +196,20 @@ protected function registerDatabaseDriver()
197196
});
198197
}
199198

200-
/**
201-
* Get mereged config for coroutine mysql.
202-
*
203-
* @param array $config
204-
* @param string $name
205-
*
206-
* @return array
207-
*/
208-
protected function getMergedDatabaseConfig(array $config, string $name)
209-
{
210-
$newConfig = $config;
211-
$newConfig = Arr::add($newConfig, 'name', $name);
212-
$newConfig = array_merge($newConfig, Arr::get($newConfig, 'read', []));
213-
$newConfig = array_merge($newConfig, Arr::get($newConfig, 'write', []));
214-
215-
return $newConfig;
216-
}
217-
218199
/**
219200
* Get a new mysql connection.
220201
*
221202
* @param array $config
203+
* @param string $connection
222204
*
223205
* @return \PDO
224206
*/
225-
protected function getNewMySqlConnection(array $config)
207+
protected function getNewMySqlConnection(array $config, string $connection = null)
226208
{
209+
if ($connection && isset($config[$connection])) {
210+
$config = array_merge($config, $config[$connection]);
211+
}
212+
227213
return ConnectorFactory::make(FW::version())->connect($config);
228214
}
229215

src/Server/Manager.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Illuminate\Contracts\Container\Container;
1919
use Illuminate\Contracts\Debug\ExceptionHandler;
2020
use SwooleTW\Http\Concerns\InteractsWithWebsocket;
21+
use Symfony\Component\Console\Output\ConsoleOutput;
2122
use SwooleTW\Http\Concerns\InteractsWithSwooleQueue;
2223
use SwooleTW\Http\Concerns\InteractsWithSwooleTable;
2324
use Symfony\Component\Debug\Exception\FatalThrowableError;
@@ -122,7 +123,7 @@ protected function setSwooleServerListeners()
122123
foreach ($this->events as $event) {
123124
$listener = Str::camel("on_$event");
124125
$callback = method_exists($this, $listener) ? [$this, $listener] : function () use ($event) {
125-
$this->container->make('events')->fire("swoole.$event", func_get_args());
126+
$this->container->make('events')->dispatch("swoole.$event", func_get_args());
126127
};
127128

128129
$this->container->make(Server::class)->on($event, $callback);
@@ -137,7 +138,7 @@ public function onStart()
137138
$this->setProcessName('master process');
138139
$this->createPidFile();
139140

140-
$this->container->make('events')->fire('swoole.start', func_get_args());
141+
$this->container->make('events')->dispatch('swoole.start', func_get_args());
141142
}
142143

143144
/**
@@ -148,7 +149,7 @@ public function onStart()
148149
public function onManagerStart()
149150
{
150151
$this->setProcessName('manager process');
151-
$this->container->make('events')->fire('swoole.managerStart', func_get_args());
152+
$this->container->make('events')->dispatch('swoole.managerStart', func_get_args());
152153
}
153154

154155
/**
@@ -162,7 +163,7 @@ public function onWorkerStart($server)
162163
{
163164
$this->clearCache();
164165

165-
$this->container->make('events')->fire('swoole.workerStart', func_get_args());
166+
$this->container->make('events')->dispatch('swoole.workerStart', func_get_args());
166167

167168
// don't init laravel app in task workers
168169
if ($server->taskworker) {
@@ -196,7 +197,7 @@ public function onWorkerStart($server)
196197
*/
197198
public function onRequest($swooleRequest, $swooleResponse)
198199
{
199-
$this->app->make('events')->fire('swoole.request');
200+
$this->app->make('events')->dispatch('swoole.request');
200201

201202
$this->resetOnRequest();
202203
$sandbox = $this->app->make(Sandbox::class);
@@ -261,21 +262,15 @@ protected function resetOnRequest()
261262
*/
262263
public function onTask($server, ...$args)
263264
{
264-
if ($args[0] instanceof \Swoole\Server\Task && $task = array_shift($args)) {
265-
list($taskId, $srcWorkerId, $data) = [$task->id, $task->worker_id, $task->data];
266-
} else {
267-
list($taskId, $srcWorkerId, $data) = $args;
268-
}
269-
270-
$this->container->make('events')->fire('swoole.task', [$server, $taskId, $srcWorkerId, $data]);
265+
$this->container->make('events')->dispatch('swoole.task', [$server, $args]);
271266

272267
try {
273268
// push websocket message
274269
if ($this->isWebsocketPushPayload($data)) {
275-
$this->pushMessage($server, $data['data'] ?? []);
270+
$this->pushMessage($server, $data['data']);
276271
// push async task to queue
277272
} elseif ($this->isAsyncTaskPayload($data)) {
278-
(new SwooleTaskJob($this->container, $server, $data, $taskId, $srcWorkerId))->fire();
273+
(new SwooleTaskJob($this->container, $server, $data, $taskId, $srcWorkerId))->dispatch();
279274
}
280275
} catch (Throwable $e) {
281276
$this->logServerError($e);
@@ -292,7 +287,7 @@ public function onTask($server, ...$args)
292287
public function onFinish($server, $taskId, $data)
293288
{
294289
// task worker callback
295-
$this->container->make('events')->fire('swoole.finish', func_get_args());
290+
$this->container->make('events')->dispatch('swoole.finish', func_get_args());
296291

297292
return;
298293
}
@@ -426,11 +421,16 @@ protected function isInTesting()
426421
*/
427422
public function logServerError(Throwable $e)
428423
{
429-
$this->container
430-
->make(ExceptionHandler::class)
431-
->report(
432-
$this->normalizeException($e)
433-
);
424+
if ($this->isInTesting()) {
425+
return;
426+
}
427+
428+
$exception = $this->normalizeException($e);
429+
$this->container->make(ConsoleOutput::class)
430+
->writeln(sprintf("<error>%s</error>", $exception));
431+
432+
$this->container->make(ExceptionHandler::class)
433+
->report($exception);
434434
}
435435

436436
/**

src/Transformers/Response.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ protected function sendInChunk($content)
129129
$this->swooleResponse->write($v);
130130
}
131131
}
132+
132133
$this->swooleResponse->end();
133134
}
134135

0 commit comments

Comments
 (0)