Skip to content

Commit

Permalink
Improve default concurrency calculation and reset chunk pool item.
Browse files Browse the repository at this point in the history
  • Loading branch information
melbahja committed Aug 16, 2020
1 parent 12a13de commit 97dd67a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
8 changes: 0 additions & 8 deletions chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ type Chunk struct {
Done chan struct{}
}

// 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{})
}

// ChunkPool helps in multi *Download files.
var ChunkPool = &sync.Pool{
New: func() interface{} {
Expand Down
18 changes: 12 additions & 6 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,19 @@ func (d *Download) Init() (err error) {
return nil
}

// Set concurrency default to Num CPU * 2.
// Set concurrency default.
if d.Concurrency == 0 {

d.Concurrency = uint(runtime.NumCPU() * 2)
d.Concurrency = uint(runtime.NumCPU() * 3)

// Set default max concurrency to 8
if d.Concurrency > 8 {
d.Concurrency = 8
// Set default max concurrency to 20.
if d.Concurrency > 20 {
d.Concurrency = 20
}

// Set default min concurrency to 2.
if d.Concurrency <= 2 {
d.Concurrency = 4
}
}

Expand Down Expand Up @@ -164,9 +169,10 @@ func (d *Download) Init() (err error) {
}

chunk := ChunkPool.Get().(*Chunk)
chunk.Reset()
chunk.Path = ""
chunk.Start = startRange
chunk.End = endRange
chunk.Done = make(chan struct{})

d.chunks = append(d.chunks, *chunk)

Expand Down

0 comments on commit 97dd67a

Please sign in to comment.