amphp / amphp/http-client

requestEnd() is not called in event listener in sync mode.

Open
#352 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
731
Forks
69
PR merge metrics
No merged PRs in 30d

Description

I converted my timings collector (based on LogHttpArchive) to the new events system and requestEnd() doesn't get called when the last request has finished.

$client = (new Amp\Http\Client\HttpClientBuilder)
    ->usingPool(Amp\Http\Client\Connection\ConnectionLimitingPool::byAuthority(4))
    ->listen(new HttpRequestTimings)
    ->build();

$semaphore = new Amp\Sync\LocalSemaphore(4);

$futures = [];

foreach ($urls as $url) {
	$futures[] = async(function () use ($client, $semaphore, $url) {
		$request = new Request($url);
		$lock = $semaphore->acquire();
		$response = $client->request($request)->getBody()->buffer();
		$lock->release();
	});
}

await($futures);

At this stage requestEnd() has not been called.

However if I add \Amp\delay(0) after await($futures), then requestEnd() is called.

Is this as intended? Should I somehow await for the HTTP client to finish?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the provided PHP example using HttpClientBuilder, HttpRequestTimings, await($futures), and the added Amp\delay(0). Trace when requestEnd() is dispatched after the final request in sync mode; done means the listener receives requestEnd() without requiring the extra delay, with behavior covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend, networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.