From 97a659bdfca64437126ed6840fca63e46a865977 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Sun, 14 Jul 2024 16:43:37 +0200 Subject: [PATCH] Updates --- http_api.php | 20 +++++++------------- src/WordPress/AsyncHttp/Client.php | 6 +++--- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/http_api.php b/http_api.php index 8aaccd29..9d014a3c 100644 --- a/http_api.php +++ b/http_api.php @@ -24,28 +24,22 @@ // 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([ - 'concurrency' => 2, + 'concurrency' => 1, 'on_progress' => function ( Request $request, $downloaded, $total ) { - echo "$request->url – Downloaded: $downloaded / $total\n"; + // echo "$request->url – Downloaded: $downloaded / $total\n"; }, 'max_redirects' => 0 ]); $client->enqueue( $requests ); -echo $client->read_bytes($requests[3], 100, Client::READ_POLL_ANY)."\n\n"; - -while(true) { - $request = $client->await_response_bytes(); - if(false === $request) { - break; - } - echo "GOT DATA CHUNK ON REQUEST $request->id:\n"; - echo $client->read_bytes($request, 1024); - echo "----------------\n\n"; + +while($request = $client->await_incoming_bytes()) { + $bytes = $client->read_bytes($request, 1024); + echo "* ✅ Got " . strlen($bytes) . " bytes on request $request->id \n"; } foreach($client->get_failed_requests() as $failed_request) { - echo "Failed request to " . $failed_request->url . " – " . $failed_request->error . "\n"; + echo "* ❌ Failed request to " . $failed_request->url . " – " . $failed_request->error . "\n"; } diff --git a/src/WordPress/AsyncHttp/Client.php b/src/WordPress/AsyncHttp/Client.php index 757dfaed..fce1f09d 100644 --- a/src/WordPress/AsyncHttp/Client.php +++ b/src/WordPress/AsyncHttp/Client.php @@ -116,7 +116,7 @@ class Client { protected $connections = array(); public function __construct( $options = [] ) { - $this->concurrency = $options['concurrency'] ?? 2; + $this->concurrency = $options['concurrency'] ?? 10; $this->requests = []; $this->on_progress = $options['on_progress'] ?? function () { }; @@ -145,7 +145,7 @@ public function enqueue( $requests ) { } } - public function await_response_bytes() { + public function await_incoming_bytes() { do { foreach ( $this->requests as $request ) { $request = $request->latest_redirect(); @@ -222,7 +222,7 @@ public function await_response_stream( Request $request ) { return false; } } while ( $this->event_loop_tick() ); - + return false; }