-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
reuse existing client #19
Conversation
Pull Request Test Coverage Report for Build 2223643706Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
Does it matter that we restart the client? From what I understood from the docs the client should be re-created every time (it does not hold any connections, does it?). |
Based on this comment, it seems that the client can reuse the connections aio-libs/aiobotocore#928 |
a7981a2
to
e4a0c42
Compare
thumbor_aws/s3_client.py
Outdated
aws_access_key_id=self.access_key_id, | ||
endpoint_url=self.endpoint_url, | ||
) | ||
self.s3_client = await client.__aenter__() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be calling __ aexit__ shouldn't we? I think the correct way here is to assing self.s3_client to client and have the rest of the codebase do:
with s3_client as client:
# Whatever you need to do!
This way we ensure proper disposition of resources, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might not need all the changes I've made; the session object should be enough to reuse the same HTTP connections.
I would need @oliverschewe help to validate this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@heynemann I've updated the PR, to only reuse the S3Client object, but to still keep the with s3_client as client
usage.
da6ab26
to
872cd12
Compare
872cd12
to
e3d0b69
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with minor nits!
thumbor_aws/loader.py
Outdated
if not self._s3_client: | ||
self._s3_client = S3Client(context) | ||
self._s3_client.configuration = { | ||
"region_name": context.config.AWS_LOADER_REGION_NAME, | ||
"secret_access_key": context.config.AWS_LOADER_S3_SECRET_ACCESS_KEY, | ||
"access_key_id": context.config.AWS_LOADER_S3_ACCESS_KEY_ID, | ||
"endpoint_url": context.config.AWS_LOADER_S3_ENDPOINT_URL, | ||
"bucket_name": context.config.AWS_LOADER_BUCKET_NAME, | ||
"root_path": context.config.AWS_LOADER_ROOT_PATH, | ||
} | ||
if self._s3_client.compatibility_mode is True: | ||
self._s3_client.configuration[ | ||
"region_name" | ||
] = context.config.TC_AWS_REGION | ||
self._s3_client.configuration[ | ||
"endpoint_url" | ||
] = context.config.TC_AWS_ENDPOINT | ||
self._s3_client.configuration[ | ||
"bucket_name" | ||
] = context.config.TC_AWS_LOADER_BUCKET | ||
self._s3_client.configuration[ | ||
"root_path" | ||
] = context.config.TC_AWS_LOADER_ROOT_PATH | ||
return self._s3_client |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if not self._s3_client: | |
self._s3_client = S3Client(context) | |
self._s3_client.configuration = { | |
"region_name": context.config.AWS_LOADER_REGION_NAME, | |
"secret_access_key": context.config.AWS_LOADER_S3_SECRET_ACCESS_KEY, | |
"access_key_id": context.config.AWS_LOADER_S3_ACCESS_KEY_ID, | |
"endpoint_url": context.config.AWS_LOADER_S3_ENDPOINT_URL, | |
"bucket_name": context.config.AWS_LOADER_BUCKET_NAME, | |
"root_path": context.config.AWS_LOADER_ROOT_PATH, | |
} | |
if self._s3_client.compatibility_mode is True: | |
self._s3_client.configuration[ | |
"region_name" | |
] = context.config.TC_AWS_REGION | |
self._s3_client.configuration[ | |
"endpoint_url" | |
] = context.config.TC_AWS_ENDPOINT | |
self._s3_client.configuration[ | |
"bucket_name" | |
] = context.config.TC_AWS_LOADER_BUCKET | |
self._s3_client.configuration[ | |
"root_path" | |
] = context.config.TC_AWS_LOADER_ROOT_PATH | |
return self._s3_client | |
if self._s3_client is not None: | |
return self._s3_client | |
self._s3_client = S3Client(context) | |
self._s3_client.configuration = { | |
"region_name": context.config.AWS_LOADER_REGION_NAME, | |
"secret_access_key": context.config.AWS_LOADER_S3_SECRET_ACCESS_KEY, | |
"access_key_id": context.config.AWS_LOADER_S3_ACCESS_KEY_ID, | |
"endpoint_url": context.config.AWS_LOADER_S3_ENDPOINT_URL, | |
"bucket_name": context.config.AWS_LOADER_BUCKET_NAME, | |
"root_path": context.config.AWS_LOADER_ROOT_PATH, | |
} | |
if self._s3_client.compatibility_mode is True: | |
self._s3_client.configuration[ | |
"region_name" | |
] = context.config.TC_AWS_REGION | |
self._s3_client.configuration[ | |
"endpoint_url" | |
] = context.config.TC_AWS_ENDPOINT | |
self._s3_client.configuration[ | |
"bucket_name" | |
] = context.config.TC_AWS_LOADER_BUCKET | |
self._s3_client.configuration[ | |
"root_path" | |
] = context.config.TC_AWS_LOADER_ROOT_PATH | |
return self._s3_client |
|
||
result.successful = True | ||
result.buffer = body | ||
class S3Loader: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get why you added a class to make it a singleton but we can make a singleton just as easily with methods:
__instance = None
def load(params):
if __instance is None:
__instance = Whatever()
# do something with __instance
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much clearer! TYVM!
This PR is an attempt to reuse the same client across S3 requests, may fix #15