Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
use Swoole\Http\Server;
use Swoole\Coroutine\Channel;
use Swoole\Coroutine\Redis;
$server = new Server('0.0.0.0', 8000);
$chan = new Channel(100);
$server->on(
'WorkerStart',
function (Server $server) use ($chan) {
for ($i = 0; $i < 100; $i++) {
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$chan->push($redis);
}
}
);
$server->on(
'request',
function ($request, $response) use ($chan) {
/** @var Redis $redis */
$redis = $chan->pop();
$redis->set('swoole', rand(1, 100));
$result = $redis->get('swoole');
$response->end("<h1>Hello World. #{$result}</h1>");
// return connection to the channel
$chan->push($redis);
}
);
$server->start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment