From 7866184b72ab9f56f608f98c1c79288bf759a583 Mon Sep 17 00:00:00 2001 From: Mohamed Elbahja Date: Sat, 15 Aug 2020 14:17:53 +0100 Subject: [PATCH] add: sync pool && Download method moved to Download --- chunk.go | 52 ++++++++++++++++------------------------------------ 1 file changed, 16 insertions(+), 36 deletions(-) diff --git a/chunk.go b/chunk.go index 4372372..22ded68 100644 --- a/chunk.go +++ b/chunk.go @@ -1,56 +1,36 @@ package got import ( - "fmt" - "io" - "net/http" - "os" + "sync" ) -// Chunk is a part of file. +// Chunk is a partial content range. type Chunk struct { - // Progress to report written bytes. - *Progress - // Chunk start pos. Start uint64 - // Chunk end. + // Chunk end pos. End uint64 // Path name where this chunk downloaded. Path string + // Done to check is this chunk downloaded. Done chan struct{} } -// Download a chunk, and report to Progress, it returns error if any! -func (c *Chunk) Download(URL string, client *http.Client, dest *os.File) (err error) { - - req, err := NewRequest("GET", URL) - - if err != nil { - return err - } - - contentRange := fmt.Sprintf("bytes=%d-%d", c.Start, c.End) - - if c.End == 0 { - contentRange = fmt.Sprintf("bytes=%d-", c.Start) - } - - req.Header.Set("Range", contentRange) - - res, err := client.Do(req) - - if err != nil { - return err - } - - defer res.Body.Close() - - _, err = io.Copy(dest, io.TeeReader(res.Body, c.Progress)) +// Reset resets the chunk item to the initial state. +func (c *Chunk) Reset() { + c.Start = 0 + c.End = 0 + c.Path = "" + c.Done = make(chan struct{}) +} - return err +// ChunkPool helps in multi *Download files. +var ChunkPool = &sync.Pool{ + New: func() interface{} { + return new(Chunk) + }, }