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 client ALPN support #2251

Merged
merged 6 commits into from
Dec 25, 2023
Merged

Add client ALPN support #2251

merged 6 commits into from
Dec 25, 2023

Conversation

Menci
Copy link
Contributor

@Menci Menci commented May 16, 2023

What problem does this PR solve?

Issue Number: #1991

Problem Summary:

What is changed and the side effects?

Changed:

In HTTP/2 standard a client should negotiate the protocol with server with ALPN. The client should send a list of protocols it support and the server will choose one. Normally curl sends http1.1 and h2. A standard client should send H2 requests in TLS payload only if the server chooses h2 in ALPN.

We tried a deployment with Nginx (TLS termination and grpc_pass to backend) in front of BRPC and found Nginx is treating BRPC client's H2 payload as H1 since there's no ALPN. It's confirmed that adding client side ALPN fixes it.

In this PR, if options.alpn_protocols is set in SSL options, the client will send ALPN extension during SSL handshake and check if the server responded with a acceptable protocol name. Normally we could set alpn_protocols to {"h2"} to only use H2. Note that the implementation will raise an error if the server returns no ALPN selection or unacceptable ALPN selection. Currently BRPC has no server side ALPN support (until #2102 is merged) so using this option with no HTTPS reverse proxy in front of BRPC server will not work.

// Example to work with "h2:grpc" protocol
channel_options.mutable_ssl_options()->alpn_protocols = {"h2"};

Client side ALPN must be set manually since it requires server side ALPN support. By default it's unset and the behavior is like without this feature.

Side effects:

  • Performance effects(性能影响): If the client side ALPN feature is used, a little more memory allocations are happened during client side SSL connection setup and handshake.

  • Breaking backward compatibility(向后兼容性): No effect


Check List:

  • Please make sure your changes are compilable(请确保你的更改可以通过编译).
  • When providing us with a new feature, it is best to add related tests(如果你向我们增加一个新的功能, 请添加相关测试).
  • Please follow Contributor Covenant Code of Conduct.(请遵循贡献者准则).

@Menci
Copy link
Contributor Author

Menci commented May 20, 2023

The tests succeeded in my fork's CI. https://github.com/Menci/brpc/actions/runs/5030791101/jobs/9023448641

Maybe rerun it?

size_t alpn_list_length = 0;
for (const auto& alpn_protocol : alpn_protocols) {
if (alpn_protocol.size() > UCHAR_MAX) {
LOG(ERROR) << "Fail to build ALPN procotol list: "
Copy link
Contributor

Choose a reason for hiding this comment

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

这里把alpn_protocol.size()打印一下?

if (!BuildALPNProtocolList(options.alpn_protocols, alpn_list)) {
return NULL;
}
SSL_CTX_set_alpn_protos(ssl_ctx.get(), alpn_list.data(), alpn_list.size());
Copy link
Contributor

Choose a reason for hiding this comment

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

mesalink是否支持这个功能?
mesalock-linux/mesalink#36

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好问题,要不我就把 mesalink 的部分删掉吧,顺便我应该怎么测试 mesalink 呢

@wwbmmm
Copy link
Contributor

wwbmmm commented Jun 25, 2023

LGTM
@Menci 能否更新一下用户文档

@wwbmmm
Copy link
Contributor

wwbmmm commented Oct 25, 2023

和master分支冲突了 @Menci

@Menci
Copy link
Contributor Author

Menci commented Dec 10, 2023

@wwbmmm resolved 了 conflict,添加了文档~

看到 server side 的 ALPN 已经进了,思考了一下没有和它一样用 AdaptiveProtocolType,因为 client 端并不知道目标的服务器端是什么,所以就不做相关的检查了。

@wwbmmm
Copy link
Contributor

wwbmmm commented Dec 19, 2023

LGTM

@Menci
Copy link
Contributor Author

Menci commented Dec 22, 2023

谁可以来 merge 一下呢

@wwbmmm wwbmmm merged commit 4b14951 into apache:master Dec 25, 2023
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants