Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
33 changes: 23 additions & 10 deletions src/aws_durable_execution_sdk_python/lambda_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -1031,19 +1031,32 @@ def get_execution_state(
class LambdaClient(DurableServiceClient):
"""Persist durable operations to the Lambda Durable Function APIs."""

_cached_boto_client: Any = None

def __init__(self, client: Any) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

for a different day, currently Any because there isn't a built-in boto type we can use without adding more imports.

since we're only using 2 methods, might be an idea to create a protocol:

from typing import Protocol

class DurableLambdaClient(Protocol):
    def checkpoint_durable_execution(self, **kwargs) -> dict: ...
    def get_durable_execution_state(self, **kwargs) -> dict: ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

for a different day, currently Any because there isn't a built-in boto type we can use without adding more imports.

since we're only using 2 methods, might be an idea to create a protocol:

from typing import Protocol

class DurableLambdaClient(Protocol):
    def checkpoint_durable_execution(self, **kwargs) -> dict: ...
    def get_durable_execution_state(self, **kwargs) -> dict: ...

I think we should consider using mypy-boto3-lambda instead of creating a custom Protocol. If we want proper type checking, a Protocol for duck typing won't bring much benefit here - today it's 2 methods, tomorrow it could be more, and we'd need to maintain it ourselves or just add classes to remove Any, honestly idk if this is the right direction.
The boto3 stubs are widely used in the community and would be a dev dependency only, not shipped with the package - just used for static analysis during development.

I'm happy to explore this in a follow-up PR if you're open to it. But pls let me know if you want me to add this class now, it's easy.

Copy link
Member

Choose a reason for hiding this comment

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

I wasn't sure how "standard" mypy-boto3-lambda is, good to hear it's got wide adoption...

and yes, adding it as dev dependency is reasonable :-)

self.client = client

@staticmethod
def initialize_client() -> LambdaClient:
client = boto3.client(
"lambda",
config=Config(
connect_timeout=5,
read_timeout=50,
),
)
return LambdaClient(client=client)
@classmethod
def initialize_client(cls) -> LambdaClient:
"""Initialize or return cached Lambda client.

Implements lazy initialization with class-level caching to optimize
Lambda warm starts. The boto3 client is created once and reused across
invocations, avoiding repeated credential resolution and connection
pool setup.

Returns:
LambdaClient: A new LambdaClient instance wrapping the cached boto3 client.
"""
if cls._cached_boto_client is None:
cls._cached_boto_client = boto3.client(
"lambda",
config=Config(
connect_timeout=5,
read_timeout=50,
),
)
return cls(client=cls._cached_boto_client)

def checkpoint(
self,
Expand Down
79 changes: 76 additions & 3 deletions tests/lambda_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@
WaitOptions,
)

# =============================================================================
# Fixtures
# =============================================================================


@pytest.fixture
def reset_lambda_client_cache():
"""Reset the class-level boto3 client cache before and after each test."""
LambdaClient._cached_boto_client = None # noqa: SLF001
yield
LambdaClient._cached_boto_client = None # noqa: SLF001


# =============================================================================
# Tests for Data Classes (ExecutionDetails, ContextDetails, ErrorObject, etc.)
# =============================================================================
Expand Down Expand Up @@ -1910,7 +1923,9 @@ def test_lambda_client_constructor():

@patch.dict("os.environ", {}, clear=True)
@patch("boto3.client")
def test_lambda_client_initialize_client_default(mock_boto_client):
def test_lambda_client_initialize_client_default(
mock_boto_client, reset_lambda_client_cache
):
"""Test LambdaClient.initialize_client with default endpoint."""
mock_client = Mock()
mock_boto_client.return_value = mock_client
Expand All @@ -1930,7 +1945,9 @@ def test_lambda_client_initialize_client_default(mock_boto_client):

@patch.dict("os.environ", {"AWS_ENDPOINT_URL_LAMBDA": "http://localhost:3000"})
@patch("boto3.client")
def test_lambda_client_initialize_client_with_endpoint(mock_boto_client):
def test_lambda_client_initialize_client_with_endpoint(
mock_boto_client, reset_lambda_client_cache
):
"""Test LambdaClient.initialize_client with custom endpoint (boto3 handles it automatically)."""
mock_client = Mock()
mock_boto_client.return_value = mock_client
Expand Down Expand Up @@ -2008,7 +2025,9 @@ def test_checkpoint_error_handling():

@patch.dict("os.environ", {}, clear=True)
@patch("boto3.client")
def test_lambda_client_initialize_client_no_endpoint(mock_boto_client):
def test_lambda_client_initialize_client_no_endpoint(
mock_boto_client, reset_lambda_client_cache
):
"""Test LambdaClient.initialize_client without AWS_ENDPOINT_URL_LAMBDA."""
mock_client = Mock()
mock_boto_client.return_value = mock_client
Expand Down Expand Up @@ -2047,6 +2066,60 @@ def test_lambda_client_checkpoint_with_non_none_client_token():


# =============================================================================
# Tests for LambdaClient caching behavior
# =============================================================================


@patch("boto3.client")
def test_lambda_client_cache_reuses_client(mock_boto_client, reset_lambda_client_cache):
"""Test that initialize_client reuses the same boto3 client on subsequent calls."""
mock_client = Mock()
mock_boto_client.return_value = mock_client

# First call should create the boto3 client
client1 = LambdaClient.initialize_client()

# Second call should reuse the same boto3 client
client2 = LambdaClient.initialize_client()

# boto3.client should only be called once
mock_boto_client.assert_called_once()

# Both LambdaClient instances should wrap the same boto3 client
assert client1.client is client2.client


@patch("boto3.client")
def test_lambda_client_cache_creates_client_only_once(
mock_boto_client, reset_lambda_client_cache
):
"""Test that boto3.client is called only once even with multiple initialize_client calls."""
mock_client = Mock()
mock_boto_client.return_value = mock_client

# Call initialize_client multiple times
for _ in range(5):
LambdaClient.initialize_client()

# boto3.client should only be called once
assert mock_boto_client.call_count == 1


@patch("boto3.client")
def test_lambda_client_cache_is_class_level(
mock_boto_client, reset_lambda_client_cache
):
"""Test that the boto3 client cache is stored at class level."""
mock_client = Mock()
mock_boto_client.return_value = mock_client

# Create client
LambdaClient.initialize_client()

# Verify the boto3 client is cached at class level
assert LambdaClient._cached_boto_client is mock_client # noqa: SLF001


# Tests for Operation JSON Serialization Methods
# =============================================================================

Expand Down