File tree Expand file tree Collapse file tree 2 files changed +18
-12
lines changed
vendor/go.bug.st/downloader Expand file tree Collapse file tree 2 files changed +18
-12
lines changed Original file line number Diff line number Diff line change @@ -11,19 +11,20 @@ import (
1111 "io"
1212 "net/http"
1313 "os"
14- "sync/atomic "
14+ "sync"
1515 "time"
1616)
1717
1818// Downloader is an asynchronous downloader
1919type Downloader struct {
20- URL string
21- Done chan bool
22- resp * http.Response
23- out * os.File
24- completed int64
25- size int64
26- err error
20+ URL string
21+ Done chan bool
22+ resp * http.Response
23+ out * os.File
24+ completed int64
25+ completedLock sync.Mutex
26+ size int64
27+ err error
2728}
2829
2930// DownloadOptions are optional flags that can be passed to Download function
@@ -79,7 +80,9 @@ func (d *Downloader) AsyncRun() {
7980 n , err := in .Read (buff [:])
8081 if n > 0 {
8182 d .out .Write (buff [:n ])
82- atomic .AddInt64 (& d .completed , int64 (n ))
83+ d .completedLock .Lock ()
84+ d .completed += int64 (n )
85+ d .completedLock .Unlock ()
8386 }
8487 if err == io .EOF {
8588 break
@@ -107,7 +110,10 @@ func (d *Downloader) Error() error {
107110
108111// Completed returns the bytes read so far
109112func (d * Downloader ) Completed () int64 {
110- return atomic .LoadInt64 (& d .completed )
113+ d .completedLock .Lock ()
114+ res := d .completed
115+ d .completedLock .Unlock ()
116+ return res
111117}
112118
113119// Download returns an asynchronous downloader that will donwload the specified url
You can’t perform that action at this time.
0 commit comments