Skip to content

Commit

Permalink
Fix Thrift UDP Transport (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-angeli-gimenes authored Dec 30, 2024
1 parent de74ee4 commit 5d014bc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
41 changes: 24 additions & 17 deletions class_map/ThriftUdpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
* @contact [email protected]
* @license https://github.com/opencodeco/hyperf-metric/blob/main/LICENSE
*/

namespace Jaeger;

use Hyperf\Context\ApplicationContext;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Coordinator\Constants;
use Hyperf\Coordinator\CoordinatorManager;
use Hyperf\Coroutine\Coroutine;
use Hyperf\Engine\Channel;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Socket;
use Thrift\Exception\TTransportException;
use Thrift\Transport\TTransport;
use Throwable;

use function Hyperf\Support\env;

class ThriftUdpTransport extends TTransport
{
private null|Socket $socket = null;
Expand All @@ -33,10 +33,8 @@ class ThriftUdpTransport extends TTransport
public function __construct(
private string $host,
private int $port,
private ?LoggerInterface $logger = null
) {
$this->logger = $logger ?? new NullLogger();
}
private LoggerInterface $logger
) {}

/**
* Whether this transport is open.
Expand Down Expand Up @@ -120,9 +118,17 @@ public function write($buf)
$this->loop();
}

$this->chan->push(function () use ($buf) {
$pushed = $this->chan->push(function () use ($buf) {
$this->doWrite($buf);
});
}, (float) env('TRACE_THRIFT_UDP_TIMEOUT', 0.1));

if (! $pushed) {
$this->logger->error('ThriftUdpTransport error:' . match (true) {
$this->chan->isTimeout() => 'Channel Timeout',
$this->chan->isClosing() => 'Channel Close',
default => 'Channel Error'
});
}
}

private function doOpen(): void
Expand Down Expand Up @@ -151,7 +157,14 @@ private function loop(): void
$this->chan = new Channel(1);
Coroutine::create(function () {
while (true) {
$this->doOpen();
try {
$this->doOpen();
} catch (Throwable $e) {
$this->chan->close();
$this->chan = null;
throw $e;
}

while (true) {
try {
$closure = $this->chan->pop();
Expand All @@ -160,13 +173,7 @@ private function loop(): void
}
$closure->call($this);
} catch (Throwable $e) {
if (ApplicationContext::hasContainer()) {
if (ApplicationContext::getContainer()->has(StdoutLoggerInterface::class)) {
ApplicationContext::getContainer()
->get(StdoutLoggerInterface::class)
->error('ThriftUdpTransport error:' . $e->getMessage());
}
}
$this->logger->error('ThriftUdpTransport error:' . $e->getMessage());
@socket_close($this->socket);
$this->socket = null;
break;
Expand Down
5 changes: 2 additions & 3 deletions src/Adapter/JaegerTracerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @contact [email protected]
* @license https://github.com/opencodeco/hyperf-metric/blob/main/LICENSE
*/

namespace Hyperf\Tracer\Adapter;

use Hyperf\Contract\ConfigInterface;
Expand All @@ -26,9 +27,7 @@ class JaegerTracerFactory implements NamedFactoryInterface

private string $name = '';

public function __construct(private ConfigInterface $config, private ?LoggerInterface $logger = null, private ?CacheItemPoolInterface $cache = null)
{
}
public function __construct(private ConfigInterface $config, private LoggerInterface $logger, private ?CacheItemPoolInterface $cache = null) {}

public function make(string $name): Tracer
{
Expand Down

0 comments on commit 5d014bc

Please sign in to comment.