Created
May 1, 2020 20:41
-
-
Save leocarmo/1c4979014b019cef5d1732a572b45dfe to your computer and use it in GitHub Desktop.
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
<?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