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

net/http: request context is not propagating deadlines #19562

Closed
rakyll opened this issue Mar 15, 2017 · 2 comments
Closed

net/http: request context is not propagating deadlines #19562

rakyll opened this issue Mar 15, 2017 · 2 comments

Comments

@rakyll
Copy link
Contributor

rakyll commented Mar 15, 2017

Consider the following program:

go func() {
    req, _ := http.NewRequest("GET", "http://localhost:8080", nil)
    ctx, cancel := context.WithDeadline(req.Context(), time.Now().Add(5*time.Second))
    defer cancel()

    req = req.WithContext(ctx)
    _, err := http.DefaultClient.Do(req)
    if err != nil {
        log.Println(err)
    }

}()

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    fmt.Println(r.Context().Deadline())
    select {
    case <-r.Context().Done():
        fmt.Fprintln(w, "done")
    }
})
http.ListenAndServe(":8080", nil)

The output of this program is:

0001-01-01 00:00:00 +0000 UTC false
2017/03/15 10:44:55 Get http://localhost:8080: context deadline exceeded

Instead, I expected to see a non-zero deadline and "done" written to the response body after 5 seconds.

Any plans to inject the deadline in a preserved header to the outgoing requests, so the handler can automatically extract them and construct the incoming request context from there.

/cc @bradfitz

@bradfitz
Copy link
Contributor

I am unaware of any standards in this area and am reluctant to invent a Go-specific one.

@bradfitz bradfitz added this to the Unplanned milestone Mar 15, 2017
@rakyll
Copy link
Contributor Author

rakyll commented Mar 15, 2017

Agreeing with not inventing one if there is not an established standard.

@rakyll rakyll closed this as completed Mar 15, 2017
@golang golang locked and limited conversation to collaborators Mar 15, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants