Skip to content

Commit

Permalink
修复同一个协程内,HttpClient 被无脑复用的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
limingxinleo committed Mar 28, 2024
1 parent 4060f53 commit 618e28e
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,19 @@ public function withOptions(array $options): static
{
$this->option = array_replace($this->option, $options);
$client = $this->client()->withOptions($options);
Context::set(static::class, $client);
Context::set($this->contextKey(), $client);
return $this;
}

protected function client(): HttpClientInterface
{
return Context::getOrSet(static::class, function () {
return Context::getOrSet($this->contextKey(), function () {
return SymfonyClient::create(RequestUtil::formatDefaultOptions($this->option));
});
}

protected function contextKey(): string
{
return sprintf('%s:%s', static::class, md5(json_encode($this->option, JSON_THROW_ON_ERROR)));
}
}

0 comments on commit 618e28e

Please # to comment.