File tree 2 files changed +18
-12
lines changed
vendor/go.bug.st/downloader
2 files changed +18
-12
lines changed Original file line number Diff line number Diff line change @@ -11,19 +11,20 @@ import (
11
11
"io"
12
12
"net/http"
13
13
"os"
14
- "sync/atomic "
14
+ "sync"
15
15
"time"
16
16
)
17
17
18
18
// Downloader is an asynchronous downloader
19
19
type 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
27
28
}
28
29
29
30
// DownloadOptions are optional flags that can be passed to Download function
@@ -79,7 +80,9 @@ func (d *Downloader) AsyncRun() {
79
80
n , err := in .Read (buff [:])
80
81
if n > 0 {
81
82
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 ()
83
86
}
84
87
if err == io .EOF {
85
88
break
@@ -107,7 +110,10 @@ func (d *Downloader) Error() error {
107
110
108
111
// Completed returns the bytes read so far
109
112
func (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
111
117
}
112
118
113
119
// Download returns an asynchronous downloader that will donwload the specified url
You can’t perform that action at this time.
0 commit comments