[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tracer): swoole v4.6 SWOOLE_HOOK_SOCKETS conflicts with jaeger tr… #3126

Merged
merged 4 commits into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [#3106](https://github.com/hyperf/hyperf/pull/3106) Fixed bug that call to a member function getArrayCopy() on null when the parent coroutine context destroyed.
- [#3108](https://github.com/hyperf/hyperf/pull/3108) Fixed routes will be replaced by another group when using `describe:routes` command.
- [#3118](https://github.com/hyperf/hyperf/pull/3118) Fixed bug that the config key of migrations is not correct.
- [#3126](https://github.com/hyperf/hyperf/pull/3126) Fixed bug that swoole v4.6 `SWOOLE_HOOK_SOCKETS` conflicts with jaeger tracing.

## Added

Expand Down
200 changes: 200 additions & 0 deletions src/tracer/class_map/ThriftUdpTransport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Jaeger;

use Hyperf\Engine\Channel;
use Hyperf\Utils\Coordinator\Constants;
use Hyperf\Utils\Coordinator\CoordinatorManager;
use Hyperf\Utils\Coroutine;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Thrift\Exception\TTransportException;
use Thrift\Transport\TTransport;

class ThriftUdpTransport extends TTransport
{
/**
* @var string
*/
private $host;

/**
* @var int
*/
private $port;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @var ?resource
*/
private $socket;

/**
* @var ?Channel
*/
private $chan;

/**
* ThriftUdpTransport constructor.
* @param LoggerInterface $logger
*/
public function __construct(string $host, int $port, LoggerInterface $logger = null)
{
$this->host = $host;
$this->port = $port;
$this->logger = $logger ?? new NullLogger();
}

/**
* Whether this transport is open.
*
* @return bool true if open
*/
public function isOpen()
{
return $this->socket !== null;
}

/**
* Open the transport for reading/writing.
*
* @throws TTransportException if cannot open
*/
public function open()
{
if (! Coroutine::inCoroutine()) {
$this->doOpen();
return;
}

if (! $this->chan) {
$this->loop();
return;
}

$this->chan->push(function () {
$this->doOpen();
});
}

/**
* Close the transport.
*/
public function close()
{
if (! Coroutine::inCoroutine()) {
@socket_close($this->socket);
$this->socket = null;
return;
}

if (! $this->chan) {
$this->loop();
}

$this->chan->push(function () {
@socket_close($this->socket);
$this->socket = null;
});
}

/**
* Read some data into the array.
*
* @todo
*
* @param int $len How much to read
* @return string The data that has been read
*/
public function read($len)
{
return '';
}

/**
* Writes the given data out.
*
* @param string $buf The data to write
* @throws TTransportException if writing fails
*/
public function write($buf)
{
if (! Coroutine::inCoroutine()) {
$this->doWrite($buf);
}

if (! $this->chan) {
$this->loop();
}

$this->chan->push(function () use ($buf) {
$this->doWrite($buf);
});
}

private function doOpen(): void
{
$this->socket = @socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$ok = @socket_connect($this->socket, $this->host, $this->port);
if ($ok === false) {
throw new TTransportException('socket_connect failed');
}
}

private function doWrite(string $buf): void
{
if (! $this->isOpen()) {
throw new TTransportException('transport is closed');
}

$ok = @socket_write($this->socket, $buf);
if ($ok === false) {
throw new TTransportException('socket_write failed');
}
}

private function loop(): void
{
$this->chan = new Channel(1);
Coroutine::create(function () {
try {
$this->doOpen();
while (true) {
$closure = $this->chan->pop();
if (! $closure) {
break;
}
$closure->call($this);
}
} finally {
@socket_close($this->socket);
$this->chan = null;
$this->socket = null;
}
});

static $once;
if (! isset($once)) {
$once = true;
Coroutine::create(function () {
CoordinatorManager::until(Constants::WORKER_EXIT)->yield();
if ($this->chan) {
$this->chan->close();
}
});
}
}
}
4 changes: 4 additions & 0 deletions src/tracer/src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use GuzzleHttp\Client;
use Hyperf\Tracer\Listener\DbQueryExecutedListener;
use Jaeger\ThriftUdpTransport;
use OpenTracing\Tracer;

class ConfigProvider
Expand All @@ -34,6 +35,9 @@ public function __invoke(): array
'paths' => [
__DIR__,
],
'class_map' => [
ThriftUdpTransport::class => __DIR__ . '/../class_map/ThriftUdpTransport.php',
],
],
],
'publish' => [
Expand Down