Permalink
Browse files

discard all connections on first retry failure -- they should all be …

…stale
  • Loading branch information...
analogue committed Jan 17, 2012
1 parent d88964d commit 9035c7e9de0a313bf4fb6abb2bbe1622e8a76670
Showing with 23 additions and 9 deletions.
  1. +13 −9 resources/src/mythbox/mythtv/conn.py
  2. +10 −0 resources/src/mythbox/pool.py
@@ -89,7 +89,7 @@ def conn_accessor():
def checkout():
'''Only acquire resource once per thread'''
threadlocals[tlsKey].level += 1
- if threadlocals[tlsKey].level == 1:
+ if threadlocals[tlsKey].level == 1 or threadlocals[tlsKey].discarded:
# not acquired - store conn in thread local storage
threadlocals[tlsKey].conn = connPool.checkout()
threadlocals[tlsKey].discarded = False
@@ -107,20 +107,24 @@ def discard():
# discard connections that have timed out since we no longer know if they are usable/functional
ilog.error('\n\n\t\tSocket timed out. Discarding conn on thread %s\n\n' % tlsKey)
try:
- connPool.discard(threadlocals[tlsKey].conn)
+ connPool.discard_all(threadlocals[tlsKey].conn)
except Exception, e:
log.warn('While discarding: %s', str(e))
- threadlocals[tlsKey].conn = None
- threadlocals[tlsKey].discarded = True
del threadlocals[tlsKey]
else:
ilog.error('Conn %s already discarded. Skipping..' % tlsKey)
def tryAgain():
log.debug('-- TRY AGAIN BEGIN --')
- ta_result = inject_conn(func)
- log.debug('-- TRY AGAIN END --')
+
+ try:
+ allocate_threadlocal()
+ checkout()
+ ta_result = func(*args, **kwargs)
+ finally:
+ checkin()
+ log.debug('-- TRY AGAIN END --')
return ta_result
@@ -133,15 +137,15 @@ def shouldGiveUp(error):
def shouldReconnect(error):
# 104 - Connection reset by peer
# 32 - Broken pipe
- # [Errno 54] Connection reset by peer
- # 9, 'Bad file descriptor'
+ # 54 - Connection reset by peer
+ # 9 - Bad file descriptor
return error.errno in (104, 32, 54, 9)
def checkin():
if threadlocals[tlsKey].level == 1:
if not threadlocals[tlsKey].discarded:
- ilog.debug('--> removed conn %s from %s at level %d' % (threadlocals[tlsKey].conn, threadlocals[tlsKey], threadlocals[tlsKey].level))
+ ilog.debug('<-- removed conn %s from %s at level %d' % (threadlocals[tlsKey].conn, threadlocals[tlsKey], threadlocals[tlsKey].level))
connPool.checkin(threadlocals[tlsKey].conn)
threadlocals[tlsKey].conn = None
threadlocals[tlsKey].discarded = None
@@ -78,6 +78,16 @@ def discard(self, resource):
self.factory.destroy(resource)
except:
log.exception('while discarding')
+
+ @sync_instance
+ def discard_all(self, resource):
+ """Discard the passed in resource and all other checked in resources
+
+ If the passed in resource is stale, chances are the rest of the
+ resources that are checked in are also stale.
+ """
+ self.discard(resource)
+ self.shrink()
@sync_instance
def shutdown(self):

0 comments on commit 9035c7e

Please sign in to comment.