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

Feature request: idempotency Middy middleware #1293

Closed
1 of 2 tasks
saragerion opened this issue Feb 17, 2023 · 3 comments · Fixed by #1474
Closed
1 of 2 tasks

Feature request: idempotency Middy middleware #1293

saragerion opened this issue Feb 17, 2023 · 3 comments · Fixed by #1474
Assignees
Labels
completed This item is complete and has been merged/shipped feature-request This item refers to a feature request for an existing or new utility idempotency This item relates to the Idempotency Utility

Comments

@saragerion
Copy link
Contributor

saragerion commented Feb 17, 2023

Use Case

Users who already use Middy middleware in their functions should be able to make their function handler idempotent via a makeHandlerIdempotent Middy-compatible middleware.

Solution/User Experience

import { 
  makeHandlerIdempotent,
  DynamoDBPersistenceLayer,
  IdempotencyConfig
} from '@aws-lambda-powertools/idempotency';
import middy from '@middy/core';

const config = new IdempotencyConfig({...});
const persistenceLayer = new DynamoDBPersistenceLayer({...});

const lambdaHandler = async (_event: any, _context: any): Promise<void> => {
  /* ...Function logic here... */
}

export const handler = middy(lambdaHandler)
  .use(makeHandlerIdempotent({ config, persistenceLayer });

The makeHandlerIdempotent middleware should work with both sync and async function handlers. The middleware should accept an object with mandatory persistenceStore and an optional config one. The former should be an instance of any class that extends BasePersistenceLayer, while the latter should be an instance of the class IdempotencyConfig.

Following the Powertools for Python decorator implementation, the middleware should:

  • return early if the POWERTOOLS_IDEMPOTENCY_DISABLED env variable has a truthy value (using EnvironmentVariableService) - onBefore
  • use the provided config object or instantiate a new one if none is passed - onBefore
  • register the Lambda context into the config object (used to manage timeouts) - onBefore
  • instantiate an IdempotencyHandler - onBefore
  • call & return the IdempotencyHandler.handle() method - onBefore

This last step will ensure that the IdempotencyHandler will perform all the actions needed to make the function idempotent.

Given how Middy handles requests and errors, there's a chance that the current public methods of IdempotencyHandler might not be enough and we'd need to add additional public methods to call in the onAfter or onError phase.

Alternative solutions

No response

Acknowledgment

@saragerion saragerion added triage This item has not been triaged by a maintainer, please wait feature-request This item refers to a feature request for an existing or new utility labels Feb 17, 2023
@saragerion saragerion added this to the Idempotency - Beta release milestone Feb 17, 2023
@saragerion saragerion changed the title Feature request(idempotency): make function idempotent via decorator for events in sequence (happy path) Feature request(idempotency): make Class method idempotent via decorator for events in sequence (happy path) Feb 17, 2023
@dreamorosi dreamorosi added idempotency This item relates to the Idempotency Utility confirmed The scope is clear, ready for implementation and removed triage This item has not been triaged by a maintainer, please wait labels Feb 17, 2023
@dreamorosi dreamorosi changed the title Feature request(idempotency): make Class method idempotent via decorator for events in sequence (happy path) Feature request: make Class method idempotent via decorator for events in sequence (happy path) Feb 17, 2023
@saragerion saragerion changed the title Feature request: make Class method idempotent via decorator for events in sequence (happy path) Feature request (idempotency): make Class method idempotent via decorator for events in sequence (happy path) Feb 17, 2023
@saragerion saragerion changed the title Feature request (idempotency): make Class method idempotent via decorator for events in sequence (happy path) Feature request(idempotency): make Class method idempotent via decorator for events in sequence (happy path) Feb 17, 2023
@saragerion saragerion changed the title Feature request(idempotency): make Class method idempotent via decorator for events in sequence (happy path) Feature request (idempotency): make Class method idempotent via decorator for events in sequence (happy path) Feb 17, 2023
@dreamorosi dreamorosi changed the title Feature request (idempotency): make Class method idempotent via decorator for events in sequence (happy path) Feature request: make Class method idempotent via decorator for events in sequence (happy path) Feb 17, 2023
@dreamorosi dreamorosi changed the title Feature request: make Class method idempotent via decorator for events in sequence (happy path) Feature request: idempotency Middy middleware Mar 20, 2023
@dreamorosi dreamorosi added blocked This item's progress is blocked by external dependency or reason and removed confirmed The scope is clear, ready for implementation labels Mar 20, 2023
@dreamorosi
Copy link
Contributor

As a result of the discussion in #1410 I have been thinking about how to implement this and realized that there's an added challenge related to how Middy works.

Middy allows middleware authors to return early in the before phase of a middleware. When this happens, the after and onError phases of the middleware that returns as well as all the other middleware in stack also skip these phases.

This mean that if the idempotency middleware is preceded or followed by other Powertools middleware they won't be able to perform any post-request action. This is problematic especially for the captureLambdaHandler middleware (as described in detail here) as skipping the after/onError phases means leaving the request segment dangling which can cause tracing data loss and in rare cases also stack overflow due to new segments being opened at every request and never closed.

This is not a case that is limited to the middleware being discussed in this issue and it also affects any custom middleware implemented by Powertools users so we will need to document and/or expose a way for middleware that do an early return to cleanup after themselves before returning.

I'm still unsure on the best way to handle this, but whatever solution we settle on will have to be adopted by this middleware, hence this note. I'll link back to this issue once we have more info.

@dreamorosi dreamorosi self-assigned this May 2, 2023
@dreamorosi dreamorosi added confirmed The scope is clear, ready for implementation and removed blocked This item's progress is blocked by external dependency or reason labels May 24, 2023
@dreamorosi dreamorosi linked a pull request May 24, 2023 that will close this issue
9 tasks
@github-actions
Copy link
Contributor

github-actions bot commented Jun 5, 2023

⚠️ COMMENT VISIBILITY WARNING ⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added pending-release This item has been merged and will be released soon and removed confirmed The scope is clear, ready for implementation labels Jun 5, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Jun 9, 2023

This is now released under v1.9.0 version!

@dreamorosi dreamorosi added completed This item is complete and has been merged/shipped and removed pending-release This item has been merged and will be released soon labels Jun 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
completed This item is complete and has been merged/shipped feature-request This item refers to a feature request for an existing or new utility idempotency This item relates to the Idempotency Utility
Projects
None yet
2 participants