-
Notifications
You must be signed in to change notification settings - Fork 4.6k
tls: append h2 to tlsconfig.NextProtos #2744
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,9 +36,6 @@ import ( | |
| "google.golang.org/grpc/credentials/internal" | ||
| ) | ||
|
|
||
| // alpnProtoStr are the specified application level protocols for gRPC. | ||
| var alpnProtoStr = []string{"h2"} | ||
|
|
||
| // PerRPCCredentials defines the common interface for the credentials which need to | ||
| // attach security information to every RPC (e.g., oauth2). | ||
| type PerRPCCredentials interface { | ||
|
|
@@ -208,10 +205,23 @@ func (c *tlsCreds) OverrideServerName(serverNameOverride string) error { | |
| return nil | ||
| } | ||
|
|
||
| const alpnProtoStrH2 = "h2" | ||
|
|
||
| func appendH2ToNextProtos(ps []string) []string { | ||
| for _, p := range ps { | ||
| if p == alpnProtoStrH2 { | ||
| return ps | ||
| } | ||
| } | ||
| ret := make([]string, 0, len(ps)+1) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like an unnecessary optimisation since the underlying array would at most end up as 4-10 elements in usual cases if the capacity was spent before an append. And this only happens when creating a new transport credential. Out of curiosity: is there something I'm missing?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't modify the underlying array, as it's owned by the user, so it needs to be copied first. Since we know the final size of it, there's no reason not to allocate the right amount of memory up front. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the explanation dfawley, that makes a lot of sense! |
||
| ret = append(ret, ps...) | ||
| return append(ret, alpnProtoStrH2) | ||
| } | ||
|
|
||
| // NewTLS uses c to construct a TransportCredentials based on TLS. | ||
| func NewTLS(c *tls.Config) TransportCredentials { | ||
| tc := &tlsCreds{cloneTLSConfig(c)} | ||
| tc.config.NextProtos = alpnProtoStr | ||
| tc.config.NextProtos = appendH2ToNextProtos(tc.config.NextProtos) | ||
| return tc | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.