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

Move timeout handling into handlers. #786

Merged
merged 17 commits into from
Nov 22, 2021
Merged

Move timeout handling into handlers. #786

merged 17 commits into from
Nov 22, 2021

Conversation

winder
Copy link
Contributor

@winder winder commented Nov 16, 2021

Summary

Replace echo TimeoutMiddleware with timeout handling in handlers. The behavior is functionally identical to the previous implementation, but no longer contains concurrency primitives spanning multiple handler functions.

Test Plan

  • New unit tests for each handler timing out.
  • Unit test for CallWithTimeout utility.
  • make integration, make e2e, and integration go tests all use this code path.

@tolikzinovyev
Copy link
Contributor

What does this fix?

@winder
Copy link
Contributor Author

winder commented Nov 16, 2021

What does this fix?

There are strange race conditions that cause response writers to get mixed up. It seems to be a result of the TimeoutMiddleware, I was able to reproduce some strange behaviors and panics in a unit test.

There are many discussions on github about people finding different data race conditions, and pretty much giving up on the middleware. Here is one: labstack/echo#1947 (comment)

@tolikzinovyev
Copy link
Contributor

@winder
Copy link
Contributor Author

winder commented Nov 16, 2021

I updated to the latest version of echo and was still able to reproduce some problems with this middleware. After disabling the middleware all issues went away.

The way echo passes around the shared context is very convenient for most normal HTTP server things, but seems to be problematic if the middleware needs to do things later on. In this case, after a timeout.

@tolikzinovyev
Copy link
Contributor

But the example I pointed out is a custom middleware. Does it also not work?

@winder
Copy link
Contributor Author

winder commented Nov 16, 2021

That custom middleware is what we were using. There are some rare race conditions in it.

@tolikzinovyev
Copy link
Contributor

No, we were using a middleware from the github.com/labstack/echo/v4/middleware package. I'm talking about a custom middleware from the example.

@winder
Copy link
Contributor Author

winder commented Nov 17, 2021

@tolikzinovyev I see. Yes, I'm using context.WithTimeout like this example. I don't think there's a readability benefit to splitting up the handling with part of it in the middleware and most of it still in the handler, so I put the whole thing in the handler.

I'm having some trouble getting context.WithTimoeut to work.

@codecov-commenter
Copy link

codecov-commenter commented Nov 17, 2021

Codecov Report

Merging #786 (cd7c063) into develop (e45af39) will increase coverage by 4.15%.
The diff coverage is 57.02%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #786      +/-   ##
===========================================
+ Coverage    54.37%   58.53%   +4.15%     
===========================================
  Files           28       29       +1     
  Lines         3908     4001      +93     
===========================================
+ Hits          2125     2342     +217     
+ Misses        1499     1364     -135     
- Partials       284      295      +11     
Impacted Files Coverage Δ
api/error_messages.go 100.00% <ø> (ø)
api/server.go 0.00% <0.00%> (ø)
api/handlers.go 58.84% <56.33%> (+34.88%) ⬆️
util/util.go 51.16% <70.00%> (ø)
api/pointer_utils.go 86.36% <0.00%> (+2.27%) ⬆️
api/converter_utils.go 90.42% <0.00%> (+3.38%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e45af39...cd7c063. Read the comment docs.

@winder winder marked this pull request as ready for review November 17, 2021 15:35
@gmalouf
Copy link
Contributor

gmalouf commented Nov 18, 2021

@winder can you add in PR description how you validated/tested the change / what the behavior is intended to do?

api/handlers.go Outdated Show resolved Hide resolved
api/error_messages.go Outdated Show resolved Hide resolved
api/handlers.go Show resolved Hide resolved
api/handlers.go Outdated Show resolved Hide resolved
api/handlers.go Outdated Show resolved Hide resolved
api/handlers.go Outdated Show resolved Hide resolved
util/util.go Outdated Show resolved Hide resolved
util/util.go Outdated Show resolved Hide resolved
util/util.go Outdated Show resolved Hide resolved
util/util.go Outdated Show resolved Hide resolved
api/handlers.go Outdated Show resolved Hide resolved
api/error_messages.go Outdated Show resolved Hide resolved
api/handlers.go Show resolved Hide resolved
api/handlers.go Outdated Show resolved Hide resolved
api/handlers.go Outdated Show resolved Hide resolved
util/util_test.go Outdated Show resolved Hide resolved
util/util_test.go Outdated Show resolved Hide resolved
util/util_test.go Outdated Show resolved Hide resolved
util/util_test.go Outdated Show resolved Hide resolved
util/util_test.go Outdated Show resolved Hide resolved
util/util.go Outdated Show resolved Hide resolved
util/util.go Outdated Show resolved Hide resolved
return errors.New("should not return")
})

require.Error(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

this shouldn't be necessary also

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm OK with this one, it ensures that there's no panic if a subsequent check assumes the error is not nil. Maybe ErrorIs handles the nil case, or maybe not.

@winder winder merged commit da24444 into develop Nov 22, 2021
@winder winder deleted the will/handler-timeouts branch November 22, 2021 21:08
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.

4 participants