@@ -7,48 +7,45 @@ import (
77 "go.podman.io/image/v5/types"
88)
99
10- // progressReader is a reader that reports its progress to a types.ProgressProperties channel on an interval.
11- type progressReader struct {
12- source io. Reader
13- channel chan <- types.ProgressProperties
14- interval time.Duration
15- artifact types.BlobInfo
16- lastUpdate time.Time
17- offset uint64
18- offsetUpdate uint64
10+ // progressReporter facilitates progress reporting through its
11+ // underlying types.ProgressProperties channel on an interval.
12+ type progressReporter struct {
13+ channel chan <- types.ProgressProperties // The reporter channel to which the progress will be sent
14+ interval time.Duration // The update interval to indicate how often the progress should update
15+ artifact types.BlobInfo // The blob metadata which is currently being progressed
16+ lastUpdate time.Time // The last time a progress channel event was sent
17+ offset uint64 // The currently downloaded size in bytes
18+ offsetUpdate uint64 // The number of bytes downloaded within the last update interval
1919}
2020
21- // newProgressReader creates a new progress reader for:
22- // `source`: The source when internally reading bytes
23- // `channel`: The reporter channel to which the progress will be sent
24- // `interval`: The update interval to indicate how often the progress should update
25- // `artifact`: The blob metadata which is currently being progressed
26- func newProgressReader (
27- source io.Reader ,
28- channel chan <- types.ProgressProperties ,
29- interval time.Duration ,
30- artifact types.BlobInfo ,
31- ) * progressReader {
32- // The progress reader constructor informs the progress channel
33- // that a new artifact will be read
34- channel <- types.ProgressProperties {
21+ // reportNewArtifact fires types.ProgressEventNewArtifact to its progress channel.
22+ func (r * progressReporter ) reportNewArtifact () {
23+ r .channel <- types.ProgressProperties {
3524 Event : types .ProgressEventNewArtifact ,
36- Artifact : artifact ,
25+ Artifact : r . artifact ,
3726 }
38- return & progressReader {
39- source : source ,
40- channel : channel ,
41- interval : interval ,
42- artifact : artifact ,
43- lastUpdate : time .Now (),
44- offset : 0 ,
45- offsetUpdate : 0 ,
27+ r .lastUpdate = time .Now ()
28+ }
29+
30+ // reportRead fires the types.ProgressEventRead event with `bytesRead`
31+ // to its progress channel.
32+ func (r * progressReporter ) reportRead (bytesRead uint64 ) {
33+ r .offset += bytesRead
34+ r .offsetUpdate += bytesRead
35+ if time .Since (r .lastUpdate ) > r .interval {
36+ r .channel <- types.ProgressProperties {
37+ Event : types .ProgressEventRead ,
38+ Artifact : r .artifact ,
39+ Offset : r .offset ,
40+ OffsetUpdate : r .offsetUpdate ,
41+ }
42+ r .lastUpdate = time .Now ()
43+ r .offsetUpdate = 0
4644 }
4745}
4846
49- // reportDone indicates to the internal channel that the progress has been
50- // finished
51- func (r * progressReader ) reportDone () {
47+ // reportDone fires the ProgressEventDone to its progress channel.
48+ func (r * progressReporter ) reportDone () {
5249 r .channel <- types.ProgressProperties {
5350 Event : types .ProgressEventDone ,
5451 Artifact : r .artifact ,
@@ -57,23 +54,40 @@ func (r *progressReader) reportDone() {
5754 }
5855}
5956
57+ // progressReader is an io.Reader that reports its progress to
58+ // an underlying *progressReporter.
59+ type progressReader struct {
60+ source io.Reader
61+ * progressReporter
62+ }
63+
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.
69+ func newProgressReader (
70+ source io.Reader ,
71+ channel chan <- types.ProgressProperties ,
72+ interval time.Duration ,
73+ artifact types.BlobInfo ,
74+ ) * progressReader {
75+ r := & progressReader {
76+ source : source ,
77+ progressReporter : & progressReporter {
78+ channel : channel ,
79+ interval : interval ,
80+ artifact : artifact ,
81+ },
82+ }
83+ r .reportNewArtifact ()
84+ return r
85+ }
86+
6087// Read continuously reads bytes into the progress reader and reports the
61- // status via the internal channel
88+ // status via the internal channel.
6289func (r * progressReader ) Read (p []byte ) (int , error ) {
6390 n , err := r .source .Read (p )
64- r .offset += uint64 (n )
65- r .offsetUpdate += uint64 (n )
66-
67- // Fire the progress reader in the provided interval
68- if time .Since (r .lastUpdate ) > r .interval {
69- r .channel <- types.ProgressProperties {
70- Event : types .ProgressEventRead ,
71- Artifact : r .artifact ,
72- Offset : r .offset ,
73- OffsetUpdate : r .offsetUpdate ,
74- }
75- r .lastUpdate = time .Now ()
76- r .offsetUpdate = 0
77- }
91+ r .reportRead (uint64 (n ))
7892 return n , err
7993}
0 commit comments