1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\PSR3\Loggly; |
4
|
|
|
|
5
|
|
|
use React\Dns\Resolver\Factory as ResolverFactory; |
6
|
|
|
use React\EventLoop\LoopInterface; |
7
|
|
|
use React\EventLoop\Timer\TimerInterface; |
8
|
|
|
use React\HttpClient\Client; |
9
|
|
|
use React\HttpClient\Factory as HttpClientFactory; |
10
|
|
|
|
11
|
|
|
final class LogglyBulkLogger extends AbstractLogglyLogger |
12
|
|
|
{ |
13
|
|
|
const LF = "\r\n"; |
14
|
|
|
const MAX_BODY_LENGTH = 5242880; |
15
|
|
|
const MAX_LINE_LENGTH = 1048576; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var LoopInterface |
19
|
|
|
*/ |
20
|
|
|
private $loop; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Client |
24
|
|
|
*/ |
25
|
|
|
private $httpClient; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $token; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var float |
34
|
|
|
*/ |
35
|
|
|
private $timeout; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string[] |
39
|
|
|
*/ |
40
|
|
|
private $buffer = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var int |
44
|
|
|
*/ |
45
|
|
|
private $bufferSize = 0; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var TimerInterface |
49
|
|
|
*/ |
50
|
|
|
private $timer; |
51
|
|
|
|
52
|
|
|
public static function create(LoopInterface $loop, string $token, float $timeout = 5.3): self |
53
|
|
|
{ |
54
|
|
|
$resolverFactory = new ResolverFactory(); |
55
|
|
|
$resolver = $resolverFactory->createCached('8.8.8.8', $loop); |
56
|
|
|
|
57
|
|
|
$factory = new HttpClientFactory(); |
58
|
|
|
$httpClient = $factory->create($loop, $resolver); |
59
|
|
|
|
60
|
|
|
return new self($loop, $httpClient, $token, $timeout); |
61
|
|
|
} |
62
|
|
|
|
63
|
12 |
|
public static function createFromHttpClient( |
64
|
|
|
LoopInterface $loop, |
65
|
|
|
Client $httpClient, |
66
|
|
|
string $token, |
67
|
|
|
float $timeout = 5.3 |
68
|
|
|
): self { |
69
|
12 |
|
return new self($loop, $httpClient, $token, $timeout); |
70
|
|
|
} |
71
|
|
|
|
72
|
12 |
|
private function __construct(LoopInterface $loop, Client $httpClient, string $token, float $timeout) |
73
|
|
|
{ |
74
|
12 |
|
$this->loop = $loop; |
75
|
12 |
|
$this->httpClient = $httpClient; |
76
|
12 |
|
$this->token = $token; |
77
|
12 |
|
$this->timeout = $timeout; |
78
|
12 |
|
} |
79
|
|
|
|
80
|
12 |
|
protected function send(string $data) |
81
|
|
|
{ |
82
|
12 |
|
$dataLength = strlen($data . self::LF); |
83
|
12 |
|
if ($dataLength > self::MAX_LINE_LENGTH) { |
84
|
|
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
12 |
|
if ($this->bufferSize + $dataLength > self::MAX_BODY_LENGTH) { |
88
|
|
|
$this->sendBulk(); |
89
|
|
|
} |
90
|
|
|
|
91
|
12 |
|
$this->buffer[] = $data; |
92
|
12 |
|
$this->bufferSize += $dataLength; |
93
|
12 |
|
$this->ensureTimer(); |
94
|
12 |
|
} |
95
|
|
|
|
96
|
12 |
|
private function ensureTimer() |
97
|
|
|
{ |
98
|
12 |
|
if ($this->timer instanceof TimerInterface) { |
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
12 |
|
$this->timer = $this->loop->addTimer($this->timeout, function () { |
103
|
12 |
|
$this->timer = null; |
104
|
12 |
|
$this->sendBulk(); |
105
|
12 |
|
}); |
106
|
12 |
|
} |
107
|
|
|
|
108
|
12 |
|
private function sendBulk() |
109
|
|
|
{ |
110
|
12 |
|
if ($this->timer instanceof TimerInterface) { |
111
|
|
|
$this->timer->cancel(); |
112
|
|
|
$this->timer = null; |
113
|
|
|
} |
114
|
|
|
|
115
|
12 |
|
$data = implode(self::LF, $this->buffer); |
116
|
|
|
|
117
|
12 |
|
$this->buffer = []; |
118
|
12 |
|
$this->bufferSize = 0; |
119
|
|
|
|
120
|
12 |
|
$this->httpClient->request( |
121
|
12 |
|
'POST', |
122
|
12 |
|
'https://logs-01.loggly.com/bulk/' . $this->token, |
123
|
|
|
[ |
124
|
12 |
|
'Content-Type' => 'application/json', |
125
|
12 |
|
'Content-Length' => strlen($data), |
126
|
|
|
], |
127
|
12 |
|
'1.1' |
128
|
12 |
|
)->end($data); |
129
|
12 |
|
} |
130
|
|
|
} |
131
|
|
|
|