@@ -15,20 +15,47 @@ type progressReporter struct {
1515 artifact types.BlobInfo // The blob metadata which is currently being progressed
1616 lastUpdate time.Time // The last time a progress channel event was sent
1717 offset uint64 // The currently downloaded size in bytes
18- offsetUpdate uint64 // The number of bytes downloaded within the last update interval
18+ offsetUpdate uint64 // The number of bytes downloaded since lastUpdate
1919}
2020
21- // reportNewArtifact fires types.ProgressEventNewArtifact to its progress channel.
22- func (r * progressReporter ) reportNewArtifact () {
23- r .channel <- types.ProgressProperties {
21+ // newProgressReporter creates a new progress reporter
22+ // and immediately reports a new artifact event.
23+ func newProgressReporter (
24+ channel chan <- types.ProgressProperties ,
25+ interval time.Duration ,
26+ artifact types.BlobInfo ,
27+ ) * progressReporter {
28+ channel <- types.ProgressProperties {
2429 Event : types .ProgressEventNewArtifact ,
25- Artifact : r .artifact ,
30+ Artifact : artifact ,
31+ }
32+ return & progressReporter {
33+ channel : channel ,
34+ interval : interval ,
35+ artifact : artifact ,
36+ lastUpdate : time .Now (),
37+ }
38+ }
39+
40+ // reset resets the reporters progress
41+ // and reports its zeroed state.
42+ // It's meant to be used on error when
43+ // the processing has to be re-started
44+ // (e.g. ErrFallbackToOrdinaryLayerDownload).
45+ func (r * progressReporter ) reset () {
46+ r .offset = 0
47+ r .offsetUpdate = 0
48+
49+ r .channel <- types.ProgressProperties {
50+ Event : types .ProgressEventRead ,
51+ Artifact : r .artifact ,
52+ Offset : r .offset ,
53+ OffsetUpdate : r .offsetUpdate ,
2654 }
2755 r .lastUpdate = time .Now ()
2856}
2957
30- // reportRead fires the types.ProgressEventRead event with `bytesRead`
31- // to its progress channel.
58+ // reportRead reports progress with the number of `bytesRead`.
3259func (r * progressReporter ) reportRead (bytesRead uint64 ) {
3360 r .offset += bytesRead
3461 r .offsetUpdate += bytesRead
@@ -44,7 +71,7 @@ func (r *progressReporter) reportRead(bytesRead uint64) {
4471 }
4572}
4673
47- // reportDone fires the ProgressEventDone to its progress channel .
74+ // reportDone reports completion .
4875func (r * progressReporter ) reportDone () {
4976 r .channel <- types.ProgressProperties {
5077 Event : types .ProgressEventDone ,
@@ -54,34 +81,23 @@ func (r *progressReporter) reportDone() {
5481 }
5582}
5683
57- // progressReader is an io.Reader that reports its progress to
58- // an underlying *progressReporter .
84+ // progressReader extends a wrapped io.Reader
85+ // with additional reporting of its progress .
5986type progressReader struct {
6087 source io.Reader
6188 * progressReporter
6289}
6390
64- // newProgressReader creates a new progress reader for
65- // `source`: The source when internally reading bytes
66- // `channel`: The reporter channel to which the progress will be sent
67- // `interval`: The update interval to indicate how often the progress should update
68- // `artifact`: The blob metadata which is currently being progressed.
91+ // newProgressReader creates a new progress reader that wraps source
92+ // and reports progress through the given reporter.
6993func newProgressReader (
7094 source io.Reader ,
71- channel chan <- types.ProgressProperties ,
72- interval time.Duration ,
73- artifact types.BlobInfo ,
95+ reporter * progressReporter ,
7496) * progressReader {
75- r := & progressReader {
76- source : source ,
77- progressReporter : & progressReporter {
78- channel : channel ,
79- interval : interval ,
80- artifact : artifact ,
81- },
97+ return & progressReader {
98+ source : source ,
99+ progressReporter : reporter ,
82100 }
83- r .reportNewArtifact ()
84- return r
85101}
86102
87103// Read continuously reads bytes into the progress reader and reports the
0 commit comments