Skip to content

Commit

Permalink
Each individual filesystem check gets it's own timeout
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Lorenz Kästle committed Mar 6, 2024
1 parent 0c3722e commit 1bd01ca
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
8 changes: 3 additions & 5 deletions cmd/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,10 @@ var diskCmd = &cobra.Command{
}

// Retrieve stats
intTimeout := time.Duration(Timeout/2) * time.Second
pCtx := context.Background()
ctx, cancel := context.WithTimeout(pCtx, intTimeout)
defer cancel()
internalTimeout := time.Duration(Timeout/2) * time.Second
ctx := context.Background()

err = filesystem.GetDiskUsage(ctx, filesystemList, &FsConfig)
err = filesystem.GetDiskUsage(ctx, internalTimeout, filesystemList, &FsConfig)
if err != nil {
check.ExitError(err)
}
Expand Down
13 changes: 9 additions & 4 deletions internal/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"regexp"
"time"

"github.com/shirou/gopsutil/v3/disk"
)
Expand All @@ -20,8 +21,12 @@ type tmpFileSystemWrapper struct {
err error
}

func GetDiskUsageSingle(ctx context.Context, fs *FilesystemType) {
func GetDiskUsageSingle(ctx context.Context, timeout time.Duration, fs *FilesystemType) {
myCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

resChan := make(chan tmpFileSystemWrapper, 1)

go func() {
tmp := tmpFileSystemWrapper{}
usageStats, err := disk.Usage(fs.PartStats.Mountpoint)
Expand All @@ -39,15 +44,15 @@ func GetDiskUsageSingle(ctx context.Context, fs *FilesystemType) {
}

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

func GetDiskUsage(ctx context.Context, fsList []FilesystemType, _ *CheckConfig) error {
func GetDiskUsage(ctx context.Context, timeout time.Duration, fsList []FilesystemType, _ *CheckConfig) error {
for index := range fsList {
GetDiskUsageSingle(ctx, &fsList[index])
GetDiskUsageSingle(ctx, timeout/time.Duration(len(fsList)), &fsList[index])
}

return nil
Expand Down

0 comments on commit 1bd01ca

Please sign in to comment.