Compare commits

...
1 Commits
Author SHA1 Message Date
monkeyWie e292926697 fix: http download resume task (#31) 2022-08-12 13:06:58 +08:00
3 changed files with 21 additions and 22 deletions

No files matched your search

+3 -1
View File
@@ -19,4 +19,6 @@ bin/
.torrent.db .torrent.db
.torrent.db-shm .torrent.db-shm
.torrent.db-wal .torrent.db-wal
.data
+15 -18
View File
@@ -36,11 +36,10 @@ func (re *RequestError) Error() string {
type Fetcher struct { type Fetcher struct {
*fetcher.DefaultFetcher *fetcher.DefaultFetcher
res *base.Resource res *base.Resource
opts *base.Options opts *base.Options
status base.Status status base.Status
chunks []*Chunk chunks []*Chunk
clients []*http.Response
ctx context.Context ctx context.Context
cancel context.CancelFunc cancel context.CancelFunc
@@ -133,7 +132,6 @@ func (f *Fetcher) Start() (err error) {
// 每个连接平均需要下载的分块大小 // 每个连接平均需要下载的分块大小
chunkSize := f.res.Length / int64(f.opts.Connections) chunkSize := f.res.Length / int64(f.opts.Connections)
f.chunks = make([]*Chunk, f.opts.Connections) f.chunks = make([]*Chunk, f.opts.Connections)
f.clients = make([]*http.Response, f.opts.Connections)
for i := 0; i < f.opts.Connections; i++ { for i := 0; i < f.opts.Connections; i++ {
var ( var (
begin = chunkSize * int64(i) begin = chunkSize * int64(i)
@@ -151,7 +149,6 @@ func (f *Fetcher) Start() (err error) {
} else { } else {
// 只支持单连接下载 // 只支持单连接下载
f.chunks = make([]*Chunk, 1) f.chunks = make([]*Chunk, 1)
f.clients = make([]*http.Response, 1)
f.chunks[0] = NewChunk(0, 0) f.chunks[0] = NewChunk(0, 0)
} }
f.fetch() f.fetch()
@@ -267,7 +264,6 @@ func (f *Fetcher) fetchChunk(index int) (err error) {
if err != nil { if err != nil {
return err return err
} }
f.clients[index] = resp
if resp.StatusCode != base.HttpCodeOK && resp.StatusCode != base.HttpCodePartialContent { if resp.StatusCode != base.HttpCodeOK && resp.StatusCode != base.HttpCodePartialContent {
err = NewRequestError(resp.StatusCode, resp.Status) err = NewRequestError(resp.StatusCode, resp.Status)
return err return err
@@ -306,17 +302,18 @@ func (f *Fetcher) fetchChunk(index int) (err error) {
} }
} }
if f.status == base.DownloadStatusPause { if err != nil {
chunk.Status = base.DownloadStatusPause chunk.Status = base.DownloadStatusError
} else if chunk.Downloaded >= chunk.End-chunk.Begin+1 { return
chunk.Status = base.DownloadStatusDone
} else {
if err != nil {
chunk.Status = base.DownloadStatusError
} else {
chunk.Status = base.DownloadStatusDone
}
} }
isComplete := f.res.Range && chunk.Downloaded >= chunk.End-chunk.Begin+1
if f.status == base.DownloadStatusPause && !isComplete {
chunk.Status = base.DownloadStatusPause
return
}
chunk.Status = base.DownloadStatusDone
return return
} }
+3 -3
View File
@@ -75,7 +75,7 @@ func TestFetcher_DownloadChunked(t *testing.T) {
defer listener.Close() defer listener.Close()
// chunked编码下载 // chunked编码下载
downloadNormal(listener, 1, t) downloadNormal(listener, 1, t)
downloadContinue(listener, 1, t) //downloadContinue(listener, 1, t)
} }
func TestFetcher_DownloadRetry(t *testing.T) { func TestFetcher_DownloadRetry(t *testing.T) {
@@ -184,9 +184,9 @@ func downloadResume(listener net.Listener, connections int, t *testing.T) {
} }
fb := new(FetcherBuilder) fb := new(FetcherBuilder)
time.Sleep(time.Second * 2) time.Sleep(time.Millisecond * 200)
data := fb.Store(fetcher) data := fb.Store(fetcher)
time.Sleep(time.Second * 1) time.Sleep(time.Millisecond * 200)
fetcher.Pause() fetcher.Pause()
fetcher = fb.Resume(res, opts, data) fetcher = fb.Resume(res, opts, data)