Skip to content

Commit 951d13d

Browse files
TUN-4456: Replaced instances of Tick() with Ticker() in h2mux paths
time.Tick() does not get garbage collected because the channel underneath never gets deleted and the underlying Ticker can never be recovered by the garbage collector. We replace this with NewTicker() to avoid this.
1 parent f99ae90 commit 951d13d

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

connection/h2mux.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ func (h *h2muxConnection) serveMuxer(ctx context.Context) error {
167167
}
168168

169169
func (h *h2muxConnection) controlLoop(ctx context.Context, connectedFuse ConnectedFuse, isNamedTunnel bool) {
170-
updateMetricsTickC := time.Tick(h.muxerConfig.MetricsUpdateFreq)
170+
updateMetricsTicker := time.NewTicker(h.muxerConfig.MetricsUpdateFreq)
171+
defer updateMetricsTicker.Stop()
171172
var shutdownCompleted <-chan struct{}
172173
for {
173174
select {
@@ -191,7 +192,7 @@ func (h *h2muxConnection) controlLoop(ctx context.Context, connectedFuse Connect
191192
// don't wait for shutdown to finish when context is closed, this is the hard termination path
192193
return
193194

194-
case <-updateMetricsTickC:
195+
case <-updateMetricsTicker.C:
195196
h.observer.metrics.updateMuxerMetrics(h.connIndexStr, h.muxer.Metrics())
196197
}
197198
}

h2mux/muxreader.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,13 @@ func (r *MuxReader) run(log *zerolog.Logger) error {
6969

7070
// routine to periodically update bytesRead
7171
go func() {
72-
tickC := time.Tick(updateFreq)
72+
ticker := time.NewTicker(updateFreq)
73+
defer ticker.Stop()
7374
for {
7475
select {
7576
case <-r.abortChan:
7677
return
77-
case <-tickC:
78+
case <-ticker.C:
7879
r.metricsUpdater.updateInBoundBytes(r.bytesRead.Count())
7980
}
8081
}

h2mux/muxwriter.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,13 @@ func (w *MuxWriter) run(log *zerolog.Logger) error {
7878

7979
// routine to periodically communicate bytesWrote
8080
go func() {
81-
tickC := time.Tick(updateFreq)
81+
ticker := time.NewTicker(updateFreq)
82+
defer ticker.Stop()
8283
for {
8384
select {
8485
case <-w.abortChan:
8586
return
86-
case <-tickC:
87+
case <-ticker.C:
8788
w.metricsUpdater.updateOutBoundBytes(w.bytesWrote.Count())
8889
}
8990
}

0 commit comments

Comments
 (0)