From 43691b65a049f692ec4a31d89c5dca808ef4ab43 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 8 Feb 2021 14:23:37 +0200 Subject: [PATCH] Read /proc/* file contents with ioutil.ReadFile() instead of opening it with os.Open() and scanning with bufio.Scanner This simplifies the code a bit. --- process_metrics_linux.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/process_metrics_linux.go b/process_metrics_linux.go index d8b7c73..f04a503 100644 --- a/process_metrics_linux.go +++ b/process_metrics_linux.go @@ -1,7 +1,6 @@ package metrics import ( - "bufio" "bytes" "fmt" "io" @@ -123,15 +122,13 @@ func getOpenFDsCount(path string) (uint64, error) { } func getMaxFilesLimit(path string) (uint64, error) { - f, err := os.Open(path) + data, err := ioutil.ReadFile(path) if err != nil { return 0, err } - defer f.Close() - prefix := "Max open files" - scan := bufio.NewScanner(f) - for scan.Scan() { - s := scan.Text() + lines := strings.Split(string(data), "\n") + const prefix = "Max open files" + for _, s := range lines { if !strings.HasPrefix(s, prefix) { continue }