improv(idempotency): handle functions with no return value #2521
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Changes
This PR improves the Idempotency utility by allowing it to be used on functions that have no return value (i.e.
void
/undefined
). This is useful when you want to make idempotent a function that causes side effects only, but has no return value.Before this PR the utility would not work on these functions because it would expect the function being made idempotent to return a value that would then be stored in the persistence layer.
As described in the linked issue, the initial plan was to align with how Powertools for AWS Lambda (Python) handle this edge case and store the value as
'null'
into DynamoDB, however after some initial tests it became clear that this was not a viable option due to the fact that it would have become impossible to covert the value back to the originalundefined
when retrieving the idempotency record.Likewise, storing the item with the attribute set to
Null
(DynamoDB attribute type) was not an option due to it being converted to JavaScriptnull
.To preserve the ability for customers to return
null
or'null'
(string), I decided instead to avoid storing the attribute altogether when the function returnsundefined
, since this is the closest value (or rather lack thereof) to the one being returned.In order to implement this logic I had to do some light refactoring in the
IdempotencyHandler
class and introduce an intermediary data structure that makes it explicit when the return value isundefined
vs whenundefined
is used as a mean of control-flow (it'll be clearer when you see the code in the diff I hope - if it's not, please ask).Issue number: #2505
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.