Skip to content

Commit

Permalink
Fix block?
Browse files Browse the repository at this point in the history
  • Loading branch information
katie31 committed Aug 10, 2017
1 parent 195459c commit 8138f9f
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ import (
"strconv"
)

func min(a, b int) int {
if a < b {
return a
}
return b
}

// EmptyWriteIgnorer handles 0 byte write in LZ4 package
// to stop pipe reader/writer from blocking.
type EmptyWriteIgnorer struct {
Expand Down Expand Up @@ -98,7 +105,6 @@ func ExtractAll(ti TarInterpreter, files []ReaderMaker) error {
}
}()



// Set maximum number of goroutines spun off by ExtractAll
var con int
Expand All @@ -107,25 +113,23 @@ func ExtractAll(ti TarInterpreter, files []ReaderMaker) error {
if ok {
con, _ = strconv.Atoi(conc)
} else {
con = 10
con = min(10, len(files))
}

concurrent := make(chan Empty, con)
for i := 0; i < con; i++ {
concurrent <- Empty{}
}
done := make(chan bool)

go func() {
for i := 0; i < len(files); i++ {
<- done
concurrent <- Empty{}
}
}()

for i, val := range files {
<- concurrent
go func(i int, val ReaderMaker) {
defer func () {
concurrent <- Empty{}
sem <- Empty{}
}()

pr, tempW := io.Pipe()
pw := &EmptyWriteIgnorer{tempW}

Expand Down Expand Up @@ -159,10 +163,7 @@ func ExtractAll(ti TarInterpreter, files []ReaderMaker) error {
collectAll <- err
}
}

done <- true
concurrent <- Empty{}
sem <- Empty{}

}(i, val)
}

Expand Down

0 comments on commit 8138f9f

Please sign in to comment.