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 fast path for the DefaultCertificateSelector #318

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,16 @@ func (cfg *Config) selectCert(hello *tls.ClientHelloInfo, name string) (Certific
// otherwise it returns an expired certificate that the client supports,
// otherwise it just returns the first certificate in the list of choices.
func DefaultCertificateSelector(hello *tls.ClientHelloInfo, choices []Certificate) (Certificate, error) {
if len(choices) == 1 {
// Fast path: There's only one choice, so we would always return that one
// regardless of whether it is expired or not compatible.
return choices[0], nil
}
if len(choices) == 0 {
return Certificate{}, fmt.Errorf("no certificates available")
}

// Slow path: There are choices, so we need to check each of them.
now := time.Now()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't check, but: There might be a good opportunity to split the slow path out into a separate function, so that the fast path can be inlined.

Copy link
Member

Choose a reason for hiding this comment

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

Should be doable. Lemme catch up on this next Caddy beta release and then I/we can explore that as a follow-up PR. I'd be curious if the inlining actually happens and if so does the performance improve?

best := choices[0]
for _, choice := range choices {
Expand Down
Loading