-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adds AsyncHttpClient * Update ClientBuilderFactory and Version classes * Refactor AsyncHttpClient to use arrow function * Fix formatting in AsyncHttpClient * Update Sentry HttpClientInterface implementation * Remove useless codes * Add Hyperf\Engine\Channel import to HttpClient.php * Refactor HttpClient and HttpClientFactory * Add http_client_chanel_size and http_client_concurrent_limit configuration options * Add http options * Refactor HttpClient.php to use Hyperf\Engine\Coroutine instead of Hyperf\Coroutine\Coroutine --------- Co-authored-by: Deeka Wong <[email protected]>
- Loading branch information
1 parent
998d02d
commit 1c880f2
Showing
6 changed files
with
162 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of friendsofhyperf/components. | ||
* | ||
* @link https://github.com/friendsofhyperf/components | ||
* @document https://github.com/friendsofhyperf/components/blob/main/README.md | ||
* @contact [email protected] | ||
*/ | ||
|
||
namespace FriendsOfHyperf\Sentry\HttpClient; | ||
|
||
use Closure; | ||
use Hyperf\Coordinator\Constants; | ||
use Hyperf\Coordinator\CoordinatorManager; | ||
use Hyperf\Coroutine\Concurrent; | ||
use Hyperf\Engine\Channel; | ||
use Hyperf\Engine\Coroutine; | ||
use Sentry\HttpClient\Request; | ||
use Sentry\HttpClient\Response; | ||
use Sentry\Options; | ||
use Throwable; | ||
|
||
class HttpClient extends \Sentry\HttpClient\HttpClient | ||
{ | ||
protected ?Channel $chan = null; | ||
|
||
protected ?Concurrent $concurrent = null; | ||
|
||
public function __construct( | ||
string $sdkIdentifier, | ||
string $sdkVersion, | ||
protected int $channelSize = 65535, | ||
int $concurrentLimit = 100, | ||
) { | ||
parent::__construct($sdkIdentifier, $sdkVersion); | ||
|
||
if ($concurrentLimit > 0) { | ||
$this->concurrent = new Concurrent($concurrentLimit); | ||
} | ||
} | ||
|
||
public function sendRequest(Request $request, Options $options): Response | ||
{ | ||
$this->loop(); | ||
|
||
$chan = $this->chan; | ||
$chan->push(fn () => parent::sendRequest($request, $options)); | ||
|
||
return new Response(202, [], 'Waiting for sendRequest'); | ||
} | ||
|
||
public function close(): void | ||
{ | ||
$chan = $this->chan; | ||
$this->chan = null; | ||
|
||
$chan?->close(); | ||
} | ||
|
||
protected function loop(): void | ||
{ | ||
if ($this->chan != null) { | ||
return; | ||
} | ||
|
||
$this->chan = new Channel($this->channelSize); | ||
|
||
Coroutine::create(function () { | ||
while (true) { | ||
while (true) { | ||
/** @var Closure|null $closure */ | ||
$closure = $this->chan?->pop(); | ||
if (! $closure) { | ||
break 2; | ||
} | ||
try { | ||
if ($this->concurrent) { | ||
$this->concurrent->create($closure); | ||
} else { | ||
Coroutine::create($closure); | ||
} | ||
} catch (Throwable) { | ||
break; | ||
} finally { | ||
$closure = null; | ||
} | ||
} | ||
} | ||
|
||
$this->close(); | ||
}); | ||
|
||
Coroutine::create(function () { | ||
if (CoordinatorManager::until(Constants::WORKER_EXIT)->yield()) { | ||
$this->close(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of friendsofhyperf/components. | ||
* | ||
* @link https://github.com/friendsofhyperf/components | ||
* @document https://github.com/friendsofhyperf/components/blob/main/README.md | ||
* @contact [email protected] | ||
*/ | ||
|
||
namespace FriendsOfHyperf\Sentry\HttpClient; | ||
|
||
use FriendsOfHyperf\Sentry\Version; | ||
use Hyperf\Contract\ConfigInterface; | ||
use Psr\Container\ContainerInterface; | ||
|
||
class HttpClientFactory | ||
{ | ||
public function __invoke(ContainerInterface $container) | ||
{ | ||
$config = $container->get(ConfigInterface::class); | ||
return new HttpClient( | ||
Version::getSdkIdentifier(), | ||
Version::getSdkVersion(), | ||
(int) $config->get('sentry.http_chanel_size', 65535), | ||
(int) $config->get('sentry.http_concurrent_limit', 100) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters