Skip to content

Commit

Permalink
Event loop improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Jul 14, 2024
1 parent 248b4ef commit d8c6a40
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 223 deletions.
126 changes: 16 additions & 110 deletions http_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,125 +11,31 @@
require_once __DIR__ . '/src/WordPress/AsyncHttp/InflateStreamWrapperData.php';

$requests = [
// new Request("https://playground.internal"),
// (new Request("https://anglesharp.azurewebsites.net/Chunked"))->set_http_version('1.1'),
// (new Request("https://anglesharp.azurewebsites.net/Chunked"))->set_http_version('1.0'),
(new Request("http://127.0.0.1:3000/")) //->set_http_version('1.0'),
new Request("https://playground.internal"),
(new Request("https://anglesharp.azurewebsites.net/Chunked"))->set_http_version('1.1'),
(new Request("https://anglesharp.azurewebsites.net/Chunked"))->set_http_version('1.0'),
(new Request("http://127.0.0.1:3000/"))->set_http_version('1.0'),
];
// list($streams, $headers, $errors) = streams_send_http_requests($requests);
// print_r($streams);
// print_r($errors);

// var_dump(streams_http_response_read_bytes($streams, 1024));
// Enqueuing another request here is instant and won't start the download yet.
$client = new Client();
$queue = $client->enqueue( $requests );
var_dump($client->read_bytes($requests[0], 10, Client::READ_POLL_ANY));
var_dump($client->read_bytes($requests[0], 1024, Client::READ_NON_BLOCKING));
var_dump($client->read_bytes($requests[0], 1024, Client::READ_POLL_ANY));
// var_dump($client->read_bytes($requests[0], 1024));

die();

// @TODO: handle wait_for_all_requested_bytes for more than content-length bytes
var_dump(stream_get_contents($requests[1]->get_response()->body_stream));
// var_dump($client->read_bytes($requests[1], 359, [
// 'mode' => 'poll_once',
// ]));
// var_dump($client->read_bytes($requests[1], 359, [
// 'mode' => 'poll_once',
// ]));
// var_dump($client->read_bytes($requests[1], 359, [
// 'mode' => 'poll_once',
// ]));
// @TODO: poll_once should eventully mark the request as finished
var_dump("----");
var_dump($client->read_bytes($requests[2], 1024, [
'mode' => 'return',
]));
var_dump($client->read_bytes($requests[2], 1024, [
'mode' => 'poll_once',
]));
// var_dump($queue);
// var_dump($queue[0]);
// var_dump($client->read_bytes($requests[0], 1024, [
// 'mode' => 'return',
// ]));
// var_dump(fread($queue[0]->get_body_stream(), 1));
// var_dump(fread($queue[0]->get_body_stream(), 1));
// var_dump(fread($queue[0]->get_body_stream(), 1));
die();
// var_dump($queue[0]);
var_dump($client->read_bytes($requests[0], 186, [
'mode' => 'return',
]));
var_dump($client->read_bytes($requests[0], 186, [
'mode' => 'return',
]));
// var_dump($queue[0]->get_status_code());
// var_dump($queue[0]->get_headers());

// var_dump(stream_get_contents($queue[0]->response->body_stream));
die();
$client = new Client();
$client->set_progress_callback( function ( Request $request, $downloaded, $total ) {
echo "$request->url – Downloaded: $downloaded / $total\n";
// echo "$request->url – Downloaded: $downloaded / $total\n";
} );

$requests = [
new Request("https://anglesharp.azurewebsites.net/Chunked")
// new Request( "https://downloads.wordpress.org/plugin/gutenberg.17.7.0.zip" ),
// new Request( "https://downloads.wordpress.org/theme/pendant.zip" ),
];
$queue = $client->enqueue( $requests );
var_dump($queue[0]);
die();
// Enqueuing another request here is instant and won't start the download yet.
//$streams2 = $client->enqueue( [
// new Request( "https://downloads.wordpress.org/plugin/hello-dolly.1.7.3.zip" ),
//] );
$client->enqueue( $requests );

try {
$client->read_bytes($requests[0], 4096);
// var_dump(stream_get_contents($streams1[0]));
} catch (Exception $e) {
echo $e->getMessage();
}
print_r($client);
print_r(stream_context_get_options($streams1[0]));
// Stream a single file, while streaming all the files
// file_put_contents( 'output-round1-0.zip', stream_get_contents( $streams1[0] ) );
//file_put_contents( 'output-round1-1.zip', stream_get_contents( $streams1[1] ) );
die();
// Initiate more HTTPS requests
$streams3 = $client->enqueue( [
new Request( "https://downloads.wordpress.org/plugin/akismet.4.1.12.zip" ),
new Request( "https://downloads.wordpress.org/plugin/hello-dolly.1.7.3.zip" ),
new Request( "https://downloads.wordpress.org/plugin/hello-dolly.1.7.3.zip" ),
] );

// Download the rest of the files. Foreach() seems like downloading things
// sequentially, but we're actually streaming all the files in parallel.
$streams = array_merge( $streams2, $streams3 );
foreach ( $streams as $k => $stream ) {
file_put_contents( 'output-round2-' . $k . '.zip', stream_get_contents( $stream ) );
while(true) {
$request = $client->next_response_chunk();
if(false === $request) {
break;
}
echo "GOT DATA CHUNK ON REQUEST $request->id:\n";
echo $request->get_response()->consume_buffer(1024);
echo "----------------\n\n";
}

echo "Done! :)";

// ----------------------------
//
// Previous explorations:

// Non-blocking parallel processing – the fastest method.
//while ( $results = sockets_http_response_read_bytes( $streams, 8096 ) ) {
// foreach ( $results as $k => $chunk ) {
// file_put_contents( 'output' . $k . '.zip', $chunk, FILE_APPEND );
// }
//}

// Blocking sequential processing – the slowest method.
//foreach ( $streams as $k => $stream ) {
// stream_set_blocking( $stream, 1 );
// file_put_contents( 'output' . $k . '.zip', stream_get_contents( $stream ) );
//}
// $client->wait_for_headers($requests[3]);
// var_dump($requests[3]->get_response()->get_headers());
Loading

0 comments on commit d8c6a40

Please sign in to comment.