Skip to content

Commit

Permalink
Simplified ALPN API for server side
Browse files Browse the repository at this point in the history
Summary:
Simplify the API so that it's more concise and easier for the end user.

ALPN enablement before this API: D64625041
ALPN enablement after this API: D65916844

Reviewed By: awalterschulze

Differential Revision: D65275022

fbshipit-source-id: 85d88fac5b0581d5e244b770375e990997071702
  • Loading branch information
echistyakov authored and facebook-github-bot committed Nov 15, 2024
1 parent 204e65f commit 0262e1e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
22 changes: 22 additions & 0 deletions thrift/lib/go/thrift/server_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package thrift

import (
"context"
"crypto/tls"
"log"
"net"
"os"
Expand Down Expand Up @@ -120,3 +121,24 @@ func WithProcessorStats(processorStats map[string]*stats.TimingSeries) ServerOpt
server.processorStats = processorStats
}
}

// WithALPNHeader adds the ALPN value "thrift" to the provided tls.Config
func WithALPNHeader() func(*tls.Config) {
return func(tlsConfig *tls.Config) {
tlsConfig.NextProtos = []string{"thrift"}
}
}

// WithALPNRocket adds the ALPN value "rs" to the provided tls.Config
func WithALPNRocket() func(*tls.Config) {
return func(tlsConfig *tls.Config) {
tlsConfig.NextProtos = []string{"rs"}
}
}

// WithALPNUpgradeToRocket adds the ALPN value "rs" and "thrift" to the provided tls.Config
func WithALPNUpgradeToRocket() func(*tls.Config) {
return func(tlsConfig *tls.Config) {
tlsConfig.NextProtos = []string{"rs" /* preferred */, "thrift" /* fallback */}
}
}
45 changes: 45 additions & 0 deletions thrift/lib/go/thrift/server_options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package thrift

import (
"crypto/tls"
"reflect"
"testing"
)

func TestListenerALPNOptions(t *testing.T) {
testCases := []struct {
name string
option func(*tls.Config)
expectedNextProtos []string
}{
{"Header", WithALPNHeader(), []string{"thrift"}},
{"Rocket", WithALPNRocket(), []string{"rs"}},
{"UpgradeToRocket", WithALPNUpgradeToRocket(), []string{"rs", "thrift"}},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tlsConfig := &tls.Config{}
tc.option(tlsConfig)
if !reflect.DeepEqual(tlsConfig.NextProtos, tc.expectedNextProtos) {
t.Fatalf("unexpected NextProtos: %v (expected), %v (actual)", tc.expectedNextProtos, tlsConfig.NextProtos)
}
})
}
}

0 comments on commit 0262e1e

Please sign in to comment.