Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Jul 14, 2024
1 parent 4f16d5a commit 97a659b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
20 changes: 7 additions & 13 deletions http_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}


Expand Down
6 changes: 3 additions & 3 deletions src/WordPress/AsyncHttp/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
};
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -222,7 +222,7 @@ public function await_response_stream( Request $request ) {
return false;
}
} while ( $this->event_loop_tick() );

return false;
}

Expand Down

0 comments on commit 97a659b

Please sign in to comment.