Skip to content

Use the context provide to UsageWithContext public function #1837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions disk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,31 @@ func Usage(path string) (*UsageStat, error) {
return UsageWithContext(context.Background(), path)
}

func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
type result struct {
usage *UsageStat
err error
}

resultCh := make(chan result, 1)

go func() {
usage, err := getUsage(ctx, path)
resultCh <- result{usage: usage, err: err}
}()

select {
case <-ctx.Done():
return nil, ctx.Err()
case res := <-resultCh:
if res.err != nil {
return nil, res.err
}

return res.usage, nil
}
}

// Partitions returns disk partitions. If all is false, returns
// physical devices only (e.g. hard disks, cd-rom drives, USB keys)
// and ignore all others (e.g. memory partitions such as /dev/shm)
Expand Down
2 changes: 1 addition & 1 deletion disk/disk_aix_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
return ret, err
}

func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
func getUsage(_ context.Context, path string) (*UsageStat, error) {
f, err := perfstat.FileSystemStat()
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion disk/disk_aix_nocgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func getFsType(stat unix.Statfs_t) string {
return FSType[int(stat.Vfstype)]
}

func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
func getUsage(ctx context.Context, path string) (*UsageStat, error) {
out, err := invoke.CommandWithContext(ctx, "df", "-v")
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion disk/disk_fallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func PartitionsWithContext(_ context.Context, _ bool) ([]PartitionStat, error) {
return []PartitionStat{}, common.ErrNotImplementedError
}

func UsageWithContext(_ context.Context, _ string) (*UsageStat, error) {
func getUsage(_ context.Context, _ string) (*UsageStat, error) {
return nil, common.ErrNotImplementedError
}

Expand Down
2 changes: 1 addition & 1 deletion disk/disk_netbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func IOCountersWithContext(_ context.Context, _ ...string) (map[string]IOCounter
return ret, common.ErrNotImplementedError
}

func UsageWithContext(_ context.Context, path string) (*UsageStat, error) {
func getUsage(_ context.Context, path string) (*UsageStat, error) {
stat := Statvfs{}
flag := uint64(1) // ST_WAIT/MNT_WAIT, see sys/fstypes.h

Expand Down
2 changes: 1 addition & 1 deletion disk/disk_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func parseDiskstats(buf []byte) (Diskstats, error) {
return ds, nil
}

func UsageWithContext(_ context.Context, path string) (*UsageStat, error) {
func getUsage(_ context.Context, path string) (*UsageStat, error) {
stat := unix.Statfs_t{}
err := unix.Statfs(path, &stat)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion disk/disk_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOC
return ret, nil
}

func UsageWithContext(_ context.Context, path string) (*UsageStat, error) {
func getUsage(_ context.Context, path string) (*UsageStat, error) {
statvfs := unix.Statvfs_t{}
if err := unix.Statvfs(path, &statvfs); err != nil {
return nil, fmt.Errorf("unable to call statvfs(2) on %q: %w", path, err)
Expand Down
2 changes: 1 addition & 1 deletion disk/disk_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"golang.org/x/sys/unix"
)

func UsageWithContext(_ context.Context, path string) (*UsageStat, error) {
func getUsage(_ context.Context, path string) (*UsageStat, error) {
stat := unix.Statfs_t{}
err := unix.Statfs(path, &stat)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion disk/disk_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func init() {
}
}

func UsageWithContext(_ context.Context, path string) (*UsageStat, error) {
func getUsage(_ context.Context, path string) (*UsageStat, error) {
lpFreeBytesAvailable := int64(0)
lpTotalNumberOfBytes := int64(0)
lpTotalNumberOfFreeBytes := int64(0)
Expand Down