File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import asyncio
12import json
23import logging
34
4- import asyncio
55from aiohttp import web
6+
67import syncrypt
78from syncrypt .app .auth import CredentialsAuthenticationProvider
89from syncrypt .backends .base import StorageBackendInvalidAuth
10+ from syncrypt .backends .binary import get_manager_instance
911
10- from .resources import (BundleResource , JSONResponse , VaultResource ,
11- VaultUserResource , UserResource , FlyingVaultResource )
12- from .auth import generate_api_auth_token , require_auth_token
1312from ..utils .updates import is_update_available
13+ from .auth import generate_api_auth_token , require_auth_token
1414from .client import APIClient
15+ from .resources import (BundleResource , FlyingVaultResource , JSONResponse ,
16+ UserResource , VaultResource , VaultUserResource )
1517
1618logger = logging .getLogger (__name__ )
1719
@@ -32,7 +34,8 @@ def get_stats(self, request):
3234 vault_states = {vault_resource .get_resource_uri (v ): v .state for v in self .app .vaults }
3335 return JSONResponse ({
3436 'stats' : self .app .stats ,
35- 'states' : vault_states
37+ 'states' : vault_states ,
38+ 'slots' : get_manager_instance ().get_stats ()
3639 })
3740
3841 @asyncio .coroutine
Original file line number Diff line number Diff line change @@ -728,8 +728,15 @@ def init(self):
728728 self .concurrency , id (asyncio .get_event_loop ()))
729729 self .slots = [BinaryStorageConnection (self ) for i in range (self .concurrency )]
730730
731- def get_active_connection_count (self ):
732- return len ([conn for conn in self .slots if conn .connected or conn .connecting ])
731+ def get_stats (self ):
732+ states = {}
733+ if self .slots :
734+ for conn in self .slots :
735+ states [conn .state ] = states .get (conn .state , 0 ) + 1
736+ states ['total' ] = len (self .slots )
737+ else :
738+ states ['total' ] = 0
739+ return states
733740
734741 @asyncio .coroutine
735742 def close (self ):
@@ -886,7 +893,8 @@ def init(self):
886893 def open (self ):
887894 if self .vault and not self .vault .config .get ('vault.id' ):
888895 raise VaultNotInitialized ()
889- if get_manager_instance ().get_active_connection_count () == 0 :
896+ stats = get_manager_instance ().get_stats ()
897+ if stats ['closed' ] == stats ['total' ]:
890898 with (yield from self ._acquire_connection ()) as conn :
891899 self .invalid_auth = False
892900 version = yield from conn .version ()
Original file line number Diff line number Diff line change @@ -195,6 +195,7 @@ def test_revision_increase_after_push(self):
195195 self .assertTrue (not post_rev is None )
196196
197197 def test_take_only_one_connection (self ):
198+ 'this will test if connection slots are properly reused'
198199 vault = self .vault
199200
200201 app = SyncryptApp (self .app_config )
@@ -206,7 +207,10 @@ def test_take_only_one_connection(self):
206207 yield from app .get_remote_size_for_vault (vault )
207208 yield from app .get_remote_size_for_vault (vault )
208209
209- self .assertEqual (get_manager_instance ().get_active_connection_count (), 1 )
210+ print (get_manager_instance ().get_stats ())
211+
212+ self .assertEqual (get_manager_instance ().get_stats ()['idle' ], 1 )
213+ self .assertEqual (get_manager_instance ().get_stats ()['busy' ], 0 )
210214
211215if __name__ == '__main__' :
212216 from syncrypt .utils .logging import setup_logging
You can’t perform that action at this time.
0 commit comments