Skip to content

Commit 2cd2b3b

Browse files
author
Michael Brewer
committed
refactor(idemptonency): slight performance tuning
make configure backwards compatible resolve the full function name ahead of time
1 parent ac34729 commit 2cd2b3b

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

aws_lambda_powertools/utilities/idempotency/persistence/base.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class BasePersistenceLayer(ABC):
112112

113113
def __init__(self):
114114
"""Initialize the defaults"""
115-
self.function_name = None
115+
self.function_name = ""
116116
self.configured = False
117117
self.event_key_jmespath: Optional[str] = None
118118
self.event_key_compiled_jmespath = None
@@ -125,16 +125,19 @@ def __init__(self):
125125
self._cache: Optional[LRUDict] = None
126126
self.hash_function = None
127127

128-
def configure(self, config: IdempotencyConfig, function_name: str) -> None:
128+
def configure(self, config: IdempotencyConfig, function_name: Optional[str] = None) -> None:
129129
"""
130130
Initialize the base persistence layer from the configuration settings
131131
132132
Parameters
133133
----------
134134
config: IdempotencyConfig
135135
Idempotency configuration settings
136+
function_name: str, Optional
137+
The name of the function being decorated
136138
"""
137-
self.function_name = function_name
139+
self.function_name = f"{os.getenv(constants.LAMBDA_FUNCTION_NAME_ENV, 'test-func')}.{function_name or ''}"
140+
138141
if self.configured:
139142
# Prevent being reconfigured multiple times
140143
return
@@ -180,8 +183,7 @@ def _get_hashed_idempotency_key(self, data: Dict[str, Any]) -> str:
180183
warnings.warn(f"No value found for idempotency_key. jmespath: {self.event_key_jmespath}")
181184

182185
generated_hash = self._generate_hash(data=data)
183-
function_name = os.getenv(constants.LAMBDA_FUNCTION_NAME_ENV, "test-func") + "." + self.function_name
184-
return f"{function_name}#{generated_hash}"
186+
return f"{self.function_name}#{generated_hash}"
185187

186188
@staticmethod
187189
def is_missing_idempotency_key(data) -> bool:

tests/functional/idempotency/test_idempotency.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ def test_in_progress_never_saved_to_cache(
648648
):
649649
# GIVEN a data record with status "INPROGRESS"
650650
# and persistence_store has use_local_cache = True
651-
persistence_store.configure(idempotency_config, "handler")
651+
persistence_store.configure(idempotency_config)
652652
data_record = DataRecord("key", status="INPROGRESS")
653653

654654
# WHEN saving to local cache
@@ -661,7 +661,7 @@ def test_in_progress_never_saved_to_cache(
661661
@pytest.mark.parametrize("idempotency_config", [{"use_local_cache": False}], indirect=True)
662662
def test_user_local_disabled(idempotency_config: IdempotencyConfig, persistence_store: DynamoDBPersistenceLayer):
663663
# GIVEN a persistence_store with use_local_cache = False
664-
persistence_store.configure(idempotency_config, "")
664+
persistence_store.configure(idempotency_config)
665665

666666
# WHEN calling any local cache options
667667
data_record = DataRecord("key", status="COMPLETED")
@@ -683,7 +683,7 @@ def test_delete_from_cache_when_empty(
683683
idempotency_config: IdempotencyConfig, persistence_store: DynamoDBPersistenceLayer
684684
):
685685
# GIVEN use_local_cache is True AND the local cache is empty
686-
persistence_store.configure(idempotency_config, "handler")
686+
persistence_store.configure(idempotency_config)
687687

688688
try:
689689
# WHEN we _delete_from_cache
@@ -755,7 +755,7 @@ def test_raise_on_no_idempotency_key(
755755
idempotency_config: IdempotencyConfig, persistence_store: DynamoDBPersistenceLayer, lambda_context
756756
):
757757
# GIVEN a persistence_store with raise_on_no_idempotency_key and no idempotency key in the request
758-
persistence_store.configure(idempotency_config, "handler")
758+
persistence_store.configure(idempotency_config)
759759
persistence_store.raise_on_no_idempotency_key = True
760760
assert persistence_store.use_local_cache is False
761761
assert "body" in persistence_store.event_key_jmespath
@@ -802,9 +802,9 @@ def test_jmespath_with_powertools_json(
802802
def test_custom_jmespath_function_overrides_builtin_functions(
803803
config_with_jmespath_options: IdempotencyConfig, persistence_store: DynamoDBPersistenceLayer, lambda_context
804804
):
805-
# GIVEN an persistence store with a custom jmespath_options
805+
# GIVEN a persistence store with a custom jmespath_options
806806
# AND use a builtin powertools custom function
807-
persistence_store.configure(config_with_jmespath_options, "handler")
807+
persistence_store.configure(config_with_jmespath_options)
808808

809809
with pytest.raises(jmespath.exceptions.UnknownFunctionError, match="Unknown function: powertools_json()"):
810810
# WHEN calling _get_hashed_idempotency_key

0 commit comments

Comments
 (0)