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

Add an overall request timeout #47

Merged
3 commits merged into from
Dec 15, 2022
Merged

Add an overall request timeout #47

3 commits merged into from
Dec 15, 2022

Conversation

ghost
Copy link

@ghost ghost commented Nov 30, 2022

What

Add an overall request timeout so that if a write timeout will be triggered, the request will be terminated as well, and a response written, before the writer is closed, since there is no value in continuing if the writer has been closed.

How to review

Review code

Who can review

Anyone

…d, the request will be terminated as well, since there is no value in continuing if the writer has been closed.
DavidSubiros
DavidSubiros previously approved these changes Dec 6, 2022
Copy link
Contributor

@DavidSubiros DavidSubiros left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@DavidSubiros DavidSubiros left a comment

Choose a reason for hiding this comment

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

small comments, feel free to ignore

So(resp.StatusCode, ShouldEqual, 200)

b, err := io.ReadAll(resp.Body)
So(err, ShouldEqual, nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest using ShouldBeNil instead

resp, err := http.Get("http://" + a)

Convey("the request is terminated with a 'connection timeout' response", func() {
So(err, ShouldEqual, nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above ShouldBeNil

go func() {
if err := s.Server.ListenAndServe(); err != nil {
if !errors.Is(err, http.ErrServerClosed) {
os.Exit(1)
Copy link
Contributor

Choose a reason for hiding this comment

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

just a suggestion, please feel free to disagree.

I would create an error channel and pass it as a paramter to startServer (or create it in startServer and return it as a second value, afte rthe func), so that tests can validate what error is returned by ListenAndServe if required. Then instead of exiting on unexpected errors we can just fail the test.
Also, this channel could be closed when the inner go func finishes its execution

If this would make things too complex, feel free to ignore comment.

Copy link
Author

Choose a reason for hiding this comment

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

No it's a good suggestion - see if the new version conforms


return func() {
if err := s.Server.Shutdown(context.Background()); err != nil {
os.Exit(1)
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe we can also provide a shutdownErr channel, similarly to the description of above.


// GetFreePort is simple utility to find a free port on the "localhost" interface of the host machine
// for a local server to use. This is especially useful for testing purposes
func GetFreePort() (port int, err error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

very good idea!


Convey("with a server whose handler runs for less than the server's WriteTimout", func() {
h := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
time.Sleep(DefaultWriteTimeout / 100)
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe this sleep is not needed? (we are testing the case where the handle finishes fast anyway)

@ghost ghost requested a review from DavidSubiros December 13, 2022 08:47
Copy link
Contributor

@DavidSubiros DavidSubiros left a comment

Choose a reason for hiding this comment

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

thanks for addressing the comments!
LGTM

@ghost ghost merged commit 6ac274b into main Dec 15, 2022
@ghost ghost deleted the feature/add-timeout-handler branch December 15, 2022 16:12
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant