-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[4.4] Fix pool closing connections too aggressively (#1038)
Whenever a new routing table was fetched, the pool would close all connections to servers that were not part of the routing table. However, it might well be, that a missing server is present still in the routing table for another database. Hence, the pool now checks the routing tables for all databases before deciding which connections are no longer needed.g
- Loading branch information
1 parent
0823655
commit 249c10b
Showing
10 changed files
with
372 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Copyright (c) "Neo4j" | ||
# Neo4j Sweden AB [http://neo4j.com] | ||
# | ||
# This file is part of Neo4j. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
from .fixtures import * # necessary for pytest to discover the fixtures |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Copyright (c) "Neo4j" | ||
# Neo4j Sweden AB [http://neo4j.com] | ||
# | ||
# This file is part of Neo4j. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
from ._fake_connection import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# Copyright (c) "Neo4j" | ||
# Neo4j Sweden AB [http://neo4j.com] | ||
# | ||
# This file is part of Neo4j. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
import inspect | ||
|
||
import pytest | ||
|
||
from neo4j import ServerInfo | ||
from neo4j._deadline import Deadline | ||
|
||
|
||
__all__ = [ | ||
"fake_connection", | ||
"fake_connection_generator", | ||
] | ||
|
||
|
||
@pytest.fixture | ||
def fake_connection_generator(session_mocker): | ||
mock = session_mocker.mock_module | ||
|
||
class FakeConnection(mock.NonCallableMagicMock): | ||
callbacks = [] | ||
server_info = ServerInfo("127.0.0.1", (4, 3)) | ||
local_port = 1234 | ||
bolt_patches = set() | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.attach_mock(mock.Mock(return_value=True), "is_reset_mock") | ||
self.attach_mock(mock.Mock(return_value=False), "defunct") | ||
self.attach_mock(mock.Mock(return_value=False), "stale") | ||
self.attach_mock(mock.Mock(return_value=False), "closed") | ||
self.attach_mock(mock.Mock(return_value=False), "socket") | ||
self.socket.attach_mock( | ||
mock.Mock(return_value=None), "get_deadline" | ||
) | ||
|
||
def set_deadline_side_effect(deadline): | ||
deadline = Deadline.from_timeout_or_deadline(deadline) | ||
self.socket.get_deadline.return_value = deadline | ||
|
||
self.socket.attach_mock( | ||
mock.Mock(side_effect=set_deadline_side_effect), "set_deadline" | ||
) | ||
|
||
def close_side_effect(): | ||
self.closed.return_value = True | ||
|
||
self.attach_mock(mock.Mock(side_effect=close_side_effect), "close") | ||
|
||
@property | ||
def is_reset(self): | ||
if self.closed.return_value or self.defunct.return_value: | ||
raise AssertionError("is_reset should not be called on a closed or " | ||
"defunct connection.") | ||
return self.is_reset_mock() | ||
|
||
def fetch_message(self, *args, **kwargs): | ||
if self.callbacks: | ||
cb = self.callbacks.pop(0) | ||
cb() | ||
return super().__getattr__("fetch_message")(*args, **kwargs) | ||
|
||
def fetch_all(self, *args, **kwargs): | ||
while self.callbacks: | ||
cb = self.callbacks.pop(0) | ||
cb() | ||
return super().__getattr__("fetch_all")(*args, **kwargs) | ||
|
||
def __getattr__(self, name): | ||
parent = super() | ||
|
||
def build_message_handler(name): | ||
def func(*args, **kwargs): | ||
def callback(): | ||
for cb_name, param_count in ( | ||
("on_success", 1), | ||
("on_summary", 0) | ||
): | ||
cb = kwargs.get(cb_name, None) | ||
if callable(cb): | ||
try: | ||
param_count = \ | ||
len(inspect.signature(cb).parameters) | ||
except ValueError: | ||
# e.g. built-in method as cb | ||
pass | ||
if param_count == 1: | ||
cb({}) | ||
else: | ||
cb() | ||
self.callbacks.append(callback) | ||
|
||
return func | ||
|
||
method_mock = parent.__getattr__(name) | ||
if name in ("run", "commit", "pull", "rollback", "discard"): | ||
method_mock.side_effect = build_message_handler(name) | ||
return method_mock | ||
|
||
return FakeConnection | ||
|
||
|
||
@pytest.fixture | ||
def fake_connection(fake_connection_generator): | ||
return fake_connection_generator() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.