Skip to content

Commit

Permalink
fix: close unused connections for HTTP interface
Browse files Browse the repository at this point in the history
  • Loading branch information
william-silversmith committed May 1, 2024
1 parent 7d8183c commit 560597e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
1 change: 1 addition & 0 deletions automated_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ def test_http_read(secrets):
cf = CloudFiles("https://storage.googleapis.com/seunglab-test/test_v0/black/", secrets=secrets)

try:
head = cf.head('info')
info = cf.get_json('info')
except requests.exceptions.HTTPError:
return
Expand Down
13 changes: 7 additions & 6 deletions cloudfiles/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,9 +745,9 @@ def put_file(self, file_path, content, content_type,
@retry
def head(self, file_path):
key = self.get_path_to_file(file_path)
resp = self.session.head(key)
resp.raise_for_status()
return resp.headers
with self.session.head(key) as resp:
resp.raise_for_status()
return resp.headers

@retry
def get_file(self, file_path, start=None, end=None, part_size=None):
Expand All @@ -762,6 +762,7 @@ def get_file(self, file_path, start=None, end=None, part_size=None):
resp = self.session.get(key)
if resp.status_code in (404, 403):
return (None, None, None, None)
resp.close()
resp.raise_for_status()

# Don't check MD5 for http because the etag can come in many
Expand All @@ -779,9 +780,8 @@ def get_file(self, file_path, start=None, end=None, part_size=None):
@retry
def exists(self, file_path):
key = self.get_path_to_file(file_path)
resp = self.session.get(key, stream=True)
resp.close()
return resp.ok
with self.session.get(key, stream=True) as resp:
return resp.ok

def files_exist(self, file_paths):
return {path: self.exists(path) for path in file_paths}
Expand All @@ -804,6 +804,7 @@ def request(token):
params={ "prefix": prefix, "pageToken": token },
)
results.raise_for_status()
results.close()
return results.json()

token = None
Expand Down

0 comments on commit 560597e

Please sign in to comment.