Skip to content

Commit 96fcb55

Browse files
author
Zhen Li
authored
Merge pull request #105 from neo4j/1.1-routing-spec
Align routing to specification
2 parents f7b74e4 + 56d74a5 commit 96fcb55

19 files changed

+1215
-399
lines changed

neo4j/__init__.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,3 @@
2020

2121

2222
from .meta import version as __version__
23-
24-
# Export current (v1) API. This should be updated to export the latest
25-
# version of the API when a new one is added. This gives the option to
26-
# `import neo4j.vX` for a specific version or `import neo4j` for the
27-
# latest.
28-
from .v1.constants import *
29-
from .v1.exceptions import *
30-
from .v1.session import *
31-
from .v1.types import *

neo4j/util.py

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -94,61 +94,3 @@ def watch(logger_name, level=logging.INFO, out=stdout):
9494
watcher = Watcher(logger_name)
9595
watcher.watch(level, out)
9696
return watcher
97-
98-
99-
class RoundRobinSet(MutableSet):
100-
101-
def __init__(self, elements=()):
102-
self._elements = OrderedDict.fromkeys(elements)
103-
self._current = None
104-
105-
def __repr__(self):
106-
return "{%s}" % ", ".join(map(repr, self._elements))
107-
108-
def __contains__(self, element):
109-
return element in self._elements
110-
111-
def __next__(self):
112-
current = None
113-
if self._elements:
114-
if self._current is None:
115-
self._current = 0
116-
else:
117-
self._current = (self._current + 1) % len(self._elements)
118-
current = list(self._elements.keys())[self._current]
119-
return current
120-
121-
def __iter__(self):
122-
return iter(self._elements)
123-
124-
def __len__(self):
125-
return len(self._elements)
126-
127-
def add(self, element):
128-
self._elements[element] = None
129-
130-
def clear(self):
131-
self._elements.clear()
132-
133-
def discard(self, element):
134-
try:
135-
del self._elements[element]
136-
except KeyError:
137-
pass
138-
139-
def next(self):
140-
return self.__next__()
141-
142-
def remove(self, element):
143-
try:
144-
del self._elements[element]
145-
except KeyError:
146-
raise ValueError(element)
147-
148-
def update(self, elements=()):
149-
self._elements.update(OrderedDict.fromkeys(elements))
150-
151-
def replace(self, elements=()):
152-
e = self._elements
153-
e.clear()
154-
e.update(OrderedDict.fromkeys(elements))

neo4j/v1/bolt.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from select import select
3838
from socket import create_connection, SHUT_RDWR, error as SocketError
3939
from struct import pack as struct_pack, unpack as struct_unpack, unpack_from as struct_unpack_from
40-
from threading import Lock
40+
from threading import RLock
4141

4242
from .constants import DEFAULT_USER_AGENT, KNOWN_HOSTS, MAGIC_PREAMBLE, TRUST_DEFAULT, TRUST_ON_FIRST_USE
4343
from .exceptions import ProtocolError, Unauthorized, ServiceUnavailable
@@ -378,15 +378,26 @@ class ConnectionPool(object):
378378
""" A collection of connections to one or more server addresses.
379379
"""
380380

381+
closed = False
382+
381383
def __init__(self, connector):
382384
self.connector = connector
383385
self.connections = {}
384-
self.lock = Lock()
386+
self.lock = RLock()
387+
388+
def __enter__(self):
389+
return self
390+
391+
def __exit__(self, exc_type, exc_value, traceback):
392+
self.close()
385393

386394
def acquire(self, address):
387395
""" Acquire a connection to a given address from the pool.
388396
This method is thread safe.
389397
"""
398+
if self.closed:
399+
raise ServiceUnavailable("This connection pool is closed so no new "
400+
"connections may be acquired")
390401
with self.lock:
391402
try:
392403
connections = self.connections[address]
@@ -411,18 +422,25 @@ def release(self, connection):
411422
with self.lock:
412423
connection.in_use = False
413424

425+
def remove(self, address):
426+
""" Remove an address from the connection pool, if present, closing
427+
all connections to that address.
428+
"""
429+
with self.lock:
430+
for connection in self.connections.pop(address, ()):
431+
try:
432+
connection.close()
433+
except IOError:
434+
pass
435+
414436
def close(self):
415437
""" Close all connections and empty the pool.
416438
This method is thread safe.
417439
"""
418440
with self.lock:
419-
for _, connections in self.connections.items():
420-
for connection in connections:
421-
try:
422-
connection.close()
423-
except IOError:
424-
pass
425-
self.connections.clear()
441+
self.closed = True
442+
for address in list(self.connections):
443+
self.remove(address)
426444

427445

428446
class CertificateStore(object):

neo4j/v1/compat.py renamed to neo4j/v1/compat/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
excluded from test coverage.
2626
"""
2727

28-
__all__ = ["integer", "perf_counter", "secure_socket", "string", "urlparse"]
29-
3028

3129
# Workaround for Python 2/3 type differences
3230
try:

neo4j/v1/compat/collections.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
4+
# Copyright (c) 2002-2016 "Neo Technology,"
5+
# Network Engine for Objects in Lund AB [http://neotechnology.com]
6+
#
7+
# This file is part of Neo4j.
8+
#
9+
# Licensed under the Apache License, Version 2.0 (the "License");
10+
# you may not use this file except in compliance with the License.
11+
# You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS,
17+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
# See the License for the specific language governing permissions and
19+
# limitations under the License.
20+
21+
from __future__ import absolute_import
22+
23+
try:
24+
from collections.abc import MutableSet
25+
except ImportError:
26+
from collections import MutableSet, OrderedDict
27+
else:
28+
from collections import OrderedDict

0 commit comments

Comments
 (0)