3826: Roll up Open() attempts into a slice and a loop.
[arvados.git] / services / crunchstat / crunchstat.go
index 6b355707a2b0295c17648509d135863077e5e655..855e115507b0e402ff27fe938e2fd5826e5f3127 100644 (file)
@@ -78,24 +78,21 @@ var reportedStatFile = map[string]string{}
 // cgroup root for the given statgroup. (This will avoid falling back
 // to host-level stats during container setup and teardown.)
 func OpenStatFile(stderr chan<- string, cgroup Cgroup, statgroup string, stat string) (*os.File, error) {
+       var paths = []string{}
+       paths = append(paths, fmt.Sprintf("%s/%s/%s/%s/%s", cgroup.root, statgroup, cgroup.parent, cgroup.cid, stat))
+       paths = append(paths, fmt.Sprintf("%s/%s/%s/%s", cgroup.root, cgroup.parent, cgroup.cid, stat))
+       paths = append(paths, fmt.Sprintf("%s/%s/%s", cgroup.root, statgroup, stat))
+       paths = append(paths, fmt.Sprintf("%s/%s", cgroup.root, stat))
        var path string
-       path = fmt.Sprintf("%s/%s/%s/%s/%s", cgroup.root, statgroup, cgroup.parent, cgroup.cid, stat)
-       file, err := os.Open(path)
-       if err != nil {
-               path = fmt.Sprintf("%s/%s/%s/%s", cgroup.root, cgroup.parent, cgroup.cid, stat)
-               file, err = os.Open(path)
-       }
-       if err != nil {
-               path = fmt.Sprintf("%s/%s/%s", cgroup.root, statgroup, stat)
+       var file *os.File
+       var err error
+       for _, path = range paths {
                file, err = os.Open(path)
-       }
-       if err != nil {
-               path = fmt.Sprintf("%s/%s", cgroup.root, stat)
-               file, err = os.Open(path)
-       }
-       if err != nil {
-               file = nil
-               path = ""
+               if err == nil {
+                       break
+               } else {
+                       path = ""
+               }
        }
        if pathWas, ok := reportedStatFile[stat]; !ok || pathWas != path {
                // Log whenever we start using a new/different cgroup