@@ -288,12 +288,9 @@ def in_use_connection_count(self, address):
288
288
""" Count the number of connections currently in use to a given
289
289
address.
290
290
"""
291
- try :
292
- connections = self .connections [address ]
293
- except KeyError :
294
- return 0
295
- else :
296
- return sum (1 if connection .in_use else 0 for connection in connections )
291
+ with self .lock :
292
+ connections = self .connections .get (address , ())
293
+ return sum (connection .in_use for connection in connections )
297
294
298
295
async def mark_all_stale (self ):
299
296
with self .lock :
@@ -447,7 +444,7 @@ def __init__(self, opener, pool_config, workspace_config, address):
447
444
# Each database have a routing table, the default database is a special case.
448
445
log .debug ("[#0000] C: <NEO4J POOL> routing address %r" , address )
449
446
self .address = address
450
- self .routing_tables = {workspace_config . database : RoutingTable ( database = workspace_config . database , routers = [ address ]) }
447
+ self .routing_tables = {}
451
448
self .refresh_lock = AsyncRLock ()
452
449
453
450
def __repr__ (self ):
@@ -456,37 +453,15 @@ def __repr__(self):
456
453
:return: The representation
457
454
:rtype: str
458
455
"""
459
- return "<{} addresses={!r}>" .format (self .__class__ .__name__ , self .get_default_database_initial_router_addresses ())
460
-
461
- @property
462
- def first_initial_routing_address (self ):
463
- return self .get_default_database_initial_router_addresses ()[0 ]
464
-
465
- def get_default_database_initial_router_addresses (self ):
466
- """ Get the initial router addresses for the default database.
467
-
468
- :return:
469
- :rtype: OrderedSet
470
- """
471
- return self .get_routing_table_for_default_database ().initial_routers
472
-
473
- def get_default_database_router_addresses (self ):
474
- """ Get the router addresses for the default database.
475
-
476
- :return:
477
- :rtype: OrderedSet
478
- """
479
- return self .get_routing_table_for_default_database ().routers
480
-
481
- def get_routing_table_for_default_database (self ):
482
- return self .routing_tables [self .workspace_config .database ]
456
+ return "<{} address={!r}>" .format (self .__class__ .__name__ ,
457
+ self .address )
483
458
484
459
async def get_or_create_routing_table (self , database ):
485
460
async with self .refresh_lock :
486
461
if database not in self .routing_tables :
487
462
self .routing_tables [database ] = RoutingTable (
488
463
database = database ,
489
- routers = self .get_default_database_initial_router_addresses ()
464
+ routers = [ self .address ]
490
465
)
491
466
return self .routing_tables [database ]
492
467
@@ -651,15 +626,15 @@ async def update_routing_table(
651
626
if prefer_initial_routing_address :
652
627
# TODO: Test this state
653
628
if await self ._update_routing_table_from (
654
- self .first_initial_routing_address , database = database ,
629
+ self .address , database = database ,
655
630
imp_user = imp_user , bookmarks = bookmarks ,
656
631
acquisition_timeout = acquisition_timeout ,
657
632
database_callback = database_callback
658
633
):
659
634
# Why is only the first initial routing address used?
660
635
return
661
636
if await self ._update_routing_table_from (
662
- * (existing_routers - {self .first_initial_routing_address }),
637
+ * (existing_routers - {self .address }),
663
638
database = database , imp_user = imp_user , bookmarks = bookmarks ,
664
639
acquisition_timeout = acquisition_timeout ,
665
640
database_callback = database_callback
@@ -668,7 +643,7 @@ async def update_routing_table(
668
643
669
644
if not prefer_initial_routing_address :
670
645
if await self ._update_routing_table_from (
671
- self .first_initial_routing_address , database = database ,
646
+ self .address , database = database ,
672
647
imp_user = imp_user , bookmarks = bookmarks ,
673
648
acquisition_timeout = acquisition_timeout ,
674
649
database_callback = database_callback
@@ -705,6 +680,14 @@ async def ensure_routing_table_is_fresh(
705
680
"""
706
681
from neo4j .api import READ_ACCESS
707
682
async with self .refresh_lock :
683
+ for database_ in list (self .routing_tables .keys ()):
684
+ # Remove unused databases in the routing table
685
+ # Remove the routing table after a timeout = TTL + 30s
686
+ log .debug ("[#0000] C: <ROUTING AGED> database=%s" , database_ )
687
+ routing_table = self .routing_tables [database_ ]
688
+ if routing_table .should_be_purged_from_memory ():
689
+ del self .routing_tables [database_ ]
690
+
708
691
routing_table = await self .get_or_create_routing_table (database )
709
692
if routing_table .is_fresh (readonly = (access_mode == READ_ACCESS )):
710
693
# Readers are fresh.
@@ -717,25 +700,21 @@ async def ensure_routing_table_is_fresh(
717
700
)
718
701
await self .update_connection_pool (database = database )
719
702
720
- for database in list (self .routing_tables .keys ()):
721
- # Remove unused databases in the routing table
722
- # Remove the routing table after a timeout = TTL + 30s
723
- log .debug ("[#0000] C: <ROUTING AGED> database=%s" , database )
724
- if (self .routing_tables [database ].should_be_purged_from_memory ()
725
- and database != self .workspace_config .database ):
726
- del self .routing_tables [database ]
727
-
728
703
return True
729
704
730
705
async def _select_address (self , * , access_mode , database ):
731
706
from ...api import READ_ACCESS
732
707
""" Selects the address with the fewest in-use connections.
733
708
"""
734
709
async with self .refresh_lock :
735
- if access_mode == READ_ACCESS :
736
- addresses = self .routing_tables [database ].readers
710
+ routing_table = self .routing_tables .get (database )
711
+ if routing_table :
712
+ if access_mode == READ_ACCESS :
713
+ addresses = routing_table .readers
714
+ else :
715
+ addresses = routing_table .writers
737
716
else :
738
- addresses = self . routing_tables [ database ]. writers
717
+ addresses = ()
739
718
addresses_by_usage = {}
740
719
for address in addresses :
741
720
addresses_by_usage .setdefault (
@@ -763,13 +742,12 @@ async def acquire(
763
742
764
743
from neo4j .api import check_access_mode
765
744
access_mode = check_access_mode (access_mode )
766
- async with self .refresh_lock :
767
- log .debug ("[#0000] C: <ROUTING TABLE ENSURE FRESH> %r" ,
768
- self .routing_tables )
769
- await self .ensure_routing_table_is_fresh (
770
- access_mode = access_mode , database = database , imp_user = None ,
771
- bookmarks = bookmarks , acquisition_timeout = timeout
772
- )
745
+
746
+ await self .ensure_routing_table_is_fresh (
747
+ access_mode = access_mode , database = database ,
748
+ imp_user = None , bookmarks = bookmarks ,
749
+ acquisition_timeout = timeout
750
+ )
773
751
774
752
while True :
775
753
try :
0 commit comments