Skip to content

Commit 9339bb9

Browse files
TUN-6929: Use same protocol for other connections as first one
This PR changes protocol initialization of the other N connections to be the same as the one we know the initial tunnel connected with. This is so we homogenize connections and not lead to some connections being QUIC-able and the others not. There's also an improvement to the connection registered log so we know what protocol every individual connection connected with from the cloudflared side.
1 parent 19106cd commit 9339bb9

File tree

4 files changed

+82
-3
lines changed

4 files changed

+82
-3
lines changed

connection/control.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (c *controlStream) ServeControlStream(
8585
return err
8686
}
8787

88-
c.observer.logServerInfo(c.connIndex, registrationDetails.Location, c.edgeAddress, fmt.Sprintf("Connection %s registered", registrationDetails.UUID))
88+
c.observer.logServerInfo(c.connIndex, registrationDetails.Location, c.edgeAddress, fmt.Sprintf("Connection %s registered with protocol: %s", registrationDetails.UUID, c.protocol))
8989
c.observer.sendConnectedEvent(c.connIndex, c.protocol, registrationDetails.Location)
9090
c.connectedFuse.Connected()
9191

supervisor/supervisor.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type Supervisor struct {
4545
config *TunnelConfig
4646
orchestrator *orchestration.Orchestrator
4747
edgeIPs *edgediscovery.Edge
48-
edgeTunnelServer *EdgeTunnelServer
48+
edgeTunnelServer TunnelServer
4949
tunnelErrors chan tunnelError
5050
tunnelsConnecting map[int]chan struct{}
5151
tunnelsProtocolFallback map[int]*protocolFallback
@@ -285,7 +285,8 @@ func (s *Supervisor) initialize(
285285
for i := 1; i < s.config.HAConnections; i++ {
286286
s.tunnelsProtocolFallback[i] = &protocolFallback{
287287
retry.BackoffHandler{MaxRetries: s.config.Retries, RetryForever: true},
288-
s.config.ProtocolSelector.Current(),
288+
// Set the protocol we know the first tunnel connected with.
289+
s.tunnelsProtocolFallback[0].protocol,
289290
false,
290291
}
291292
go s.startTunnel(ctx, i, s.newConnectedTunnelSignal(i))

supervisor/supervisor_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package supervisor
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/rs/zerolog"
8+
"github.com/stretchr/testify/assert"
9+
10+
"github.com/cloudflare/cloudflared/connection"
11+
"github.com/cloudflare/cloudflared/edgediscovery"
12+
"github.com/cloudflare/cloudflared/edgediscovery/allregions"
13+
"github.com/cloudflare/cloudflared/signal"
14+
)
15+
16+
type mockProtocolSelector struct {
17+
protocols []connection.Protocol
18+
index int
19+
}
20+
21+
func (m *mockProtocolSelector) Current() connection.Protocol {
22+
return m.protocols[m.index]
23+
}
24+
25+
func (m *mockProtocolSelector) Fallback() (connection.Protocol, bool) {
26+
m.index++
27+
if m.index == len(m.protocols) {
28+
return m.protocols[len(m.protocols)-1], false
29+
}
30+
31+
return m.protocols[m.index], true
32+
}
33+
34+
type mockEdgeTunnelServer struct {
35+
config *TunnelConfig
36+
}
37+
38+
func (m *mockEdgeTunnelServer) Serve(ctx context.Context, connIndex uint8, protocolFallback *protocolFallback, connectedSignal *signal.Signal) error {
39+
// This is to mock the first connection falling back because of connectivity issues.
40+
protocolFallback.protocol, _ = m.config.ProtocolSelector.Fallback()
41+
connectedSignal.Notify()
42+
return nil
43+
}
44+
45+
// Test to check if initialize sets all the different connections to the same protocol should the first
46+
// tunnel fall back.
47+
func Test_Initialize_Same_Protocol(t *testing.T) {
48+
edgeIPs, err := edgediscovery.ResolveEdge(&zerolog.Logger{}, "us", allregions.Auto)
49+
assert.Nil(t, err)
50+
s := Supervisor{
51+
edgeIPs: edgeIPs,
52+
config: &TunnelConfig{
53+
ProtocolSelector: &mockProtocolSelector{protocols: []connection.Protocol{connection.QUIC, connection.HTTP2, connection.H2mux}},
54+
},
55+
tunnelsProtocolFallback: make(map[int]*protocolFallback),
56+
edgeTunnelServer: &mockEdgeTunnelServer{
57+
config: &TunnelConfig{
58+
ProtocolSelector: &mockProtocolSelector{protocols: []connection.Protocol{connection.QUIC, connection.HTTP2, connection.H2mux}},
59+
},
60+
},
61+
}
62+
63+
ctx := context.Background()
64+
connectedSignal := signal.New(make(chan struct{}))
65+
s.initialize(ctx, connectedSignal)
66+
67+
// Make sure we fell back to http2 as the mock Serve is wont to do.
68+
assert.Equal(t, s.tunnelsProtocolFallback[0].protocol, connection.HTTP2)
69+
70+
// Ensure all the protocols we set to try are the same as what the first tunnel has fallen back to.
71+
for _, protocolFallback := range s.tunnelsProtocolFallback {
72+
assert.Equal(t, protocolFallback.protocol, s.tunnelsProtocolFallback[0].protocol)
73+
}
74+
}

supervisor/tunnel.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ type EdgeTunnelServer struct {
206206
connAwareLogger *ConnAwareLogger
207207
}
208208

209+
type TunnelServer interface {
210+
Serve(ctx context.Context, connIndex uint8, protocolFallback *protocolFallback, connectedSignal *signal.Signal) error
211+
}
212+
209213
func (e *EdgeTunnelServer) Serve(ctx context.Context, connIndex uint8, protocolFallback *protocolFallback, connectedSignal *signal.Signal) error {
210214
haConnections.Inc()
211215
defer haConnections.Dec()

0 commit comments

Comments
 (0)