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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃敟 Update: add timeout context middleware #2090

Merged
merged 9 commits into from Sep 16, 2022
Merged

馃敟 Update: add timeout context middleware #2090

merged 9 commits into from Sep 16, 2022

Conversation

hakankutluay
Copy link
Contributor

@hakankutluay hakankutluay commented Sep 14, 2022

Description

Timeout Context middleware for Fiber. As a fiber.Handler wrapper, it creates a context with context.WithTimeout and pass it in UserContext.

If the context passed executions (eg. DB ops, Http calls) takes longer than the given duration to return, the timeout error is set and forwarded to the centralized ErrorHandler.

It has no race conditions, ready to use on production.

Fixes #2088

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • 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

Commit formatting:

Use emojis on commit messages so it provides an easy way of identifying the purpose or intention of a commit. Check out the emoji cheatsheet here: https://gitmoji.carloscuesta.me/

@ReneWerner87
Copy link
Member

hi thanks for the work,

before you go down the checklist and tick off what is already done, i would like to make one comment

can we include this functionality under the current timeout middleware without causing a breaking change ?
i guess some consumers are confused and later don't know which of the two to use or wonder why 2 exist

if a merge of both doesn't work, what do you think about creating another entry function?

like for example

timeout.NewWithContext

this we can then customize later in v3 and make it controllable via configurations which are injected by changing the strategy, like with the limiter middleware

what do you think ?

@hakankutluay
Copy link
Contributor Author

Hi @ReneWerner87 ,

Thanks for your valuable comments. I got your point.

This feature cannot be included in the current timeout middleware, current one handles timeout in a different way and has race conditions also not using context.

Having a new entrypoint that you mentioned is much more clear, I've changed implementation in that way. Thanks for your suggestion.

We may also think to replace the current timeout middleware with this one because it has race conditions and satisfies timeout needs.

@ReneWerner87
Copy link
Member

thx,
don麓t forget this file middleware/timeoutcontext/README.md

@ReneWerner87
Copy link
Member

since the interface is the same or just extended, I would also support the replacement unless you see any other breaking changes ?

@hakankutluay
Copy link
Contributor Author

There is no breaking change on the signature so I replaced the current timeout middleware with the new one.

@ReneWerner87
Copy link
Member

ok thx

@vigo
Copy link

vigo commented Sep 16, 2022

great work @hakankutluay thank you!

@efectn efectn linked an issue Sep 16, 2022 that may be closed by this pull request
@efectn
Copy link
Member

efectn commented Sep 16, 2022

Please create a PR for docs repo

@hakankutluay
Copy link
Contributor Author

hakankutluay commented Sep 16, 2022

PR is created on docs repo

@ReneWerner87
Copy link
Member

@hakankutluay have you tested the examples ? where does the parameter come from, don't see it in the route or routes

our consumers are sometimes lazy and copy directly the examples and see how the whole thing behaves

middleware/timeout/README.md Outdated Show resolved Hide resolved
Copy link
Member

@efectn efectn left a comment

Choose a reason for hiding this comment

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

Update docs pls

@hakankutluay
Copy link
Contributor Author

docs are also updated

@ReneWerner87 ReneWerner87 changed the title 馃敟 Feature: add timeoutcontext middleware 馃敟 Update: add timeout context middleware Sep 16, 2022
@ReneWerner87 ReneWerner87 merged commit 7c83e38 into gofiber:master Sep 16, 2022
@welcome
Copy link

welcome bot commented Sep 16, 2022

Congrats on merging your first pull request! 馃帀 We here at Fiber are proud of you! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants