Skip to content

Commit

Permalink
refactor: use file process pool for executiong file processing tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
ybirader committed Aug 13, 2023
1 parent 347d7e0 commit 5ae63db
Showing 1 changed file with 46 additions and 7 deletions.
53 changes: 46 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Archiver struct {
filesToProcess chan File
filesToWrite chan File
numberOfWorkers int
fileProcessPool *FileProcessPool
}

type File struct {
Expand All @@ -25,11 +26,23 @@ type File struct {
}

func NewArchiver(archive *os.File) *Archiver {
return &Archiver{Dest: archive, w: zip.NewWriter(archive), numberOfWorkers: runtime.GOMAXPROCS(0)}
a := &Archiver{Dest: archive,
w: zip.NewWriter(archive),
numberOfWorkers: runtime.GOMAXPROCS(0),
}

executor := func(file File) {
a.filesToWrite <- file
}

fileProcessPool, _ := NewFileProcessPool(a.numberOfWorkers, executor)
a.fileProcessPool = fileProcessPool

return a
}

func (a *Archiver) ArchiveDir(root string) error {
err := a.walkDir(root)
err := a.newWalkDir(root)

if err != nil {
return err
Expand Down Expand Up @@ -88,12 +101,38 @@ func (f *FileProcessPool) Enqueue(file File) {
f.tasks <- file
}

// Process files i.e.
func (a *Archiver) newWalkDir(root string) error {
a.filesToWrite = make(chan File)
a.fileProcessPool.Start()

wg := new(sync.WaitGroup)
wg.Add(1)
go a.writeFiles(wg)

err := filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}

// channel to put them on i.e. filesToProcess
// number of workers that listen for tasks
// enqueue tasks
// close pool i.e. exit
if path == root {
return nil
}

f := File{Path: path, Info: info}
a.fileProcessPool.Enqueue(f)
return nil
})

if err != nil {
return err
}

a.fileProcessPool.Close()
close(a.filesToWrite)
wg.Wait()

return nil
}

func (a *Archiver) walkDir(root string) error {
a.initializeChannels()
Expand Down

0 comments on commit 5ae63db

Please sign in to comment.