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 a gRPC Web to HTTP/2 codec #1047

Merged
merged 1 commit into from
Nov 20, 2020
Merged

Add a gRPC Web to HTTP/2 codec #1047

merged 1 commit into from
Nov 20, 2020

Conversation

glbrntt
Copy link
Collaborator

@glbrntt glbrntt commented Nov 19, 2020

Motivation:

Following from #1041, we'd like to switch our currency type on the
server from HTTP/1 to HTTP/2. This means that to retain support for gRPC
Web we need a codec to convert between HTTP/1 and HTTP/2 in the opposite
direction to the codecs provided by NIO HTTP/2.

Modifications:

  • Add a gRPC Web to HTTP2 codec

Result:

  • Nothing yet, as this remains unused.

@glbrntt glbrntt added the 🆕 semver/minor Adds new public API. label Nov 19, 2020
@glbrntt glbrntt requested a review from Lukasa November 19, 2020 13:39
) -> GRPCWebToHTTP2ServerCodec.StateMachine.Action {
switch self {
case .idle:
var headers = HPACKHeaders(httpHeaders: head.headers, normalizeHTTPHeaders: true)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we reserve capacity here?

case .idle:
var headers = HPACKHeaders(httpHeaders: head.headers, normalizeHTTPHeaders: true)
headers.add(name: ":path", value: head.uri)
headers.add(name: ":method", value: head.method.rawValue)
Copy link
Collaborator

Choose a reason for hiding this comment

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

These pseudo-headers are surely not going to be in the right place in the block, right? And I think we're missing two: :authority and :scheme are both expected as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I didn't think the order mattered given these will only be read by the HTTP/2 to gRPC codec? Easy enough to change though.

I wasn't sure about including :authority and :scheme; they're also not required but I suppose they might be expected by a user in e.g. an interceptor. WDYT?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm inclined to suggest that we should do the right thing here. It reduces the risk that we might accidentally introduce a component that doesn't expect this and get issues down the road. The same applies to adding the extra headers.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That seems wise. Done!

self.init()
self.reserveCapacity(headers.count)
// Don't add any pseudo-headers.
for (name, value, _) in headers where name.utf8.first != .some(UInt8(ascii: ":")) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

There's a mild perf improvement available here: well-formatted HTTP/2 header blocks must have all their pseudo-headers first. You can therefore do a dropFirst(where:) to only look at the minimal number of headers and then just do add(contentsOf:) or similar to reduce the amount of unnecessary checking you do.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh, smart thinking, I like it!

Motivation:

Following from grpc#1041, we'd like to switch our currency type on the
server from HTTP/1 to HTTP/2. This means that to retain support for gRPC
Web we need a codec to convert between HTTP/1 and HTTP/2 in the opposite
direction to the codecs provided by NIO HTTP/2.

Modifications:

- Add a gRPC Web to HTTP2 codec

Result:

- Nothing yet, as this remains unused.
@glbrntt glbrntt merged commit cdd3538 into grpc:main Nov 20, 2020
@glbrntt glbrntt deleted the gb-grpc-web branch November 20, 2020 15:17
@glbrntt glbrntt added 🔨 semver/patch No public API change. and removed 🆕 semver/minor Adds new public API. labels Nov 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants