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

Ensure Context arg is the same as request Context #97

Merged
merged 1 commit into from
Apr 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions httpx/middleware/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func (h *Header) ServeHTTPContext(ctx context.Context, w http.ResponseWriter, r
value := e(r)

ctx = httpx.WithHeader(ctx, h.key, value)
r = r.WithContext(ctx)
Copy link
Contributor

Choose a reason for hiding this comment

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

so this is overriding r.Context with the argument ctx, so would it be possible to loss data configured in the previous r.Context instance? What if the server made r.Context cancelable for instance?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, that's a good point. The assumption is that at all times the ctx passed into ServeHTTPContext will be the same object as r.Context(). They start off as the same object in the background middleware, and with this change, they would stay the same object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With this change, we could support using our httpx middleware with a regular http.Handler such as mux.Router like this #100

And eventually deprecate the ServeHTTPContext interface. I'm just playing with some ideas here though.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with the direction here, however I'm concerned this may mask non-intended behavior where there might be two different Context objects here. If httpx handlers can rely on the request's context, then they could simply ignore the ctx arg here during this transition period, no?

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 think either way is a risk. An app can have custom middleware that relies on modifying the context passed in or the request context. In our case it is almost certainly relying on the context passed in.


return h.handler.ServeHTTPContext(ctx, w, r)
}

Expand Down
5 changes: 5 additions & 0 deletions httpx/middleware/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"context"

"github.com/remind101/pkg/httpx"
)

Expand All @@ -24,7 +25,11 @@ func TestHeader(t *testing.T) {
m := ExtractHeader(
httpx.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
data := httpx.Header(ctx, tt.key)
if got, want := data, tt.val; got != want {
t.Fatalf("%s => %s; want %s", tt.key, got, want)
}

data = httpx.Header(r.Context(), tt.key)
if got, want := data, tt.val; got != want {
t.Fatalf("%s => %s; want %s", tt.key, got, want)
}
Expand Down
3 changes: 3 additions & 0 deletions httpx/middleware/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ func LogTo(h httpx.Handler, g loggerGenerator) httpx.Handler {
func InsertLogger(h httpx.Handler, g loggerGenerator) httpx.Handler {
return httpx.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
l := g(ctx, r)

ctx = logger.WithLogger(ctx, l)
r = r.WithContext(ctx)

return h.ServeHTTPContext(ctx, w, r)
})
}
Expand Down
1 change: 1 addition & 0 deletions httpx/middleware/opentracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (h *OpentracingTracer) ServeHTTPContext(ctx context.Context, w http.Respons

defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
r = r.WithContext(ctx)

rw := NewResponseWriter(w)
reqErr := h.handler.ServeHTTPContext(ctx, rw, r)
Expand Down
2 changes: 2 additions & 0 deletions httpx/middleware/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func (m *Reporter) ServeHTTPContext(ctx context.Context, w http.ResponseWriter,
// Add the request id to reporter context.
ctx = errors.WithInfo(ctx, "request_id", httpx.RequestID(ctx))

r = r.WithContext(ctx)

return m.handler.ServeHTTPContext(ctx, w, r)
}

Expand Down
3 changes: 3 additions & 0 deletions httpx/middleware/request_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"

"context"

"github.com/remind101/pkg/httpx"
)

Expand Down Expand Up @@ -39,5 +40,7 @@ func (h *RequestID) ServeHTTPContext(ctx context.Context, w http.ResponseWriter,
requestID := e(r)

ctx = httpx.WithRequestID(ctx, requestID)
r = r.WithContext(ctx)

return h.handler.ServeHTTPContext(ctx, w, r)
}
8 changes: 7 additions & 1 deletion httpx/middleware/request_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"net/http/httptest"
"testing"

"github.com/remind101/pkg/httpx"
"context"

"github.com/remind101/pkg/httpx"
)

func TestRequestID(t *testing.T) {
Expand All @@ -23,7 +24,12 @@ func TestRequestID(t *testing.T) {
m := &RequestID{
handler: httpx.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
requestID := httpx.RequestID(ctx)
if got, want := requestID, tt.id; got != want {
t.Fatalf("RequestID => %s; want %s", got, want)
}

// From request.Context()
requestID = httpx.RequestID(r.Context())
if got, want := requestID, tt.id; got != want {
t.Fatalf("RequestID => %s; want %s", got, want)
}
Expand Down