Commit 29d4a051 authored by mar-ar's avatar mar-ar Committed by oroulet

limit the max amount of connections (#114)

* now you can limit the max amount of connections

* changed the discussed code
parent 98fd52a1
......@@ -20,6 +20,8 @@ class InternalSession:
"""
"""
max_connections = 1000
_current_connections = 0
_counter = 10
_auth_counter = 1000
......@@ -45,6 +47,7 @@ class InternalSession:
def __str__(self):
return f'InternalSession(name:{self.name}, user:{self.user}, id:{self.session_id}, auth_token:{self.auth_token})'
async def get_endpoints(self, params=None, sockname=None):
return await self.iserver.get_endpoints(params, sockname)
......@@ -63,6 +66,10 @@ class InternalSession:
async def close_session(self, delete_subs=True):
self.logger.info('close session %s', self.name)
if self.state == SessionState.Activated:
InternalSession._current_connections -= 1
if InternalSession._current_connections < 0:
InternalSession._current_connections = 0
self.state = SessionState.Closed
await self.delete_subscriptions(self.subscriptions)
......@@ -71,11 +78,14 @@ class InternalSession:
result = ua.ActivateSessionResult()
if self.state != SessionState.Created:
raise ServiceError(ua.StatusCodes.BadSessionIdInvalid)
if InternalSession._current_connections >= InternalSession.max_connections:
raise ServiceError(ua.StatusCodes.BadMaxConnectionsReached)
self.nonce = create_nonce(32)
result.ServerNonce = self.nonce
for _ in params.ClientSoftwareCertificates:
result.Results.append(ua.StatusCode())
self.state = SessionState.Activated
InternalSession._current_connections += 1
id_token = params.UserIdentityToken
if isinstance(id_token, ua.UserNameIdentityToken):
if self.iserver.check_user_token(self, id_token) is False:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment