Skip to content
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

Merged
merged 12 commits into from
May 2, 2022
Merged

reuse existing client #19

merged 12 commits into from
May 2, 2022

Conversation

guilhermef
Copy link
Member

@guilhermef guilhermef commented Apr 26, 2022

This PR is an attempt to reuse the same client across S3 requests, may fix #15

@guilhermef guilhermef requested a review from heynemann April 26, 2022 00:19
@coveralls
Copy link

coveralls commented Apr 26, 2022

Pull Request Test Coverage Report for Build 2223643706

Warning: 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

  • 4 of 4 (100.0%) changed or added relevant lines in 1 file are covered.
  • 7 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.3%) to 91.239%

Files with Coverage Reduction New Missed Lines %
thumbor_aws/storage.py 1 92.94%
thumbor_aws/s3_client.py 6 87.72%
Totals Coverage Status
Change from base Build 2197971738: -0.3%
Covered Lines: 302
Relevant Lines: 331

💛 - Coveralls

@heynemann
Copy link
Member

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?).

@guilhermef
Copy link
Member Author

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

@guilhermef guilhermef force-pushed the reuse-existing-client branch 3 times, most recently from a7981a2 to e4a0c42 Compare May 1, 2022 12:41
aws_access_key_id=self.access_key_id,
endpoint_url=self.endpoint_url,
)
self.s3_client = await client.__aenter__()
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member Author

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.

@guilhermef guilhermef force-pushed the reuse-existing-client branch from da6ab26 to 872cd12 Compare May 1, 2022 15:55
@guilhermef guilhermef force-pushed the reuse-existing-client branch from 872cd12 to e3d0b69 Compare May 1, 2022 16:00
@guilhermef guilhermef requested a review from heynemann May 2, 2022 20:55
Copy link
Member

@heynemann heynemann left a 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!

Comment on lines 65 to 88
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:
Copy link
Member

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

Copy link
Member

@heynemann heynemann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much clearer! TYVM!

@guilhermef guilhermef merged commit ef89bd4 into main May 2, 2022
@guilhermef guilhermef deleted the reuse-existing-client branch May 2, 2022 21:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Slow response when using S3
3 participants