Skip to content

Fix uint64 un-aligned atomic access #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 16 additions & 10 deletions vendor/go.bug.st/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ import (
"io"
"net/http"
"os"
"sync/atomic"
"sync"
"time"
)

// Downloader is an asynchronous downloader
type Downloader struct {
URL string
Done chan bool
resp *http.Response
out *os.File
completed int64
size int64
err error
URL string
Done chan bool
resp *http.Response
out *os.File
completed int64
completedLock sync.Mutex
size int64
err error
}

// DownloadOptions are optional flags that can be passed to Download function
Expand Down Expand Up @@ -79,7 +80,9 @@ func (d *Downloader) AsyncRun() {
n, err := in.Read(buff[:])
if n > 0 {
d.out.Write(buff[:n])
atomic.AddInt64(&d.completed, int64(n))
d.completedLock.Lock()
d.completed += int64(n)
d.completedLock.Unlock()
}
if err == io.EOF {
break
Expand Down Expand Up @@ -107,7 +110,10 @@ func (d *Downloader) Error() error {

// Completed returns the bytes read so far
func (d *Downloader) Completed() int64 {
return atomic.LoadInt64(&d.completed)
d.completedLock.Lock()
res := d.completed
d.completedLock.Unlock()
return res
}

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