Skip to content

Commit

Permalink
Limit batch size of files
Browse files Browse the repository at this point in the history
  • Loading branch information
vodkabears committed Jun 18, 2017
1 parent b73615d commit 66cf955
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions reader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ import (
)

const maxBytes = 10000
const batchSize = 256

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

return b
}

// Chunk is a chunk of text from file
type Chunk struct {
Expand Down Expand Up @@ -42,17 +51,22 @@ func readFile(file string, ch chan Chunk, wg *sync.WaitGroup) {
wg.Done()
}

// Read reads a batch of files chunk by chunk and passes content to the channel
func Read(ch chan Chunk, files []string) {
func readBatch(ch chan Chunk, files []string) {
var wg sync.WaitGroup

wg.Add(len(files))
for _, file := range files {
go readFile(file, ch, &wg)
}

go func() {
wg.Wait()
close(ch)
}()
wg.Wait()
}

// Read reads a batch of files chunk by chunk and passes content to the channel
func Read(ch chan Chunk, files []string) {
for i, l := 0, len(files); i < l; i += batchSize {
readBatch(ch, files[i:min(l, i+batchSize)])
}

close(ch)
}

0 comments on commit 66cf955

Please sign in to comment.