mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-18 02:40:45 +00:00
metrics: replace time.Tick with time.NewTicker to prevent resource leaks
time.Tick() creates a ticker whose underlying resources can never be released because there is no handle to call Stop(). This is flagged by go vet (SA1015). 6 files in metrics/ fixed: debug.go, json.go, log.go, opentsdb.go, syslog.go, writer.go. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
ab357151da
commit
9137152e19
6 changed files with 18 additions and 6 deletions
|
|
@ -22,7 +22,9 @@ var (
|
||||||
// CaptureDebugGCStats captures new values for the Go garbage collector statistics
|
// CaptureDebugGCStats captures new values for the Go garbage collector statistics
|
||||||
// exported in debug.GCStats. This is designed to be called as a goroutine.
|
// exported in debug.GCStats. This is designed to be called as a goroutine.
|
||||||
func CaptureDebugGCStats(r Registry, d time.Duration) {
|
func CaptureDebugGCStats(r Registry, d time.Duration) {
|
||||||
for range time.Tick(d) {
|
ticker := time.NewTicker(d)
|
||||||
|
defer ticker.Stop()
|
||||||
|
for range ticker.C {
|
||||||
CaptureDebugGCStatsOnce(r)
|
CaptureDebugGCStatsOnce(r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,9 @@ func (r *StandardRegistry) MarshalJSON() ([]byte, error) {
|
||||||
// WriteJSON writes metrics from the given registry periodically to the
|
// WriteJSON writes metrics from the given registry periodically to the
|
||||||
// specified io.Writer as JSON.
|
// specified io.Writer as JSON.
|
||||||
func WriteJSON(r Registry, d time.Duration, w io.Writer) {
|
func WriteJSON(r Registry, d time.Duration, w io.Writer) {
|
||||||
for range time.Tick(d) {
|
ticker := time.NewTicker(d)
|
||||||
|
defer ticker.Stop()
|
||||||
|
for range ticker.C {
|
||||||
WriteJSONOnce(r, w)
|
WriteJSONOnce(r, w)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,9 @@ func LogScaled(r Registry, freq time.Duration, scale time.Duration, l Logger) {
|
||||||
du := float64(scale)
|
du := float64(scale)
|
||||||
duSuffix := scale.String()[1:]
|
duSuffix := scale.String()[1:]
|
||||||
|
|
||||||
for range time.Tick(freq) {
|
ticker := time.NewTicker(freq)
|
||||||
|
defer ticker.Stop()
|
||||||
|
for range ticker.C {
|
||||||
r.Each(func(name string, i interface{}) {
|
r.Each(func(name string, i interface{}) {
|
||||||
switch metric := i.(type) {
|
switch metric := i.(type) {
|
||||||
case *Counter:
|
case *Counter:
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,9 @@ func OpenTSDB(r Registry, d time.Duration, prefix string, addr *net.TCPAddr) {
|
||||||
// OpenTSDBWithConfig is a blocking exporter function just like OpenTSDB,
|
// OpenTSDBWithConfig is a blocking exporter function just like OpenTSDB,
|
||||||
// but it takes a OpenTSDBConfig instead.
|
// but it takes a OpenTSDBConfig instead.
|
||||||
func OpenTSDBWithConfig(c OpenTSDBConfig) {
|
func OpenTSDBWithConfig(c OpenTSDBConfig) {
|
||||||
for range time.Tick(c.FlushInterval) {
|
ticker := time.NewTicker(c.FlushInterval)
|
||||||
|
defer ticker.Stop()
|
||||||
|
for range ticker.C {
|
||||||
if err := openTSDB(&c); nil != err {
|
if err := openTSDB(&c); nil != err {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,9 @@ import (
|
||||||
// Syslog outputs each metric in the given registry to syslog periodically using
|
// Syslog outputs each metric in the given registry to syslog periodically using
|
||||||
// the given syslogger.
|
// the given syslogger.
|
||||||
func Syslog(r Registry, d time.Duration, w *syslog.Writer) {
|
func Syslog(r Registry, d time.Duration, w *syslog.Writer) {
|
||||||
for range time.Tick(d) {
|
ticker := time.NewTicker(d)
|
||||||
|
defer ticker.Stop()
|
||||||
|
for range ticker.C {
|
||||||
r.Each(func(name string, i interface{}) {
|
r.Each(func(name string, i interface{}) {
|
||||||
switch metric := i.(type) {
|
switch metric := i.(type) {
|
||||||
case *Counter:
|
case *Counter:
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,9 @@ import (
|
||||||
// Write sorts writes each metric in the given registry periodically to the
|
// Write sorts writes each metric in the given registry periodically to the
|
||||||
// given io.Writer.
|
// given io.Writer.
|
||||||
func Write(r Registry, d time.Duration, w io.Writer) {
|
func Write(r Registry, d time.Duration, w io.Writer) {
|
||||||
for range time.Tick(d) {
|
ticker := time.NewTicker(d)
|
||||||
|
defer ticker.Stop()
|
||||||
|
for range ticker.C {
|
||||||
WriteOnce(r, w)
|
WriteOnce(r, w)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue