avoid db session while connecting to peer

This commit is contained in:
j 2019-01-31 14:11:54 +05:30
parent 0216c16c3d
commit 8c0f68e2d5

View file

@ -202,28 +202,34 @@ class Node(Thread):
def _send_response(self): def _send_response(self):
with db.session(): with db.session():
u = user.models.User.get(self.user_id) 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: if DEBUG_NODES:
logger.debug('go online peered=%s queued=%s %s (%s)', u.peered, u.queued, u.id, u.nickname) logger.debug('failed to connect to %s', self.user_id)
try: self.online = False
self.online = self.can_connect() if self.online:
except: if DEBUG_NODES:
logger.debug('connected to %s', self.url)
if user_queued:
if DEBUG_NODES: if DEBUG_NODES:
logger.debug('failed to connect to %s', self.user_id) logger.debug('queued peering event pending=%s peered=%s', user_pending, user_peered)
self.online = False if user_pending == 'sent':
if self.online: self.peering('requestPeering')
if DEBUG_NODES: elif user_pending == '' and user_peered:
logger.debug('connected to %s', self.url) self.peering('acceptPeering')
if u.queued: else:
if DEBUG_NODES: #fixme, what about cancel/reject peering here?
logger.debug('queued peering event pending=%s peered=%s', u.pending, u.peered) self.peering('removePeering')
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')
def trigger_status(self): def trigger_status(self):
if self.online is not None: if self.online is not None:
@ -317,21 +323,26 @@ class Node(Thread):
def peering(self, action): def peering(self, action):
with db.session(): with db.session():
u = user.models.User.get_or_create(self.user_id) u = user.models.User.get_or_create(self.user_id)
if action in ('requestPeering', 'acceptPeering'): user_info = u.info
r = self.request(action, settings.preferences['username'], u.info.get('message')) if action in ('requestPeering', 'acceptPeering'):
else: r = self.request(action, settings.preferences['username'], user_info.get('message'))
r = self.request(action, u.info.get('message')) else:
if r != None: 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 u.queued = False
if 'message' in u.info: if 'message' in u.info:
del u.info['message'] del u.info['message']
u.save() u.save()
else: else:
logger.debug('peering failed? %s %s', action, r) logger.debug('peering failed? %s %s', action, r)
if action in ('cancelPeering', 'rejectPeering', 'removePeering'): if action in ('cancelPeering', 'rejectPeering', 'removePeering'):
self.online = False self.online = False
trigger_event('peering.%s'%action.replace('Peering', ''), u.json()) with db.session():
return True u = user.models.User.get(self.user_id)
trigger_event('peering.%s' % action.replace('Peering', ''), u.json())
return True
headers = { headers = {
'X-Node-Protocol': settings.NODE_PROTOCOL, 'X-Node-Protocol': settings.NODE_PROTOCOL,