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: Add idempotency middleware #2253

Merged
merged 12 commits into from
Jan 6, 2023

Conversation

leonklingele
Copy link
Member

@leonklingele leonklingele commented Dec 3, 2022

Description

Add idempotency middleware.
Implements #2163.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • For new functionalities I follow the inspiration of the express js framework and built them similar in usage
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation - https://github.com/gofiber/docs for https://docs.gofiber.io/
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • If new dependencies exist, I have checked that they are really necessary and agreed with the maintainers/community (we want to have as few dependencies as possible)
  • I tried to make my code as fast as possible with as few allocations as possible
  • For new code I have written benchmarks so that they can be analyzed and improved

@leonklingele leonklingele force-pushed the add-idempotency-middleware branch 6 times, most recently from 5593988 to 979bbe3 Compare December 3, 2022 05:16
@leonklingele leonklingele force-pushed the add-idempotency-middleware branch 8 times, most recently from 5dd8b22 to 4372dd9 Compare December 3, 2022 15:33
@leonklingele leonklingele marked this pull request as ready for review December 3, 2022 15:41
@leonklingele leonklingele force-pushed the add-idempotency-middleware branch from 4372dd9 to f242f13 Compare December 3, 2022 16:54
Copy link
Contributor

@GalvinGao GalvinGao left a comment

Choose a reason for hiding this comment

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

I would argue the same as in the conversation https://github.com/gofiber/fiber/pull/2253/files#r1038919642 to have consistency with fiber.Storage. Otherwise wonderful work!

@efectn
Copy link
Member

efectn commented Dec 4, 2022

Why did you target v3-beta branch? I think this can be a part of v2.

Also apply PR template into your PRs please.

@leonklingele leonklingele changed the title middleware: add idempotency middleware 🚀 Feature: Add idempotency middleware Dec 4, 2022
@leonklingele
Copy link
Member Author

@efectn I'll backport this to v2 once merged.

@leonklingele leonklingele force-pushed the add-idempotency-middleware branch 6 times, most recently from 7dbc78a to 7912335 Compare December 4, 2022 23:45
@leonklingele
Copy link
Member Author

Soo, finally this is now ready for review. I removed the Storage interface, switched to msgp for marshalling, and improved some other things.

@leonklingele
Copy link
Member Author

Let's make this go in v3 first and I'll open up another PR targeting v2 later on.

@leonklingele
Copy link
Member Author

Anyone gotten the chance to review this already? I'm working on a project which might benefit from having this.

@ReneWerner87
Copy link
Member

Will do it tomorrow

Copy link
Member

@ReneWerner87 ReneWerner87 left a comment

Choose a reason for hiding this comment

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

implementation looks very good

a few small comments/improvements

  • benchmark for the middleware usage would be good, so we can improve it later on
  • instead of the clousure i would maybe try to use a function that doesn't need to be created on-the-fly every time
  • with the locals thing i'm not quite sure, what happens e.g. if you define the idempotency middleware multiple times, normally there shouldn't be any collisions as they should be used on different routes then ... currently quite not sure with that
  • in some places we can optimize and save allocations, like in the ToLower( there exists a fn which does not cause allocations) and other places where only comparisons are done
    -> for comparisons no copies need to be made, only if the data should be kept

@ReneWerner87
Copy link
Member

@leonklingele will wait for the others for the feedback and then i merge it

@efectn @GalvinGao do you have any other points, otherwise i would merge the pull request

@leonklingele
Copy link
Member Author

leonklingele commented Jan 5, 2023 via email

@ReneWerner87
Copy link
Member

Yes use the copy there, i was not sure about this

Copy link
Contributor

@GalvinGao GalvinGao left a comment

Choose a reason for hiding this comment

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

lgtm!

@leonklingele leonklingele force-pushed the add-idempotency-middleware branch from f334ff0 to dc6cfb8 Compare January 6, 2023 00:33
@leonklingele
Copy link
Member Author

I've rebased & addressed @ReneWerner87's points. ptal

@ReneWerner87 ReneWerner87 merged commit 0b5a7d0 into gofiber:v3-beta Jan 6, 2023
@ReneWerner87 ReneWerner87 linked an issue Jan 6, 2023 that may be closed by this pull request
3 tasks
leonklingele added a commit to leonklingele/fiber that referenced this pull request Jan 6, 2023
* middleware: add idempotency middleware

* middleware/idempotency: use fiber.Storage instead of custom storage

* middleware/idempotency: only allocate data if really required

* middleware/idempotency: marshal response using msgp

* middleware/idempotency: add msgp tests

* middleware/idempotency: do not export response

* middleware/idempotency: disable msgp's -io option to disable generating unused methods

* middleware/idempotency: switch to time.Duration based app.Test

* middleware/idempotency: only create closure once

* middleware/idempotency: add benchmarks

* middleware/idempotency: optimize strings.ToLower when making comparison

The real "strings.ToLower" still needs to be used when storing the data.

* middleware/idempotency: safe-copy body
ReneWerner87 pushed a commit that referenced this pull request Jan 13, 2023
* 🚀 Feature: Add idempotency middleware (#2253)

* middleware: add idempotency middleware

* middleware/idempotency: use fiber.Storage instead of custom storage

* middleware/idempotency: only allocate data if really required

* middleware/idempotency: marshal response using msgp

* middleware/idempotency: add msgp tests

* middleware/idempotency: do not export response

* middleware/idempotency: disable msgp's -io option to disable generating unused methods

* middleware/idempotency: switch to time.Duration based app.Test

* middleware/idempotency: only create closure once

* middleware/idempotency: add benchmarks

* middleware/idempotency: optimize strings.ToLower when making comparison

The real "strings.ToLower" still needs to be used when storing the data.

* middleware/idempotency: safe-copy body

* middleware/idempotency: backport to v2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🚀 [Feature]: Idempotency Middleware
4 participants