Skip to content

Commit

Permalink
logging: add a Close function to the Tracer (#4298)
Browse files Browse the repository at this point in the history
* logging: add a Close function to the Tracer

* close the Tracer when the Transport is closed
  • Loading branch information
marten-seemann committed Feb 3, 2024
1 parent b675e34 commit 07ec324
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 1 deletion.
36 changes: 36 additions & 0 deletions internal/mocks/logging/internal/tracer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/mocks/logging/mockgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Tracer interface {
SentVersionNegotiationPacket(_ net.Addr, dest, src logging.ArbitraryLenConnectionID, _ []logging.VersionNumber)
DroppedPacket(net.Addr, logging.PacketType, logging.ByteCount, logging.PacketDropReason)
Debug(name, msg string)
Close()
}

//go:generate sh -c "go run go.uber.org/mock/mockgen -typed -build_flags=\"-tags=gomock\" -package internal -destination internal/connection_tracer.go github.com/quic-go/quic-go/internal/mocks/logging ConnectionTracer"
Expand Down
3 changes: 3 additions & 0 deletions internal/mocks/logging/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@ func NewMockTracer(ctrl *gomock.Controller) (*logging.Tracer, *MockTracer) {
Debug: func(name, msg string) {
t.Debug(name, msg)
},
Close: func() {
t.Close()
},
}, t
}
6 changes: 6 additions & 0 deletions logging/multiplex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ var _ = Describe("Tracing", func() {
tr2.EXPECT().Debug("foo", "bar")
tracer.Debug("foo", "bar")
})

It("traces the Close event", func() {
tr1.EXPECT().Close()
tr2.EXPECT().Close()
tracer.Close()
})
})
})

Expand Down
8 changes: 8 additions & 0 deletions logging/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Tracer struct {
SentVersionNegotiationPacket func(_ net.Addr, dest, src ArbitraryLenConnectionID, _ []VersionNumber)
DroppedPacket func(net.Addr, PacketType, ByteCount, PacketDropReason)
Debug func(name, msg string)
Close func()
}

// NewMultiplexedTracer creates a new tracer that multiplexes events to multiple tracers.
Expand Down Expand Up @@ -47,5 +48,12 @@ func NewMultiplexedTracer(tracers ...*Tracer) *Tracer {
}
}
},
Close: func() {
for _, t := range tracers {
if t.Close != nil {
t.Close()
}
}
},
}
}
6 changes: 5 additions & 1 deletion server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ var _ = Describe("Server", func() {
})

AfterEach(func() {
tracer.EXPECT().Close()
tr.Close()
})

Expand Down Expand Up @@ -1429,7 +1430,10 @@ var _ = Describe("Server", func() {
serv.connHandler = phm
})

AfterEach(func() { tr.Close() })
AfterEach(func() {
tracer.EXPECT().Close()
tr.Close()
})

It("passes packets to existing connections", func() {
connID := protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8})
Expand Down
4 changes: 4 additions & 0 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ type Transport struct {
MaxHandshakes int

// A Tracer traces events that don't belong to a single QUIC connection.
// Tracer.Close is called when the transport is closed.
Tracer *logging.Tracer

handlerMap packetHandlerManager
Expand Down Expand Up @@ -366,6 +367,9 @@ func (t *Transport) close(e error) {
if t.server != nil {
t.server.close(e, false)
}
if t.Tracer != nil && t.Tracer.Close != nil {
t.Tracer.Close()
}
t.closed = true
}

Expand Down
2 changes: 2 additions & 0 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ var _ = Describe("Transport", func() {
Eventually(dropped).Should(BeClosed())

// shutdown
tracer.EXPECT().Close()
close(packetChan)
tr.Close()
})
Expand Down Expand Up @@ -391,6 +392,7 @@ var _ = Describe("Transport", func() {
Eventually(done).Should(BeClosed())

// shutdown
tracer.EXPECT().Close()
close(packetChan)
tr.Close()
})
Expand Down

0 comments on commit 07ec324

Please sign in to comment.