mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-27 20:50:00 +00:00
TUN-3635: Send event when unregistering tunnel for gracful shutdown so /ready endpoint reports down status befoe connections finish handling pending requests.
This commit is contained in:
@@ -27,7 +27,6 @@ var (
|
||||
Scheme: "https",
|
||||
Host: "connectiontest.argotunnel.com",
|
||||
}
|
||||
testObserver = NewObserver(&log, &log, false)
|
||||
testLargeResp = make([]byte, largeFileSize)
|
||||
)
|
||||
|
||||
|
@@ -22,4 +22,6 @@ const (
|
||||
SetURL
|
||||
// RegisteringTunnel means the non-named tunnel is registering its connection.
|
||||
RegisteringTunnel
|
||||
// We're unregistering tunnel from the edge in preparation for a disconnect
|
||||
Unregistering
|
||||
)
|
||||
|
@@ -33,7 +33,7 @@ func newH2MuxConnection(t require.TestingT) (*h2muxConnection, *h2mux.Muxer) {
|
||||
edgeMuxChan := make(chan *h2mux.Muxer)
|
||||
go func() {
|
||||
edgeMuxConfig := h2mux.MuxerConfig{
|
||||
Log: testObserver.log,
|
||||
Log: &log,
|
||||
Handler: h2mux.MuxedStreamFunc(func(stream *h2mux.MuxedStream) error {
|
||||
// we only expect RPC traffic in client->edge direction, provide minimal support for mocking
|
||||
require.True(t, stream.IsRPCStream())
|
||||
@@ -47,6 +47,7 @@ func newH2MuxConnection(t require.TestingT) (*h2muxConnection, *h2mux.Muxer) {
|
||||
edgeMuxChan <- edgeMux
|
||||
}()
|
||||
var connIndex = uint8(0)
|
||||
testObserver := NewObserver(&log, &log, false)
|
||||
h2muxConn, err, _ := NewH2muxConnection(testConfig, testMuxerConfig, originConn, connIndex, testObserver, nil)
|
||||
require.NoError(t, err)
|
||||
return h2muxConn, <-edgeMuxChan
|
||||
|
@@ -142,6 +142,7 @@ func (c *http2Connection) serveControlStream(ctx context.Context, respWriter *ht
|
||||
c.stoppedGracefully = true
|
||||
}
|
||||
|
||||
c.observer.sendUnregisteringEvent(c.connIndex)
|
||||
rpcClient.GracefulShutdown(ctx, c.config.GracePeriod)
|
||||
c.observer.log.Info().Uint8(LogFieldConnIndex, c.connIndex).Msg("Unregistered tunnel connection")
|
||||
return nil
|
||||
|
@@ -35,7 +35,7 @@ func newTestHTTP2Connection() (*http2Connection, net.Conn) {
|
||||
testConfig,
|
||||
&NamedTunnelConfig{},
|
||||
&pogs.ConnectionOptions{},
|
||||
testObserver,
|
||||
NewObserver(&log, &log, false),
|
||||
connIndex,
|
||||
mockConnectedFuse{},
|
||||
nil,
|
||||
@@ -256,7 +256,9 @@ func TestGracefulShutdownHTTP2(t *testing.T) {
|
||||
registered: make(chan struct{}),
|
||||
unregistered: make(chan struct{}),
|
||||
}
|
||||
events := &eventCollectorSink{}
|
||||
http2Conn.newRPCClientFunc = rpcClientFactory.newMockRPCClient
|
||||
http2Conn.observer.RegisterSink(events)
|
||||
shutdownC := make(chan struct{})
|
||||
http2Conn.gracefulShutdownC = shutdownC
|
||||
|
||||
@@ -301,6 +303,11 @@ func TestGracefulShutdownHTTP2(t *testing.T) {
|
||||
|
||||
cancel()
|
||||
wg.Wait()
|
||||
|
||||
events.assertSawEvent(t, Event{
|
||||
Index: http2Conn.connIndex,
|
||||
EventType: Unregistering,
|
||||
})
|
||||
}
|
||||
|
||||
func benchmarkServeHTTP(b *testing.B, test testRequest) {
|
||||
|
@@ -117,6 +117,10 @@ func (o *Observer) SendReconnect(connIndex uint8) {
|
||||
o.sendEvent(Event{Index: connIndex, EventType: Reconnecting})
|
||||
}
|
||||
|
||||
func (o *Observer) sendUnregisteringEvent(connIndex uint8) {
|
||||
o.sendEvent(Event{Index: connIndex, EventType: Unregistering})
|
||||
}
|
||||
|
||||
func (o *Observer) SendDisconnect(connIndex uint8) {
|
||||
o.sendEvent(Event{Index: connIndex, EventType: Disconnected})
|
||||
}
|
||||
|
@@ -66,3 +66,21 @@ func TestObserverEventsDontBlock(t *testing.T) {
|
||||
mu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
type eventCollectorSink struct {
|
||||
observedEvents []Event
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func (s *eventCollectorSink) OnTunnelEvent(event Event) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.observedEvents = append(s.observedEvents, event)
|
||||
}
|
||||
|
||||
func (s *eventCollectorSink) assertSawEvent(t *testing.T, event Event) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
assert.Contains(t, s.observedEvents, event)
|
||||
}
|
@@ -291,6 +291,8 @@ func (h *h2muxConnection) registerNamedTunnel(
|
||||
}
|
||||
|
||||
func (h *h2muxConnection) unregister(isNamedTunnel bool) {
|
||||
h.observer.sendUnregisteringEvent(h.connIndex)
|
||||
|
||||
unregisterCtx, cancel := context.WithTimeout(context.Background(), h.config.GracePeriod)
|
||||
defer cancel()
|
||||
|
||||
|
Reference in New Issue
Block a user