From 8c0f68e2d553dc3eb81411a9879d9586353e83a3 Mon Sep 17 00:00:00 2001 From: j Date: Thu, 31 Jan 2019 14:11:54 +0530 Subject: [PATCH] avoid db session while connecting to peer --- oml/nodes.py | 73 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 42 insertions(+), 31 deletions(-) diff --git a/oml/nodes.py b/oml/nodes.py index f9b3ff2..42f1703 100644 --- a/oml/nodes.py +++ b/oml/nodes.py @@ -202,28 +202,34 @@ class Node(Thread): def _send_response(self): with db.session(): u = user.models.User.get(self.user_id) - if u and u.peered or u.queued: + send_response = u and u.peered or u.queued + if u: + user_pending = u.pending + user_peered = u.peered + user_queued = u.queued + if DEBUG_NODES: + logger.debug('go online peered=%s queued=%s %s (%s)', u.peered, u.queued, u.id, u.nickname) + + if send_response: + try: + self.online = self.can_connect() + except: if DEBUG_NODES: - logger.debug('go online peered=%s queued=%s %s (%s)', u.peered, u.queued, u.id, u.nickname) - try: - self.online = self.can_connect() - except: + logger.debug('failed to connect to %s', self.user_id) + self.online = False + if self.online: + if DEBUG_NODES: + logger.debug('connected to %s', self.url) + if user_queued: if DEBUG_NODES: - logger.debug('failed to connect to %s', self.user_id) - self.online = False - if self.online: - if DEBUG_NODES: - logger.debug('connected to %s', self.url) - if u.queued: - if DEBUG_NODES: - logger.debug('queued peering event pending=%s peered=%s', u.pending, u.peered) - if u.pending == 'sent': - self.peering('requestPeering') - elif u.pending == '' and u.peered: - self.peering('acceptPeering') - else: - #fixme, what about cancel/reject peering here? - self.peering('removePeering') + logger.debug('queued peering event pending=%s peered=%s', user_pending, user_peered) + if user_pending == 'sent': + self.peering('requestPeering') + elif user_pending == '' and user_peered: + self.peering('acceptPeering') + else: + #fixme, what about cancel/reject peering here? + self.peering('removePeering') def trigger_status(self): if self.online is not None: @@ -317,21 +323,26 @@ class Node(Thread): def peering(self, action): with db.session(): u = user.models.User.get_or_create(self.user_id) - if action in ('requestPeering', 'acceptPeering'): - r = self.request(action, settings.preferences['username'], u.info.get('message')) - else: - r = self.request(action, u.info.get('message')) - if r != None: + user_info = u.info + if action in ('requestPeering', 'acceptPeering'): + r = self.request(action, settings.preferences['username'], user_info.get('message')) + else: + r = self.request(action, user_info.get('message')) + if r is not None: + with db.session(): + u = user.models.User.get(self.user_id) u.queued = False if 'message' in u.info: del u.info['message'] u.save() - else: - logger.debug('peering failed? %s %s', action, r) - if action in ('cancelPeering', 'rejectPeering', 'removePeering'): - self.online = False - trigger_event('peering.%s'%action.replace('Peering', ''), u.json()) - return True + else: + logger.debug('peering failed? %s %s', action, r) + if action in ('cancelPeering', 'rejectPeering', 'removePeering'): + self.online = False + with db.session(): + u = user.models.User.get(self.user_id) + trigger_event('peering.%s' % action.replace('Peering', ''), u.json()) + return True headers = { 'X-Node-Protocol': settings.NODE_PROTOCOL,