Skip to content

Commit

Permalink
use session object for http connection
Browse files Browse the repository at this point in the history
  • Loading branch information
jreadey committed Mar 20, 2017
1 parent a281593 commit 0e06918
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
2 changes: 2 additions & 0 deletions h5pyd/_hl/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ def putACL(self, acl):
def close(self):
""" Clears reference to remote resource.
"""
# this will close the socket of the http_conn singleton
self._id._http_conn.close()
self._id.close()

def remove(self):
Expand Down
2 changes: 0 additions & 2 deletions h5pyd/_hl/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,6 @@ def getObjByUuid(collection_type, uuid):
link_class = link_json['class']

if link_class == 'H5L_TYPE_HARD':
#print "hard link, collection:", link_json['collection']
tgt = getObjByUuid(link_json['collection'], link_json['id'])
elif link_class == 'H5L_TYPE_SOFT':
h5path = link_json['h5path']
Expand Down Expand Up @@ -498,7 +497,6 @@ def __setitem__(self, name, obj):
# print "create named type"

type_json = h5type.getTypeItem(obj)
#print "type_json:", type_json
req = "/datatypes"

body = {'type': type_json }
Expand Down
28 changes: 23 additions & 5 deletions h5pyd/_hl/httpconn.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def __init__(self, domain_name, endpoint=None,
self._password = os.environ["H5SERV_PASSWORD"]
else:
self._password = password

self._s = None # Sessions


def getHeaders(self, domain=None, username=None, password=None, headers=None):
if headers is None:
Expand Down Expand Up @@ -98,7 +99,8 @@ def GET(self, req, format="json", headers=None):
self.log.info("GET: {} [{}]".format(req, headers["host"]))

try:
rsp = requests.get(req, headers=headers, verify=self.verifyCert())
s = self.session
rsp = s.get(req, headers=headers, verify=self.verifyCert())
self.log.info("status: {}".format(rsp.status_code))
except ConnectionError as ce:
self.log.error("connection error: {}".format(ce))
Expand Down Expand Up @@ -126,7 +128,8 @@ def PUT(self, req, body=None, format="json", params=None, headers=None):
else:
data = json.dumps(body)
# self.log.info("BODY: " + str(data))
rsp = requests.put(req, data=data, headers=headers,
s = self.session
rsp = s.put(req, data=data, headers=headers,
params=params, verify=self.verifyCert())
return rsp

Expand All @@ -147,7 +150,8 @@ def POST(self, req, body=None, headers=None):
self.log.info("PST: " + req)

try:
rsp = requests.post(req, data=data, headers=headers, verify=self.verifyCert())
s = self.session
rsp = s.post(req, data=data, headers=headers, verify=self.verifyCert())
except ConnectionError as ce:
self.log.warn("connection error: ", ce)
raise IOError(str(ce))
Expand All @@ -167,8 +171,22 @@ def DELETE(self, req, headers=None):
headers = self.getHeaders()

self.log.info("DEL: " + req)
rsp = requests.delete(req, headers=headers, verify=self.verifyCert())
s = self.session
rsp = s.delete(req, headers=headers, verify=self.verifyCert())
return rsp

@property
def session(self):
# create a session object to re-use http connection when possible
# TBD: Add retry here - see: https://laike9m.com/blog/requests-secret-pool_connections-and-pool_maxsize,89/
if self._s is None:
self._s = requests.Session()
return self._s

def close(self):
if self._s:
self._s.close()
self._s = None

@property
def domain(self):
Expand Down
4 changes: 2 additions & 2 deletions test/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ def test_create(self):
softlink = r.get('mysoftlink', getlink=True)
self.assertEqual(softlink.path, '/g1/g1.1')


print("get class")
linkee_class = r.get('myexternallink', getclass=True)

print("get link")
link_class = r.get('myexternallink', getclass=True, getlink=True)
self.assertEqual(link_class, h5py.ExternalLink)
external_link = r.get('myexternallink', getlink=True)
Expand Down

0 comments on commit 0e06918

Please sign in to comment.