Skip to content

Base

Base for Idempotency utility

Usage Documentation

Idempotency

CLASS DESCRIPTION
IdempotencyHandler

Base class to orchestrate calls to persistence layer.

IdempotencyHandler

IdempotencyHandler(
    function: Callable,
    function_payload: Any,
    config: IdempotencyConfig,
    persistence_store: BasePersistenceLayer,
    output_serializer: (
        BaseIdempotencySerializer | None
    ) = None,
    key_prefix: str | None = None,
    function_args: tuple | None = None,
    function_kwargs: dict | None = None,
)

Base class to orchestrate calls to persistence layer.

PARAMETER DESCRIPTION
function_payload

JSON Serializable payload to be hashed

TYPE: Any

config

Idempotency Configuration

TYPE: IdempotencyConfig

persistence_store

Instance of persistence layer to store idempotency records

TYPE: BasePersistenceLayer

output_serializer

Serializer to transform the data to and from a dictionary. If not supplied, no serialization is done via the NoOpSerializer

TYPE: BaseIdempotencySerializer | None DEFAULT: None

key_prefix

Custom prefix for idempotency key: key_prefix#hash

TYPE: str | None DEFAULT: None

function_args

Function arguments

TYPE: tuple | None DEFAULT: None

function_kwargs

Function keyword arguments

TYPE: dict | None DEFAULT: None

METHOD DESCRIPTION
handle

Main entry point for handling idempotent execution of a function.

Source code in aws_lambda_powertools/utilities/idempotency/base.py
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
def __init__(
    self,
    function: Callable,
    function_payload: Any,
    config: IdempotencyConfig,
    persistence_store: BasePersistenceLayer,
    output_serializer: BaseIdempotencySerializer | None = None,
    key_prefix: str | None = None,
    function_args: tuple | None = None,
    function_kwargs: dict | None = None,
):
    """
    Initialize the IdempotencyHandler

    Parameters
    ----------
    function_payload: Any
        JSON Serializable payload to be hashed
    config: IdempotencyConfig
        Idempotency Configuration
    persistence_store : BasePersistenceLayer
        Instance of persistence layer to store idempotency records
    output_serializer: BaseIdempotencySerializer | None
        Serializer to transform the data to and from a dictionary.
        If not supplied, no serialization is done via the NoOpSerializer
    key_prefix: str | Optional
        Custom prefix for idempotency key: key_prefix#hash
    function_args: tuple | None
        Function arguments
    function_kwargs: dict | None
        Function keyword arguments
    """
    self.function = function
    self.output_serializer = output_serializer or NoOpSerializer()
    self.data = deepcopy(_prepare_data(function_payload))
    self.fn_args = function_args
    self.fn_kwargs = function_kwargs
    self.config = config
    self.key_prefix = key_prefix

    persistence_store.configure(
        config=config,
        function_name=f"{self.function.__module__}.{self.function.__qualname__}",
        key_prefix=self.key_prefix,
    )

    self.persistence_store = persistence_store

handle

handle() -> Any

Main entry point for handling idempotent execution of a function.

RETURNS DESCRIPTION
Any

Function response

Source code in aws_lambda_powertools/utilities/idempotency/base.py
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
def handle(self) -> Any:
    """
    Main entry point for handling idempotent execution of a function.

    Returns
    -------
    Any
        Function response

    """
    # IdempotencyInconsistentStateError can happen under rare but expected cases
    # when persistent state changes in the small time between put & get requests.
    # In most cases we can retry successfully on this exception.
    for i in range(MAX_RETRIES + 1):  # pragma: no cover
        try:
            return self._process_idempotency()
        except IdempotencyInconsistentStateError:
            if i == MAX_RETRIES:
                raise  # Bubble up when exceeded max tries