Skip to content

Commit

Permalink
abstract.client: Rename close to abort, clean to recycle.
Browse files Browse the repository at this point in the history
  • Loading branch information
chfoo committed Feb 17, 2015
1 parent 181adc6 commit 2e4f5b5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
30 changes: 18 additions & 12 deletions wpull/abstract/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(self, connection_pool=None, recorder=None,
@abc.abstractmethod
def _session_class(self):
'''Return session class.'''
return BaseSession # return something for code checkers

@contextlib.contextmanager
def session(self):
Expand All @@ -58,11 +59,12 @@ def session(self):
yield session
except Exception as error:
if not isinstance(error, StopIteration):
_logger.debug('Close session.')
session.close()
_logger.debug('Early close session.')
session.abort()
session.recycle()
raise
finally:
session.clean()
else:
session.recycle()
else:
session = self._session_class()(
connection_pool=self._connection_pool,
Expand All @@ -72,11 +74,12 @@ def session(self):
yield session
except Exception as error:
if not isinstance(error, StopIteration):
_logger.debug('Close session.')
session.close()
_logger.debug('Early close session.')
session.abort()
session.recycle()
raise
finally:
session.clean()
else:
session.recycle()

def close(self):
'''Close the connection pool and recorders.'''
Expand All @@ -99,12 +102,15 @@ def __init__(self, connection_pool=None, recorder_session=None,
self._connection = None

@abc.abstractmethod
def close(self):
'''Close any connections.'''
def abort(self):
'''Terminate early and close any connections.'''

@abc.abstractmethod
def clean(self):
'''Return connection back to the pool.'''
def recycle(self):
'''Clean up and return connection back to the pool.
Connections should be kept alive if supported.
'''

@trollius.coroutine
def _check_out_connection(self, request):
Expand Down
19 changes: 13 additions & 6 deletions wpull/ftp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,16 @@ def _fetch_size(self, request):
except FTPServerError:
return

def clean(self):
def abort(self):
self._close_data_connection()

if self._connection:
self._connection.close()
self._login_table.pop(self._connection, None)

def recycle(self):
self._close_data_connection()

if self._connection:
if self._recorder_session:
self._recorder_session.end_control(
Expand All @@ -346,14 +355,12 @@ def clean(self):

self._connection_pool.check_in(self._connection)

def _close_data_connection(self):
if self._data_connection:
self._data_connection.close()
self._connection_pool.check_in(self._data_connection)
self._data_connection = None

if self._data_stream:
self._data_stream.data_observer.clear()

def close(self):
if self._connection:
self._connection.close()
self._login_table.pop(self._connection, None)
self._data_stream = None
11 changes: 5 additions & 6 deletions wpull/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,13 @@ def done(self):
'''
return self._session_complete

def close(self):
'''Abort the session if not complete.'''
if not self._session_complete and self._connection:
def abort(self):
if self._connection:
self._connection.close()
self._session_complete = True

def clean(self):
'''Return connection back to the pool.'''
self._session_complete = True

def recycle(self):
assert self._session_complete

if self._connection:
Expand Down

0 comments on commit 2e4f5b5

Please sign in to comment.