Skip to content

Commit 56c4f01

Browse files
committed
Updated go.bug.st/downloader
Fix #87
1 parent e3b0665 commit 56c4f01

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

Diff for: Gopkg.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: vendor/go.bug.st/downloader/downloader.go

+16-10
Original file line numberDiff line numberDiff 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
1919
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
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
109112
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
111117
}
112118

113119
// Download returns an asynchronous downloader that will donwload the specified url

0 commit comments

Comments
 (0)