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

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:
 				}
 			}
-
 		} else {
 			t.broadcastDone = true
 		}
@@ -159,18 +158,23 @@ func (t *transfer) Watch(progressOutput progress.Output) *Watcher {
 		defer func() {
 			close(w.running)
 		}()
-		done := false
+		var (
+			done           bool
+			lastWritten    progress.Progress
+			hasLastWritten bool
+		)
 		for {
 			t.mu.Lock()
 			hasLastProgress := t.hasLastProgress
 			lastProgress := t.lastProgress
 			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)
+				lastWritten = lastProgress
+				hasLastWritten = true
 			}
 
 			if done {

+ 0 - 9
distribution/xfer/transfer_test.go

@@ -41,15 +41,6 @@ func TestTransfer(t *testing.T) {
 				if p.Current != 0 {
 					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 {
 				t.Fatalf("got unexpected progress value: %d (expected %d)", p.Current, val+1)
 			}