Skip to content

Commit b07bc1e

Browse files
committed
Allow passing Client as configuration option.
Prevent calling database creation if Client driver does not support QueryDriverInterface
1 parent 242cd7e commit b07bc1e

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

InfluxDbStorage.php

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,22 @@ public function pushSentMessageStats(SentMessageStats $stats): void
204204

205205
private function doWrite(array $points): void
206206
{
207-
if (!$this->client || $this->client->getDriver() instanceof QueryDriverInterface) {
208-
$this->getDb()->writePoints($points, Database::PRECISION_MILLISECONDS, $this->config['retentionPolicy']);
207+
if (null === $this->client) {
208+
$this->client = new Client(
209+
$this->config['host'],
210+
$this->config['port'],
211+
$this->config['user'],
212+
$this->config['password']
213+
);
214+
}
215+
216+
if ($this->client->getDriver() instanceof QueryDriverInterface) {
217+
if (null === $this->database) {
218+
$this->database = $this->client->selectDB($this->config['db']);
219+
$this->database->create();
220+
}
221+
222+
$this->database->writePoints($points, Database::PRECISION_MILLISECONDS, $this->config['retentionPolicy']);
209223
} else {
210224
// Code below mirrors what `writePoints` method of Database does.
211225
try {
@@ -225,25 +239,6 @@ private function doWrite(array $points): void
225239
}
226240
}
227241

228-
private function getDb(): Database
229-
{
230-
if (null === $this->client) {
231-
$this->client = new Client(
232-
$this->config['host'],
233-
$this->config['port'],
234-
$this->config['user'],
235-
$this->config['password']
236-
);
237-
}
238-
239-
if (null === $this->database) {
240-
$this->database = $this->client->selectDB($this->config['db']);
241-
$this->database->create();
242-
}
243-
244-
return $this->database;
245-
}
246-
247242
private static function parseDsn(string $dsn): array
248243
{
249244
$dsn = Dsn::parseFirst($dsn);

0 commit comments

Comments
 (0)