Skip to content

Commit 84b850b

Browse files
committed
feat: Add ability to close all connections attached to a driver
1 parent 907d539 commit 84b850b

File tree

9 files changed

+55
-2
lines changed

9 files changed

+55
-2
lines changed

src/Basic/Driver.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,9 @@ public static function create(string|UriInterface $uri, ?DriverConfiguration $co
5757

5858
return new self($driver);
5959
}
60+
61+
public function closeConnections(): void
62+
{
63+
$this->driver->closeConnections();
64+
}
6065
}

src/Bolt/BoltDriver.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,9 @@ public function verifyConnectivity(?SessionConfiguration $config = null): bool
109109

110110
return true;
111111
}
112+
113+
public function closeConnections(): void
114+
{
115+
$this->pool->close();
116+
}
112117
}

src/Bolt/ConnectionPool.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ public function __construct(
4646
private readonly ?Neo4jLogger $logger
4747
) {}
4848

49-
public static function create(UriInterface $uri, AuthenticateInterface $auth, DriverConfiguration $conf, SemaphoreInterface $semaphore): self
50-
{
49+
public static function create(
50+
UriInterface $uri,
51+
AuthenticateInterface $auth,
52+
DriverConfiguration $conf,
53+
SemaphoreInterface $semaphore
54+
): self {
5155
return new self(
5256
$semaphore,
5357
BoltFactory::create($conf->getLogger()),
@@ -165,4 +169,9 @@ private function returnAnyAvailableConnection(SessionConfiguration $config): ?Co
165169

166170
return null;
167171
}
172+
173+
public function close(): void
174+
{
175+
$this->activeConnections = [];
176+
}
168177
}

src/Contracts/ConnectionPoolInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,9 @@ public function acquire(SessionConfiguration $config): Generator;
4444
* Releases a connection back to the pool.
4545
*/
4646
public function release(ConnectionInterface $connection): void;
47+
48+
/**
49+
* Closes all connections in the pool.
50+
*/
51+
public function close(): void;
4752
}

src/Contracts/DriverInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,9 @@ public function createSession(?SessionConfiguration $config = null): SessionInte
3838
* Returns true if the driver can make a valid connection with the server.
3939
*/
4040
public function verifyConnectivity(?SessionConfiguration $config = null): bool;
41+
42+
/**
43+
* Closes all connections in the pool.
44+
*/
45+
public function closeConnections(): void;
4146
}

src/Http/HttpConnectionPool.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,9 @@ public function release(ConnectionInterface $connection): void
127127
{
128128
// Nothing to release in the current HTTP Protocol implementation
129129
}
130+
131+
public function close(): void
132+
{
133+
// Nothing to close in the current HTTP Protocol implementation
134+
}
130135
}

src/Http/HttpDriver.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,9 @@ private function tsxUrl(SessionConfiguration $config): Resolvable
198198
return str_replace('{databaseName}', $database, $tsx);
199199
});
200200
}
201+
202+
public function closeConnections(): void
203+
{
204+
// Nothing to close in the current HTTP Protocol implementation
205+
}
201206
}

src/Neo4j/Neo4jConnectionPool.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,13 @@ private function createKey(ConnectionRequestData $data, ?SessionConfiguration $c
233233
':',
234234
], '|', $key);
235235
}
236+
237+
public function close(): void
238+
{
239+
foreach (self::$pools as $pool) {
240+
$pool->close();
241+
}
242+
self::$pools = [];
243+
$this->cache->clear();
244+
}
236245
}

src/Neo4j/Neo4jDriver.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,9 @@ public function verifyConnectivity(?SessionConfiguration $config = null): bool
111111

112112
return true;
113113
}
114+
115+
public function closeConnections(): void
116+
{
117+
$this->pool->close();
118+
}
114119
}

0 commit comments

Comments
 (0)