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

Commit 572d7f9

Browse files
authored
Merge pull request #139 from swooletw/feature/coroutine_feature
Feature/coroutine feature
2 parents 698e274 + 392052d commit 572d7f9

22 files changed

+121
-50
lines changed

config/swoole_http.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
'public_path' => base_path('public'),
1818
// Determine if to use swoole to respond request for static files
1919
'handle_static_files' => env('SWOOLE_HANDLE_STATIC', true),
20+
// You must add --enable-openssl while compiling Swoole
21+
// Put `SWOOLE_SOCK_TCP | SWOOLE_SSL` if you want to enable SSL
22+
'socket_type' => SWOOLE_SOCK_TCP,
2023
'options' => [
2124
'pid_file' => env('SWOOLE_HTTP_PID_FILE', base_path('storage/logs/swoole_http.pid')),
2225
'log_file' => env('SWOOLE_HTTP_LOG_FILE', base_path('storage/logs/swoole_http.log')),
@@ -57,6 +60,17 @@
5760
*/
5861
'ob_output' => env('SWOOLE_OB_OUTPUT', true),
5962

63+
/*
64+
|--------------------------------------------------------------------------
65+
| Pre-resolved instances here will be resolved when sandbox created.
66+
|--------------------------------------------------------------------------
67+
*/
68+
'pre_resolved' => [
69+
'view', 'files', 'session', 'session.store', 'routes',
70+
'db', 'db.factory', 'cache', 'cache.store', 'config', 'cookie',
71+
'encrypter', 'hash', 'router', 'translator', 'url', 'log',
72+
],
73+
6074
/*
6175
|--------------------------------------------------------------------------
6276
| Instances here will be cleared on every request.

src/Commands/HttpServerCommand.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -328,13 +328,14 @@ protected function isDaemon()
328328
protected function checkEnvironment()
329329
{
330330
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
331-
throw new \RuntimeException("Swoole extension doesn't support Windows OS yet.");
331+
$this->error("Swoole extension doesn't support Windows OS yet.");
332+
exit;
332333
} elseif (! extension_loaded('swoole')) {
333-
throw new \RuntimeException("Can't detect Swoole extension installed.");
334-
}
335-
336-
if (! version_compare(swoole_version(), '4.0.0-alpha', 'ge')) {
337-
throw new \RuntimeException("Your Swoole version must be higher than 4.0 to use coroutine.");
334+
$this->error("Can't detect Swoole extension installed.");
335+
exit;
336+
} elseif (! version_compare(swoole_version(), '4.0.0', 'ge')) {
337+
$this->error("Your Swoole version must be higher than 4.0 to use coroutine.");
338+
exit;
338339
}
339340
}
340341
}

src/Concerns/WithApplication.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,6 @@ trait WithApplication
2020
*/
2121
protected $app;
2222

23-
/**
24-
* Aliases for pre-resolving.
25-
*
26-
* @var array
27-
*/
28-
protected $resolves = [
29-
'view', 'files', 'session', 'session.store', 'routes',
30-
'db', 'db.factory', 'cache', 'cache.store', 'config', 'cookie',
31-
'encrypter', 'hash', 'router', 'translator', 'url', 'log'
32-
];
33-
3423
/**
3524
* Bootstrap framework.
3625
*/
@@ -146,7 +135,9 @@ public function getBasePath()
146135
*/
147136
protected function preResolveInstances()
148137
{
149-
foreach ($this->resolves as $abstract) {
138+
$resolves = $this->container['config']->get('swoole_http.pre_resolved', []);
139+
140+
foreach ($resolves as $abstract) {
150141
if ($this->getApplication()->offsetExists($abstract)) {
151142
$this->getApplication()->make($abstract);
152143
}

src/HttpServiceProvider.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,9 @@ protected function createSwooleServer()
116116
$server = $this->isWebsocket ? WebsocketServer::class : HttpServer::class;
117117
$host = $this->app['config']->get('swoole_http.server.host');
118118
$port = $this->app['config']->get('swoole_http.server.port');
119-
$hasCert = $this->app['config']->get('swoole_http.server.options.ssl_cert_file');
120-
$hasKey = $this->app['config']->get('swoole_http.server.options.ssl_key_file');
121-
$args = $hasCert && $hasKey ? [SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL] : [];
119+
$socketType = $this->app['config']->get('swoole_http.server.socket_type', SWOOLE_SOCK_TCP);
122120

123-
static::$server = new $server($host, $port, ...$args);
121+
static::$server = new $server($host, $port, SWOOLE_PROCESS, $socketType);
124122
}
125123

126124
/**
@@ -185,7 +183,7 @@ protected function registerSwooleQueueDriver()
185183
{
186184
$this->app->afterResolving('queue', function ($manager) {
187185
$manager->addConnector('swoole', function () {
188-
return new SwooleTaskConnector(static::$server);
186+
return new SwooleTaskConnector($this->app->make(Server::class));
189187
});
190188
});
191189
}

src/Server/Manager.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ public function onTask($server, $taskId, $srcWorkerId, $data)
255255
public function onFinish($server, $taskId, $data)
256256
{
257257
// task worker callback
258+
return;
258259
}
259260

260261
/**
@@ -271,7 +272,6 @@ public function onShutdown()
271272
protected function bindToLaravelApp()
272273
{
273274
$this->bindSandbox();
274-
$this->bindSwooleServer();
275275
$this->bindSwooleTable();
276276

277277
if ($this->isWebsocket) {
@@ -280,17 +280,6 @@ protected function bindToLaravelApp()
280280
}
281281
}
282282

283-
/**
284-
* Bind swoole server to Laravel app container.
285-
*/
286-
protected function bindSwooleServer()
287-
{
288-
$this->app->singleton(Server::class, function ($app) {
289-
return $this->container['swoole.server'];
290-
});
291-
$this->app->alias(Server::class, 'swoole.server');
292-
}
293-
294283
/**
295284
* Bind sandbox to Laravel app container.
296285
*/
@@ -396,6 +385,6 @@ protected function isInTesting()
396385
*/
397386
public function logServerError(Throwable $e)
398387
{
399-
$this->app[ExceptionHandler::class]->report($e);
388+
$this->container[ExceptionHandler::class]->report($e);
400389
}
401390
}

src/Server/Resetters/BindRequest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
use Illuminate\Http\Request;
66
use SwooleTW\Http\Server\Sandbox;
77
use Illuminate\Contracts\Container\Container;
8+
use SwooleTW\Http\Server\Resetters\ResetterContract;
89

9-
class BindRequest
10+
class BindRequest implements ResetterContract
1011
{
1112
/**
1213
* "handle" function for resetting app.

src/Server/Resetters/ClearInstances.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
use SwooleTW\Http\Server\Sandbox;
66
use Illuminate\Contracts\Container\Container;
7+
use SwooleTW\Http\Server\Resetters\ResetterContract;
78

8-
class ClearInstances
9+
class ClearInstances implements ResetterContract
910
{
1011
/**
1112
* "handle" function for resetting app.

src/Server/Resetters/RebindKernelContainer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
use SwooleTW\Http\Server\Sandbox;
66
use Illuminate\Contracts\Http\Kernel;
77
use Illuminate\Contracts\Container\Container;
8+
use SwooleTW\Http\Server\Resetters\ResetterContract;
89

9-
class RebindKernelContainer
10+
class RebindKernelContainer implements ResetterContract
1011
{
1112
/**
1213
* "handle" function for resetting app.

src/Server/Resetters/RebindRouterContainer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
use SwooleTW\Http\Server\Sandbox;
66
use Illuminate\Contracts\Container\Container;
7+
use SwooleTW\Http\Server\Resetters\ResetterContract;
78
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
89

9-
class RebindRouterContainer
10+
class RebindRouterContainer implements ResetterContract
1011
{
1112
/**
1213
* "handle" function for resetting app.

src/Server/Resetters/RebindViewContainer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
use SwooleTW\Http\Server\Sandbox;
66
use Illuminate\Contracts\Container\Container;
7+
use SwooleTW\Http\Server\Resetters\ResetterContract;
78

8-
class RebindViewContainer
9+
class RebindViewContainer implements ResetterContract
910
{
1011
/**
1112
* "handle" function for resetting app.

0 commit comments

Comments
 (0)