Преглед на файлове

Merge pull request #14488 from HuKeping/fixstats

Fix connection block when using docker stats API
Brian Goff преди 9 години
родител
ревизия
5f5d73d515
променени са 2 файла, в които са добавени 19 реда и са изтрити 3 реда
  1. 9 3
      api/client/stats.go
  2. 10 0
      api/server/container.go

+ 9 - 3
api/client/stats.go

@@ -61,10 +61,16 @@ func (s *containerStats) Collect(cli *DockerCli, streamStats bool) {
 				u <- err
 				return
 			}
-			var (
+
+			var memPercent = 0.0
+			var cpuPercent = 0.0
+
+			// MemoryStats.Limit will never be 0 unless the container is not running and we havn't
+			// got any data from cgroup
+			if v.MemoryStats.Limit != 0 {
 				memPercent = float64(v.MemoryStats.Usage) / float64(v.MemoryStats.Limit) * 100.0
-				cpuPercent = 0.0
-			)
+			}
+
 			previousCPU = v.PreCPUStats.CPUUsage.TotalUsage
 			previousSystem = v.PreCPUStats.SystemUsage
 			cpuPercent = calculateCPUPercent(previousCPU, previousSystem, v)

+ 10 - 0
api/server/container.go

@@ -74,6 +74,16 @@ func (s *Server) getContainersStats(version version.Version, w http.ResponseWrit
 	}
 
 	stream := boolValueOrDefault(r, "stream", true)
+
+	// If the container is not running and requires no stream, return an empty stats.
+	container, err := s.daemon.Get(vars["name"])
+	if err != nil {
+		return err
+	}
+	if !container.IsRunning() && !stream {
+		return writeJSON(w, http.StatusOK, &types.Stats{})
+	}
+
 	var out io.Writer
 	if !stream {
 		w.Header().Set("Content-Type", "application/json")