This repository has been archived by the owner on Jun 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThriftUdpTransport.php
90 lines (70 loc) · 2.49 KB
/
ThriftUdpTransport.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
declare(strict_types=1);
namespace OpenTelemetry\Contrib\Jaeger;
use Thrift\Exception\TTransportException;
use Thrift\Transport\TTransport;
class ThriftUdpTransport extends TTransport
{
const MAX_UDP_PACKET = 65000;
protected $server;
protected $port;
protected $socket = null;
protected $buffer = '';
// this implements a TTransport over UDP
public function __construct(ParsedEndpointUrl $parsedEndpoint)
{
$this->server = $parsedEndpoint->getHost();
$this->port = $parsedEndpoint->getPort();
// open a UDP socket to somewhere
if (!($this->socket = \socket_create(AF_INET, SOCK_DGRAM, SOL_UDP))) {
$errorcode = \socket_last_error();
$errormsg = \socket_strerror($errorcode);
error_log("jaeger: transport: Couldn't create socket: [$errorcode] $errormsg");
throw new TTransportException('unable to open UDP socket', TTransportException::UNKNOWN);
}
}
public function isOpen()
{
return $this->socket != null;
}
// Open does nothing as connection is opened on creation
// Required to maintain thrift.TTransport interface
public function open()
{
}
public function close()
{
\socket_close($this->socket);
$this->socket = null;
}
public function read($len)
{
// not implemented
return '';
}
public function write($buf)
{
// ensure that the data will still fit in a UDP packeg
if (strlen($this->buffer) + strlen($buf) > self::MAX_UDP_PACKET) {
throw new TTransportException('Data does not fit within one UDP packet', TTransportException::UNKNOWN);
}
// buffer up some data
$this->buffer .= $buf;
}
public function flush()
{
// no data to send; don't send a packet
if (strlen($this->buffer) == 0) {
return;
}
// TODO(tylerc): This assumes that the whole buffer successfully sent... I believe
// that this should always be the case for UDP packets, but I could be wrong.
// flush the buffer to the socket
if (!\socket_sendto($this->socket, $this->buffer, strlen($this->buffer), 0, $this->server, $this->port)) {
$errorcode = \socket_last_error();
$errormsg = \socket_strerror($errorcode);
error_log("jaeger: transport: Could not flush data: [$errorcode] $errormsg");
}
$this->buffer = ''; // empty the buffer
}
}