Commit 7f6e96f4 authored by Cristian Maglie's avatar Cristian Maglie Committed by Cristian Maglie

Updated go.bug.st/downloader

Fix #87
parent a32d52fa
......@@ -397,11 +397,11 @@
[[projects]]
branch = "master"
digest = "1:5cd3c0f530ab660c7b08f563020dc00b8e761c2f7650457a3f669b6601f7f28d"
digest = "1:975e88bfd9ed90a0e939a9e67ecab0682a28c0b9ed021bd30f469a0b5b4e3739"
name = "go.bug.st/downloader"
packages = ["."]
pruneopts = "UT"
revision = "60d736642c5c2252f6d19016d7e68178b19478f0"
revision = "9b8976a44d87b5d86444bccc3ed875b2530f45e0"
[[projects]]
branch = "master"
......
......@@ -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
......@@ -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
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment