Extract the code for optimizing the http connection using unix socket to
a helper function. Calling the new function inside the try block ensure
that errors creating the connection will cancel the transfer.
---
v2v/rhv-upload-plugin.py | 33 ++++++++++++++++++++-------------
1 file changed, 20 insertions(+), 13 deletions(-)
diff --git a/v2v/rhv-upload-plugin.py b/v2v/rhv-upload-plugin.py
index 2abd304bb..3755c34b7 100644
--- a/v2v/rhv-upload-plugin.py
+++ b/v2v/rhv-upload-plugin.py
@@ -86,6 +86,7 @@ def open(readonly):
destination_url = parse_transfer_url(transfer)
http = create_http(destination_url)
options = get_options(http, destination_url)
+ http = optimize_http(http, host, options)
except:
transfer_service.cancel()
raise
@@ -94,19 +95,6 @@ def open(readonly):
"zero=%(can_zero)r unix_socket=%(unix_socket)r"
% options)
- # If we are connected to imageio on the local host and the
- # transfer features a unix_socket then we can reconnect to that.
- if host is not None and options['unix_socket'] is not None:
- try:
- http = UnixHTTPConnection(options['unix_socket'])
- except Exception as e:
- # Very unlikely failure, but we can recover by using the https
- # connection.
- debug("cannot create unix socket connection, using https: %s" % e)
- else:
- debug("optimizing connection using unix socket %r"
- % options['unix_socket'])
-
# Save everything we need to make requests in the handle.
return {
'can_flush': options['can_flush'],
@@ -606,3 +594,22 @@ def get_options(http, url):
else:
raise RuntimeError("could not use OPTIONS request: %d: %s" %
(r.status, r.reason))
+
+def optimize_http(http, host, options):
+ """
+ Return an optimized http connection using unix socket if we are connected
+ to imageio server on the local host and it features a unix socket.
+ """
+ unix_socket = options['unix_socket']
+
+ if host is not None and unix_socket is not None:
+ try:
+ http = UnixHTTPConnection(unix_socket)
+ except Exception as e:
+ # Very unlikely failure, but we can recover by using the https
+ # connection.
+ debug("cannot create unix socket connection, using https: %s" % e)
+ else:
+ debug("optimizing connection using unix socket %r" % unix_socket)
+
+ return http
--
2.21.0