Skip to content

Commit 15a7e92

Browse files
exaby73transistive
authored andcommitted
feat: Add explicit close() method to BoltConnection instead of relying on __destruct
1 parent b18c661 commit 15a7e92

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

src/Bolt/BoltConnection.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,23 @@ public function getAuthentication(): AuthenticateInterface
143143
*/
144144
public function isOpen(): bool
145145
{
146-
return !in_array($this->protocol()->serverState, [ServerState::DISCONNECTED, ServerState::DEFUNCT], true);
146+
return !in_array(
147+
$this->protocol()->serverState,
148+
[ServerState::DISCONNECTED, ServerState::DEFUNCT],
149+
true
150+
);
151+
}
152+
153+
/**
154+
* @psalm-mutation-free
155+
*/
156+
public function isStreaming(): bool
157+
{
158+
return in_array(
159+
$this->protocol()->serverState,
160+
[ServerState::STREAMING, ServerState::TX_STREAMING],
161+
true
162+
);
147163
}
148164

149165
public function setTimeout(float $timeout): void
@@ -154,7 +170,8 @@ public function setTimeout(float $timeout): void
154170
public function consumeResults(): void
155171
{
156172
$this->logger?->log(LogLevel::DEBUG, 'Consuming results');
157-
if ($this->protocol()->serverState !== ServerState::STREAMING && $this->protocol()->serverState !== ServerState::TX_STREAMING) {
173+
if ($this->protocol()->serverState !== ServerState::STREAMING && $this->protocol(
174+
)->serverState !== ServerState::TX_STREAMING) {
158175
$this->subscribedResults = [];
159176

160177
return;
@@ -225,8 +242,14 @@ public function discard(?int $qid): void
225242
*
226243
* @return BoltMeta
227244
*/
228-
public function run(string $text, array $parameters, ?string $database, ?float $timeout, BookmarkHolder $holder, ?AccessMode $mode): array
229-
{
245+
public function run(
246+
string $text,
247+
array $parameters,
248+
?string $database,
249+
?float $timeout,
250+
BookmarkHolder $holder,
251+
?AccessMode $mode
252+
): array {
230253
$extra = $this->buildRunExtra($database, $timeout, $holder, $mode);
231254
$this->logger?->log(LogLevel::DEBUG, 'RUN', $extra);
232255
$response = $this->protocol()
@@ -298,10 +321,15 @@ public function pull(?int $qid, ?int $fetchSize): array
298321
}
299322

300323
public function __destruct()
324+
{
325+
$this->close();
326+
}
327+
328+
public function close(): void
301329
{
302330
try {
303331
if ($this->isOpen()) {
304-
if ($this->protocol()->serverState === ServerState::STREAMING || $this->protocol()->serverState === ServerState::TX_STREAMING) {
332+
if ($this->isStreaming()) {
305333
$this->consumeResults();
306334
}
307335

src/Bolt/ConnectionPool.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ private function returnAnyAvailableConnection(SessionConfiguration $config): ?Co
172172

173173
public function close(): void
174174
{
175+
foreach ($this->activeConnections as $activeConnection) {
176+
$activeConnection->close();
177+
}
175178
$this->activeConnections = [];
176179
}
177180
}

src/Contracts/ConnectionInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,9 @@ public function getEncryptionLevel(): string;
112112
* Returns the user agent handling this connection.
113113
*/
114114
public function getUserAgent(): string;
115+
116+
/**
117+
* Closes the connection.
118+
*/
119+
public function close(): void;
115120
}

0 commit comments

Comments
 (0)