浏览代码

Avoid outputting last progress item twice

A watcher would output the current progress item when it was detached,
in case it missed that item earlier, which would leave the user seeing
some intermediate step of the operation. This commit changes it to only
output it on detach if it didn't already output the same item.

Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
Aaron Lehmann 9 年之前
父节点
当前提交
fde2329eaa
共有 2 个文件被更改,包括 10 次插入15 次删除
  1. 10 6
      distribution/xfer/transfer.go
  2. 0 9
      distribution/xfer/transfer_test.go

+ 10 - 6
distribution/xfer/transfer.go

@@ -124,7 +124,6 @@ func (t *transfer) Broadcast(masterProgressChan <-chan progress.Progress) {
 				default:
 				default:
 				}
 				}
 			}
 			}
-
 		} else {
 		} else {
 			t.broadcastDone = true
 			t.broadcastDone = true
 		}
 		}
@@ -159,18 +158,23 @@ func (t *transfer) Watch(progressOutput progress.Output) *Watcher {
 		defer func() {
 		defer func() {
 			close(w.running)
 			close(w.running)
 		}()
 		}()
-		done := false
+		var (
+			done           bool
+			lastWritten    progress.Progress
+			hasLastWritten bool
+		)
 		for {
 		for {
 			t.mu.Lock()
 			t.mu.Lock()
 			hasLastProgress := t.hasLastProgress
 			hasLastProgress := t.hasLastProgress
 			lastProgress := t.lastProgress
 			lastProgress := t.lastProgress
 			t.mu.Unlock()
 			t.mu.Unlock()
 
 
-			// This might write the last progress item a
-			// second time (since channel closure also gets
-			// us here), but that's fine.
-			if hasLastProgress {
+			// Make sure we don't write the last progress item
+			// twice.
+			if hasLastProgress && (!done || !hasLastWritten || lastProgress != lastWritten) {
 				progressOutput.WriteProgress(lastProgress)
 				progressOutput.WriteProgress(lastProgress)
+				lastWritten = lastProgress
+				hasLastWritten = true
 			}
 			}
 
 
 			if done {
 			if done {

+ 0 - 9
distribution/xfer/transfer_test.go

@@ -41,15 +41,6 @@ func TestTransfer(t *testing.T) {
 				if p.Current != 0 {
 				if p.Current != 0 {
 					t.Fatalf("got unexpected progress value: %d (expected 0)", p.Current)
 					t.Fatalf("got unexpected progress value: %d (expected 0)", p.Current)
 				}
 				}
-			} else if p.Current == 10 {
-				// Special case: last progress output may be
-				// repeated because the transfer finishing
-				// causes the latest progress output to be
-				// written to the channel (in case the watcher
-				// missed it).
-				if p.Current != 9 && p.Current != 10 {
-					t.Fatalf("got unexpected progress value: %d (expected %d)", p.Current, val+1)
-				}
 			} else if p.Current != val+1 {
 			} else if p.Current != val+1 {
 				t.Fatalf("got unexpected progress value: %d (expected %d)", p.Current, val+1)
 				t.Fatalf("got unexpected progress value: %d (expected %d)", p.Current, val+1)
 			}
 			}