-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Demin Yin <[email protected]>
- Loading branch information
Showing
7 changed files
with
82 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,18 @@ | ||
# This Dockerfile is to build Docker image "deminy/swoole-by-examples:server". | ||
# This Dockerfile is to build Docker image "deminy/swoole-by-examples:server-5.1". | ||
# @see https://hub.docker.com/r/deminy/swoole-by-examples | ||
FROM phpswoole/swoole:5.1-php8.2 | ||
|
||
COPY ./rootfilesystem / | ||
|
||
# The System V messages support is to run some example in script "./examples/pool/process-pool/client.php". | ||
# 1. The System V messages support is to run the example from script "./examples/pool/process-pool/client.php". | ||
# 2. The APCu extension is to run the example from script "./examples/servers/apcu-caching.php". | ||
RUN \ | ||
set -ex && \ | ||
docker-php-ext-install sysvmsg && \ | ||
pecl channel-update pecl && \ | ||
pecl install apcu-stable && \ | ||
docker-php-ext-enable apcu && \ | ||
echo "apc.enable_cli=1" >> $(php-config --ini-dir)/docker-php-ext-apcu.ini && \ | ||
apt-get update && \ | ||
apt-get install -y net-tools watch --no-install-recommends && \ | ||
rm -rf /var/lib/apt/lists/* |
12 changes: 12 additions & 0 deletions
12
dockerfiles/server/rootfilesystem/etc/supervisor/service.d/apcu-caching.conf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[supervisord] | ||
user = root | ||
|
||
[program:apcu-caching] | ||
command = /var/www/servers/apcu-caching.php | ||
user = root | ||
autostart = true | ||
autorestart = true | ||
stdout_logfile=/proc/self/fd/1 | ||
stdout_logfile_maxbytes=0 | ||
stderr_logfile=/proc/self/fd/1 | ||
stderr_logfile_maxbytes=0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* In this example we use APCu to count number of HTTP requests processed by each worker process in Swoole. From this | ||
* example, we can see that APCu caching in Swoole works the same way as in other PHP CLI applications, even when | ||
* multiple coroutines and multiple processes are involved. | ||
* | ||
* Here is how to run this example: | ||
* 1. First, let's make 499 HTTP requests to the server concurrently: | ||
* docker compose exec -t client ab -n 499 -c 499 http://server:9513/ | ||
* With the above command, we make 499 HTTP requests to the server concurrently. The server uses APCu to store the | ||
* number of HTTP requests processed by each worker process. | ||
* 2. Next, let's check the summary of all counters by making another HTTP request to the server: | ||
* docker compose exec -t client curl http://server:9513/summary | ||
* The server has 3 worker processes to process these requests. Hopefully we can see that each worker process | ||
* processes different number of HTTP requests. | ||
*/ | ||
|
||
use Swoole\Constant; | ||
use Swoole\Http\Request; | ||
use Swoole\Http\Response; | ||
use Swoole\Http\Server; | ||
|
||
apcu_clear_cache(); // Clear APCu caches. | ||
|
||
$server = new Server('0.0.0.0', 9513); | ||
$server->set( | ||
[ | ||
Constant::OPTION_WORKER_NUM => 3, // The number of worker processes to process HTTP requests. | ||
] | ||
); | ||
|
||
$server->on( | ||
'request', | ||
function (Request $request, Response $response) use ($server): void { | ||
if ($request->server['request_uri'] === '/summary') { // Show summary of all counters. | ||
$output = ''; | ||
foreach (apcu_cache_info()['cache_list'] as $item) { // @phpstan-ignore foreach.nonIterable | ||
$output .= "{$item['info']}: " . apcu_fetch($item['info']) . PHP_EOL; // @phpstan-ignore-line | ||
} | ||
// The output will be like: | ||
// counter_0: 46 | ||
// counter_1: 16 | ||
// counter_2: 437 | ||
$response->end($output); | ||
} else { // Increase a counter. | ||
apcu_inc("counter_{$server->worker_id}"); | ||
$response->end('OK' . PHP_EOL); | ||
} | ||
} | ||
); | ||
|
||
$server->start(); |