-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
added client finalizer #533
Conversation
based on the server finazlizer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, nice! I think there are a few differences between Client and Server that need addressing (see comments below) but this is pretty close!
transport/http/client.go
Outdated
// intended use is for request logging. In addition to the response code | ||
// provided in the function signature, additional response parameters are | ||
// provided in the context under keys with the ContextKeyResponse prefix. | ||
type ClientFinalizerFunc func(ctx context.Context, code int, r *http.Request) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. This is the same signature as in the ServerFinalizer, but I'm not sure it actually makes sense here. In the server we're guaranteed to have an http.Request, and we're guaranteed to have a code, so they make sense as parameters. But in the client, we may not make it all the way to generating an http.Request, and we may not get a status code back. And, we have an explicit error value that will always be returned via the endpoint (though it may be nil). So I think the better signature here is
type ClientFinalizerFunc func(ctx context.Context, err error)
Of course, with a note in the comment about which fields in the context will be populated under which conditions. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense to me. the main reason i went with the signature of the ServerFinalizer was from a lack of how to tackle the problem. But it makes sense to me to have a signature like that. I'll update the PR later this evening and push up the requested changes. Thanks for the comments 😸 !
transport/http/client.go
Outdated
ctx = context.WithValue(ctx, ContextKeyResponseSize, resp.ContentLength) | ||
c.finalizer(ctx, resp.StatusCode, req) | ||
}() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure it's correct to create zero-value http.Requests and Responses; my intuition is that they need to be created by package http in an e.g. NewReqeust constructor. But that's fine, I think we can get by without them. Going with the change in the ClientFinalizerFunc below, perhaps this can look like..
var (
resp *http.Response
err error
)
if c.finalizer != nil {
defer func() {
if resp != nil {
ctx = context.WithValue(ctx, ContextKeyResponseHeaders, resp.Header)
ctx = context.WithValue(ctx, ContextKeyResponseSize, resp.ContentLength)
}
c.finalizer(ctx, err)
}()
}
What do you think?
func TestClientFinalizer(t *testing.T) { | ||
var ( | ||
headerKey = "X-Henlo-Lizer" | ||
headerVal = "Helllo you stinky lizard" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's ridiculous how my stream-of-consciousness garbage thoughts when I was writing this test get propagated into the infinite future 🤙
update based on comments to previous commit.
@peterbourgon updated based your comments. |
Sweet! Thanks a ton! |
based on the server finazlizer. @peterbourgon super open to suggestions to make this better, also excited as it will be my first real contribution to this project if it makes it in. should resolve #472 for the most part i believe.