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