Skip to content

Commit 72c0866

Browse files
committed
Expose connection states in stats api
1 parent ecaf57d commit 72c0866

3 files changed

Lines changed: 24 additions & 9 deletions

File tree

syncrypt/api/__init__.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
import asyncio
12
import json
23
import logging
34

4-
import asyncio
55
from aiohttp import web
6+
67
import syncrypt
78
from syncrypt.app.auth import CredentialsAuthenticationProvider
89
from 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
1312
from ..utils.updates import is_update_available
13+
from .auth import generate_api_auth_token, require_auth_token
1414
from .client import APIClient
15+
from .resources import (BundleResource, FlyingVaultResource, JSONResponse,
16+
UserResource, VaultResource, VaultUserResource)
1517

1618
logger = 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

syncrypt/backends/binary.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff 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()

tests/test_binary.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff 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

211215
if __name__ == '__main__':
212216
from syncrypt.utils.logging import setup_logging

0 commit comments

Comments
 (0)