Skip to content

Commit a289a6a

Browse files
author
bawenmao
committed
Avoid time.after memory leak when calling time.After
Signed-off-by: bawenmao <bawenmao@sogou-inc.com>
1 parent 5a38949 commit a289a6a

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

collector/filesystem_linux.go

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,28 @@ func (c *filesystemCollector) GetStats() ([]filesystemStats, error) {
5050
return nil, err
5151
}
5252
stats := []filesystemStats{}
53+
mountCheckTimer := time.NewTimer(*mountTimeout)
54+
defer mountCheckTimer.Stop()
55+
// stuckMountWatcher listens on the given success channel and if the channel closes
56+
// then the watcher does nothing. If instead the timeout is reached, the
57+
// mount point that is being watched is marked as stuck.
58+
stuckMountWatcher := func(mountPoint string, success chan struct{}, logger log.Logger) {
59+
select {
60+
case <-success:
61+
// Success
62+
case <-mountCheckTimer.C:
63+
// Timed out, mark mount as stuck
64+
stuckMountsMtx.Lock()
65+
select {
66+
case <-success:
67+
// Success came in just after the timeout was reached, don't label the mount as stuck
68+
default:
69+
level.Debug(logger).Log("msg", "Mount point timed out, it is being labeled as stuck and will not be monitored", "mountpoint", mountPoint)
70+
stuckMounts[mountPoint] = struct{}{}
71+
}
72+
stuckMountsMtx.Unlock()
73+
}
74+
}
5375
for _, labels := range mps {
5476
if c.excludedMountPointsPattern.MatchString(labels.mountPoint) {
5577
level.Debug(c.logger).Log("msg", "Ignoring mount point", "mountpoint", labels.mountPoint)
@@ -118,27 +140,6 @@ func (c *filesystemCollector) GetStats() ([]filesystemStats, error) {
118140
return stats, nil
119141
}
120142

121-
// stuckMountWatcher listens on the given success channel and if the channel closes
122-
// then the watcher does nothing. If instead the timeout is reached, the
123-
// mount point that is being watched is marked as stuck.
124-
func stuckMountWatcher(mountPoint string, success chan struct{}, logger log.Logger) {
125-
select {
126-
case <-success:
127-
// Success
128-
case <-time.After(*mountTimeout):
129-
// Timed out, mark mount as stuck
130-
stuckMountsMtx.Lock()
131-
select {
132-
case <-success:
133-
// Success came in just after the timeout was reached, don't label the mount as stuck
134-
default:
135-
level.Debug(logger).Log("msg", "Mount point timed out, it is being labeled as stuck and will not be monitored", "mountpoint", mountPoint)
136-
stuckMounts[mountPoint] = struct{}{}
137-
}
138-
stuckMountsMtx.Unlock()
139-
}
140-
}
141-
142143
func mountPointDetails(logger log.Logger) ([]filesystemLabels, error) {
143144
file, err := os.Open(procFilePath("1/mounts"))
144145
if errors.Is(err, os.ErrNotExist) {

0 commit comments

Comments
 (0)