Skip to content

Commit

Permalink
fix: http download resume task
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie committed Aug 12, 2022
1 parent 8100baf commit 3fa3f28
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ bin/

.torrent.db
.torrent.db-shm
.torrent.db-wal
.torrent.db-wal

.data
31 changes: 14 additions & 17 deletions internal/protocol/http/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ func (re *RequestError) Error() string {
type Fetcher struct {
*fetcher.DefaultFetcher

res *base.Resource
opts *base.Options
status base.Status
chunks []*Chunk
clients []*http.Response
res *base.Resource
opts *base.Options
status base.Status
chunks []*Chunk

ctx context.Context
cancel context.CancelFunc
Expand Down Expand Up @@ -133,7 +132,6 @@ func (f *Fetcher) Start() (err error) {
// 每个连接平均需要下载的分块大小
chunkSize := f.res.Length / int64(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++ {
var (
begin = chunkSize * int64(i)
Expand All @@ -151,7 +149,6 @@ func (f *Fetcher) Start() (err error) {
} else {
// 只支持单连接下载
f.chunks = make([]*Chunk, 1)
f.clients = make([]*http.Response, 1)
f.chunks[0] = NewChunk(0, 0)
}
f.fetch()
Expand Down Expand Up @@ -267,7 +264,6 @@ func (f *Fetcher) fetchChunk(index int) (err error) {
if err != nil {
return err
}
f.clients[index] = resp
if resp.StatusCode != base.HttpCodeOK && resp.StatusCode != base.HttpCodePartialContent {
err = NewRequestError(resp.StatusCode, resp.Status)
return err
Expand Down Expand Up @@ -306,17 +302,18 @@ func (f *Fetcher) fetchChunk(index int) (err error) {
}
}

if f.status == base.DownloadStatusPause {
if err != nil {
chunk.Status = base.DownloadStatusError
return
}

isComplete := f.res.Range && chunk.Downloaded >= chunk.End-chunk.Begin+1
if f.status == base.DownloadStatusPause && !isComplete {
chunk.Status = base.DownloadStatusPause
} else if chunk.Downloaded >= chunk.End-chunk.Begin+1 {
chunk.Status = base.DownloadStatusDone
} else {
if err != nil {
chunk.Status = base.DownloadStatusError
} else {
chunk.Status = base.DownloadStatusDone
}
return
}

chunk.Status = base.DownloadStatusDone
return
}

Expand Down
6 changes: 3 additions & 3 deletions internal/protocol/http/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestFetcher_DownloadChunked(t *testing.T) {
defer listener.Close()
// chunked编码下载
downloadNormal(listener, 1, t)
downloadContinue(listener, 1, t)
//downloadContinue(listener, 1, t)
}

func TestFetcher_DownloadRetry(t *testing.T) {
Expand Down Expand Up @@ -184,9 +184,9 @@ func downloadResume(listener net.Listener, connections int, t *testing.T) {
}

fb := new(FetcherBuilder)
time.Sleep(time.Second * 2)
time.Sleep(time.Millisecond * 200)
data := fb.Store(fetcher)
time.Sleep(time.Second * 1)
time.Sleep(time.Millisecond * 200)
fetcher.Pause()

fetcher = fb.Resume(res, opts, data)
Expand Down

0 comments on commit 3fa3f28

Please sign in to comment.