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

fix: cache boto3 swf clients #434

Merged
merged 5 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions simpleflow/boto3_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from __future__ import annotations

import hashlib
from threading import local
Copy link
Contributor

Choose a reason for hiding this comment

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

from typing import Any

import boto3

from simpleflow.utils import json_dumps


def get_or_create_boto3_client(*, region_name: str | None, service_name: str, **kwargs: Any):
d = {
"region_name": region_name,
"service_name": service_name,
}
d.update(kwargs)
key = hashlib.sha1(json_dumps(d).encode()).hexdigest()
local_data = local()
client = getattr(local_data, key, None)
if client is None:
session = boto3.session.Session(region_name=region_name)
client = session.client(service_name, **kwargs)
setattr(local_data, key, client)
return client
7 changes: 3 additions & 4 deletions simpleflow/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@
from botocore.exceptions import ClientError

from . import logger, settings
from .boto3_utils import get_or_create_boto3_client
from .swf.mapper.exceptions import extract_error_code

if TYPE_CHECKING:
from typing import Optional, Tuple # NOQA

from mypy_boto3_s3.service_resource import Bucket, ObjectSummary # NOQA
from mypy_boto3_s3.service_resource import Bucket, ObjectSummary

BUCKET_CACHE = {}
BUCKET_LOCATIONS_CACHE = {}


def get_client() -> boto3.session.Session.client:
return boto3.session.Session().client("s3")
return get_or_create_boto3_client(region_name=None, service_name="s3")


def get_resource(host_or_region: str) -> boto3.session.Session.resource:
Expand Down
7 changes: 3 additions & 4 deletions simpleflow/swf/mapper/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
# config hosted in simpleflow. This wouldn't be the case with a standard
# "logging.getLogger(__name__)" which would write logs under the "swf" namespace
from simpleflow import logger
from simpleflow.boto3_utils import get_or_create_boto3_client
from simpleflow.swf.mapper import settings
from simpleflow.utils import remove_none, retry

from . import settings

SETTINGS = settings.get()
RETRIES = int(os.environ.get("SWF_CONNECTION_RETRIES", "5"))
DEFAULT_AWS_REGION = "us-east-1"
Expand Down Expand Up @@ -47,9 +47,8 @@ def __init__(self, *args, **kwargs):

self.boto3_client = kwargs.pop("boto3_client", None)
if not self.boto3_client:
session = boto3.session.Session(region_name=self.region)
# raises EndpointConnectionError if region is wrong
self.boto3_client = session.client("swf", **creds_)
self.boto3_client = get_or_create_boto3_client(region_name=self.region, service_name="swf", **creds_)

logger.debug(f"initiated connection to region={self.region}")

Expand Down