-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Use the same hpack encoder on a transport and share it between RPCs. #1536
Conversation
c81f8d3
to
b817f5e
Compare
b817f5e
to
f7f1e8e
Compare
@@ -209,6 +210,8 @@ func newHTTP2Client(ctx context.Context, addr TargetInfo, opts ConnectOptions) ( | |||
goAway: make(chan struct{}), | |||
awakenKeepalive: make(chan struct{}, 1), | |||
framer: newFramer(conn), | |||
hBuf: &buf, |
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.
hBuf: &bytes.Buffer{}
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 know we talked about this but now I realize that hEnc needs the same buffer.
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.
Yes, that should have been obvious, sorry!
transport/http2_client.go
Outdated
// Check if credentials.PerRPCCredentials were provided via call options. | ||
// Note: if these credentials are provided both via dial options and call | ||
// options, then both sets of credentials will be applied. | ||
if callCreds := callHdr.Creds; callCreds != nil { | ||
if !t.isSecure && callCreds.RequireTransportSecurity() { | ||
return nil, streamErrorf(codes.Unauthenticated, "transport: cannot send secure credentials on an insecure connection") | ||
return nil, streamErrorf(codes.Unauthenticated, "transport: cannot send secure credentials on an insecure conneciton") |
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.
connection
transport/http2_client.go
Outdated
// Make the slice of certain predictable size to reduce allocations made by append. | ||
hfLen := 7 // :method, :scheme, :path, :authority, content-type, user-agent, te | ||
hfLen += len(authData) + len(callAuthData) | ||
headerFields := make([]hpack.HeaderField, 0, hfLen) |
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.
This will result in reallocations if there is any user-provided metadata.
It probably would be a win to count it first.
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 thought of that but we'll need to loop through the and see if there are any reserved headers in there or not twice (one to count and one to actually append in the slice). And that holds true for other headers too. So I ended up counting the ones that are easily countable. And since append allocates twice the current size. I'm guessing there won't be too many allocations happening.
Thoughts?
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.
Could you add some benchmarks to cover this so we can decide? We don't have anything that includes metadata. If the number of entries and size of each entry can be tweaked through settings, that would be ideal. Ideally we could independently configure client headers, server headers, and server trailers. It would be interesting to compare the performance of both approaches in the following scenarios:
- no metadata
- with a small amount of metadata (~2)
- with a large amount of metadata (~20)
transport/http2_client.go
Outdated
Client: true, | ||
WireLength: bufLen, | ||
Client: true, | ||
//WireLength: // TODO(mmukhi): Revisit this if needed. |
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.
Delete
transport/http2_client.go
Outdated
@@ -961,7 +939,7 @@ func (t *http2Client) handleGoAway(f *http2.GoAwayFrame) { | |||
t.notifyError(connectionErrorf(true, nil, "received illegal http2 GOAWAY frame: stream ID %d is even", f.LastStreamID)) | |||
return | |||
} | |||
// A client can receive multiple GoAways from server (look at https://github.com/grpc/grpc-go/issues/1387). | |||
// A client can recieve multiple GoAways from server (look at https://github.com/grpc/grpc-go/issues/1387). |
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.
except after "c"
transport/http2_server.go
Outdated
hEnc := hpack.NewEncoder(hBuf) | ||
hEnc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"}) | ||
hEnc.WriteField(hpack.HeaderField{Name: "content-type", Value: "application/grpc"}) | ||
headerFields := make([]hpack.HeaderField, 0, 2) // aleast :status, content-type will be there if none else. |
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 think it makes sense to do the math to avoid the reallocation/copy.
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.
Same argument as above.
transport/http2_server.go
Outdated
@@ -782,18 +758,13 @@ func (t *http2Server) WriteStatus(s *Stream, st *status.Status) error { | |||
headersSent = true | |||
} | |||
|
|||
hBuf := bytes.NewBuffer([]byte{}) // TODO(mmukhi): Try and re-use this memory. | |||
hEnc := hpack.NewEncoder(hBuf) | |||
headerFields := make([]hpack.HeaderField, 0, 2) // grpc-status and grpc-message will be there if none else. |
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.
Do the math?
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.
Same argument as above.
transport/http2_server.go
Outdated
if t.stats != nil { | ||
outTrailer := &stats.OutTrailer{ | ||
WireLength: bufLen, | ||
// WireLength:TODO(mmukhi): Revisit this later, if needed. |
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.
Delete
transport/http2_server.go
Outdated
@@ -938,7 +908,7 @@ func (t *http2Server) keepalive() { | |||
maxAge := time.NewTimer(t.kp.MaxConnectionAge) | |||
keepalive := time.NewTimer(t.kp.Time) | |||
// NOTE: All exit paths of this function should reset their | |||
// respective timers. A failure to do so will cause the | |||
// respecitve timers. A failure to do so will cause the |
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.
*respective
err = t.framer.fr.WriteHeaders(i.p) | ||
case *continuationFrame: | ||
err = t.framer.fr.WriteContinuation(i.streamID, i.endHeaders, i.headerBlockFragment) | ||
t.hBuf.Reset() |
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.
Can this logic be shared with the client's?
transport.go or control.go: headerFrameHandler(hEnc, hBuf, framer)
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.
Actually, most of these cases in item handler can be shared between the server and the client along with most of Write method's code and I'm guessing there are other such opportunities too scattered throughout the transport layer. I kept it this way to be consistent with the rest of the code and with hopes that one day we'll refactor the transport to take care of all this duplication.
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.
SGTM, let's try not to put it off for too long. Maybe add a TODO in this PR...
hpack encoding will now happen in the loopy writer goroutine right before the header is written on the wire. This is a more efficient use of hpack encoder since it maintains state and can be more performant in subsequent call.
As a result, the stats handler call with OutHeader can not include the wire length of the header written since it's not yet calculated. We might add this field back in the future which would require changing the code quite a bit.