Skip to content

Commit 1bd01ca

Browse files
author
Lorenz Kästle
committed
Each individual filesystem check gets it's own timeout
This should fix an error whith the previous commits where more than two hanging filesystems could cause an early abort and potentially wrong or at least misleading results
1 parent 0c3722e commit 1bd01ca

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

cmd/filesystem.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,10 @@ var diskCmd = &cobra.Command{
139139
}
140140

141141
// Retrieve stats
142-
intTimeout := time.Duration(Timeout/2) * time.Second
143-
pCtx := context.Background()
144-
ctx, cancel := context.WithTimeout(pCtx, intTimeout)
145-
defer cancel()
142+
internalTimeout := time.Duration(Timeout/2) * time.Second
143+
ctx := context.Background()
146144

147-
err = filesystem.GetDiskUsage(ctx, filesystemList, &FsConfig)
145+
err = filesystem.GetDiskUsage(ctx, internalTimeout, filesystemList, &FsConfig)
148146
if err != nil {
149147
check.ExitError(err)
150148
}

internal/filesystem/filesystem.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"regexp"
7+
"time"
78

89
"github.com/shirou/gopsutil/v3/disk"
910
)
@@ -20,8 +21,12 @@ type tmpFileSystemWrapper struct {
2021
err error
2122
}
2223

23-
func GetDiskUsageSingle(ctx context.Context, fs *FilesystemType) {
24+
func GetDiskUsageSingle(ctx context.Context, timeout time.Duration, fs *FilesystemType) {
25+
myCtx, cancel := context.WithTimeout(ctx, timeout)
26+
defer cancel()
27+
2428
resChan := make(chan tmpFileSystemWrapper, 1)
29+
2530
go func() {
2631
tmp := tmpFileSystemWrapper{}
2732
usageStats, err := disk.Usage(fs.PartStats.Mountpoint)
@@ -39,15 +44,15 @@ func GetDiskUsageSingle(ctx context.Context, fs *FilesystemType) {
3944
}
4045

4146
fs.UsageStats = tmp.usage
42-
case <-ctx.Done():
47+
case <-myCtx.Done():
4348
err := errors.New("Timeout exceeded for fs " + fs.PartStats.Mountpoint + ". Maybe hanging network filesystem?")
4449
fs.Error = err
4550
}
4651
}
4752

48-
func GetDiskUsage(ctx context.Context, fsList []FilesystemType, _ *CheckConfig) error {
53+
func GetDiskUsage(ctx context.Context, timeout time.Duration, fsList []FilesystemType, _ *CheckConfig) error {
4954
for index := range fsList {
50-
GetDiskUsageSingle(ctx, &fsList[index])
55+
GetDiskUsageSingle(ctx, timeout/time.Duration(len(fsList)), &fsList[index])
5156
}
5257

5358
return nil

0 commit comments

Comments
 (0)