Skip to content

Commit c314d58

Browse files
committed
TUN-5616: Never fallback transport if user chooses it on purpose
1 parent 628545d commit c314d58

File tree

3 files changed

+47
-36
lines changed

3 files changed

+47
-36
lines changed

connection/protocol.go

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ const (
1919
edgeH2TLSServerName = "h2.cftunnel.com"
2020
// edgeQUICServerName is the server name to establish quic connection with edge.
2121
edgeQUICServerName = "quic.cftunnel.com"
22-
// threshold to switch back to h2mux when the user intentionally pick --protocol http2
23-
explicitHTTP2FallbackThreshold = -1
24-
autoSelectFlag = "auto"
22+
autoSelectFlag = "auto"
2523
)
2624

2725
var (
@@ -237,25 +235,23 @@ func selectNamedTunnelProtocols(
237235
threshold int32,
238236
protocol Protocol,
239237
) (ProtocolSelector, error) {
240-
if protocolFlag == H2mux.String() {
241-
return &staticProtocolSelector{
242-
current: H2mux,
243-
}, nil
244-
}
245-
246-
if protocolFlag == QUIC.String() {
247-
return newAutoProtocolSelector(QUIC, []Protocol{QUIC, HTTP2, H2mux}, explicitHTTP2FallbackThreshold, fetchFunc, ttl, log), nil
238+
// If the user picks a protocol, then we stick to it no matter what.
239+
switch protocolFlag {
240+
case H2mux.String():
241+
return &staticProtocolSelector{current: H2mux}, nil
242+
case QUIC.String():
243+
return &staticProtocolSelector{current: QUIC}, nil
244+
case HTTP2.String():
245+
return &staticProtocolSelector{current: HTTP2}, nil
248246
}
249247

250-
if protocolFlag == HTTP2.String() {
251-
return newAutoProtocolSelector(HTTP2, []Protocol{HTTP2, H2mux}, explicitHTTP2FallbackThreshold, fetchFunc, ttl, log), nil
248+
// If the user does not pick (hopefully the majority) then we use the one derived from the TXT DNS record and
249+
// fallback on failures.
250+
if protocolFlag == autoSelectFlag {
251+
return newAutoProtocolSelector(protocol, []Protocol{QUIC, HTTP2, H2mux}, threshold, fetchFunc, ttl, log), nil
252252
}
253253

254-
if protocolFlag != autoSelectFlag {
255-
return nil, fmt.Errorf("Unknown protocol %s, %s", protocolFlag, AvailableProtocolFlagMessage)
256-
}
257-
258-
return newAutoProtocolSelector(protocol, []Protocol{QUIC, HTTP2, H2mux}, threshold, fetchFunc, ttl, log), nil
254+
return nil, fmt.Errorf("Unknown protocol %s, %s", protocolFlag, AvailableProtocolFlagMessage)
259255
}
260256

261257
func selectWarpRoutingProtocols(
@@ -266,19 +262,21 @@ func selectWarpRoutingProtocols(
266262
threshold int32,
267263
protocol Protocol,
268264
) (ProtocolSelector, error) {
269-
if protocolFlag == QUIC.String() {
270-
return newAutoProtocolSelector(QUICWarp, []Protocol{QUICWarp, HTTP2Warp}, explicitHTTP2FallbackThreshold, fetchFunc, ttl, log), nil
271-
}
272-
273-
if protocolFlag == HTTP2.String() {
274-
return newAutoProtocolSelector(HTTP2Warp, []Protocol{HTTP2Warp}, explicitHTTP2FallbackThreshold, fetchFunc, ttl, log), nil
265+
// If the user picks a protocol, then we stick to it no matter what.
266+
switch protocolFlag {
267+
case QUIC.String():
268+
return &staticProtocolSelector{current: QUICWarp}, nil
269+
case HTTP2.String():
270+
return &staticProtocolSelector{current: HTTP2Warp}, nil
275271
}
276272

277-
if protocolFlag != autoSelectFlag {
278-
return nil, fmt.Errorf("Unknown protocol %s, %s", protocolFlag, AvailableProtocolFlagMessage)
273+
// If the user does not pick (hopefully the majority) then we use the one derived from the TXT DNS record and
274+
// fallback on failures.
275+
if protocolFlag == autoSelectFlag {
276+
return newAutoProtocolSelector(protocol, []Protocol{QUICWarp, HTTP2Warp}, threshold, fetchFunc, ttl, log), nil
279277
}
280278

281-
return newAutoProtocolSelector(protocol, []Protocol{QUICWarp, HTTP2Warp}, threshold, fetchFunc, ttl, log), nil
279+
return nil, fmt.Errorf("Unknown protocol %s, %s", protocolFlag, AvailableProtocolFlagMessage)
282280
}
283281

284282
func switchThreshold(accountTag string) int32 {

connection/protocol_test.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,33 @@ func TestNewProtocolSelector(t *testing.T) {
7272
name: "named tunnel over http2",
7373
protocol: "http2",
7474
expectedProtocol: HTTP2,
75-
hasFallback: true,
76-
expectedFallback: H2mux,
7775
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 0}),
7876
namedTunnelConfig: testNamedTunnelConfig,
7977
},
8078
{
81-
name: "named tunnel http2 disabled",
79+
name: "named tunnel http2 disabled still gets http2 because it is manually picked",
8280
protocol: "http2",
83-
expectedProtocol: H2mux,
81+
expectedProtocol: HTTP2,
8482
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: -1}),
8583
namedTunnelConfig: testNamedTunnelConfig,
8684
},
85+
{
86+
name: "named tunnel quic disabled still gets quic because it is manually picked",
87+
protocol: "quic",
88+
expectedProtocol: QUIC,
89+
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 100}, edgediscovery.ProtocolPercent{Protocol: "quic", Percentage: -1}),
90+
namedTunnelConfig: testNamedTunnelConfig,
91+
},
92+
{
93+
name: "named tunnel quic and http2 disabled",
94+
protocol: "auto",
95+
expectedProtocol: H2mux,
96+
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: -1}, edgediscovery.ProtocolPercent{Protocol: "quic", Percentage: -1}),
97+
namedTunnelConfig: testNamedTunnelConfig,
98+
},
8799
{
88100
name: "named tunnel quic disabled",
89-
protocol: "quic",
101+
protocol: "auto",
90102
expectedProtocol: HTTP2,
91103
// Hasfallback true is because if http2 fails, then we further fallback to h2mux.
92104
hasFallback: true,
@@ -155,7 +167,7 @@ func TestNewProtocolSelector(t *testing.T) {
155167
},
156168
{
157169
name: "warp routing quic",
158-
protocol: "quic",
170+
protocol: "auto",
159171
expectedProtocol: QUICWarp,
160172
hasFallback: true,
161173
expectedFallback: HTTP2Warp,
@@ -254,6 +266,7 @@ func TestAutoProtocolSelectorRefresh(t *testing.T) {
254266

255267
func TestHTTP2ProtocolSelectorRefresh(t *testing.T) {
256268
fetcher := dynamicMockFetcher{}
269+
// Since the user chooses http2 on purpose, we always stick to it.
257270
selector, err := NewProtocolSelector("http2", noWarpRoutingEnabled, testNamedTunnelConfig, fetcher.fetch(), testNoTTL, &log)
258271
assert.NoError(t, err)
259272
assert.Equal(t, HTTP2, selector.Current())
@@ -269,7 +282,7 @@ func TestHTTP2ProtocolSelectorRefresh(t *testing.T) {
269282

270283
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: -1}}
271284
fetcher.err = nil
272-
assert.Equal(t, H2mux, selector.Current())
285+
assert.Equal(t, HTTP2, selector.Current())
273286

274287
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 0}}
275288
assert.Equal(t, HTTP2, selector.Current())
@@ -278,7 +291,7 @@ func TestHTTP2ProtocolSelectorRefresh(t *testing.T) {
278291
assert.Equal(t, HTTP2, selector.Current())
279292

280293
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: -1}}
281-
assert.Equal(t, H2mux, selector.Current())
294+
assert.Equal(t, HTTP2, selector.Current())
282295
}
283296

284297
func TestProtocolSelectorRefreshTTL(t *testing.T) {

origin/tunnel_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestWaitForBackoffFallback(t *testing.T) {
4141
}
4242
warpRoutingEnabled := false
4343
protocolSelector, err := connection.NewProtocolSelector(
44-
connection.HTTP2.String(),
44+
"auto",
4545
warpRoutingEnabled,
4646
namedTunnel,
4747
mockFetcher.fetch(),

0 commit comments

Comments
 (0)