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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
<?php
namespace gipfl\InfluxDb;
use gipfl\Curl\RequestError;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;
use React\EventLoop\LoopInterface;
use React\EventLoop\TimerInterface;
/**
* Gives no result, enqueue and forget
*/
class ChunkedInfluxDbWriter implements LoggerAwareInterface
{
use LoggerAwareTrait;
const DEFAULT_BUFFER_SIZE = 5000;
const DEFAULT_FLUSH_INTERVAL = 0.2;
const DEFAULT_PRECISION = 's';
/** @var int */
protected $bufferSize = self::DEFAULT_BUFFER_SIZE;
/** @var float */
protected $flushInterval = self::DEFAULT_FLUSH_INTERVAL;
/** @var string */
protected $precision = self::DEFAULT_PRECISION;
/** @var DataPoint[] */
protected $buffer = [];
/** @var InfluxDbConnection */
protected $connection;
/** @var string */
protected $dbName;
/** @var LoopInterface */
protected $loop;
/** @var ?TimerInterface */
protected $flushTimer;
public function __construct(InfluxDbConnection $connection, $dbName, LoopInterface $loop)
{
$this->setLogger(new NullLogger());
$this->connection = $connection;
$this->dbName = $dbName;
$this->loop = $loop;
}
/**
* @param DataPoint $point
*/
public function enqueue(DataPoint $point)
{
$this->buffer[] = $point;
$count = count($this->buffer);
if ($count >= $this->bufferSize) {
$this->flush();
} else {
$this->startFlushTimer();
}
}
/**
* @param int $bufferSize
* @return ChunkedInfluxDbWriter
*/
public function setBufferSize($bufferSize)
{
$this->bufferSize = $bufferSize;
return $this;
}
/**
* @param float $flushInterval
* @return ChunkedInfluxDbWriter
*/
public function setFlushInterval($flushInterval)
{
$this->flushInterval = $flushInterval;
return $this;
}
/**
* @param string $precision ns,u,ms,s,m,h
* @return ChunkedInfluxDbWriter
*/
public function setPrecision($precision)
{
$this->precision = $precision;
return $this;
}
public function flush()
{
$buffer = $this->buffer;
$this->buffer = [];
$this->stopFlushTimer();
$this->logger->debug(sprintf('Flushing InfluxDB buffer, sending %d data points', count($buffer)));
$start = microtime(true);
$this->connection->writeDataPoints($this->dbName, $buffer, $this->precision)
->then(function (ResponseInterface $response) use ($start) {
$code = $response->getStatusCode();
$duration = (microtime(true) - $start) * 1000;
if ($code > 199 && $code < 300) {
$this->logger->debug(sprintf('Got response from InfluxDB after %.2Fms', $duration));
} else {
$this->logger->error(sprintf(
'Got unexpected %d from InfluxDB after %.2Fms: %s',
$code,
$duration,
$response->getReasonPhrase()
));
}
}, function (RequestError $e) {
$this->logger->error($e->getMessage());
})->done();
}
public function stop()
{
$this->flush();
}
protected function startFlushTimer()
{
if ($this->flushTimer === null) {
$this->flushTimer = $this->loop->addPeriodicTimer($this->flushInterval, function () {
if (! empty($this->buffer)) {
$this->flush();
}
});
}
}
protected function stopFlushTimer()
{
if ($this->flushTimer) {
$this->loop->cancelTimer($this->flushTimer);
$this->flushTimer = null;
}
}
public function __destruct()
{
$this->stopFlushTimer();
$this->loop = null;
$this->connection = null;
}
}
|