Skip to content

Commit

Permalink
refactor: split functions in filewalker.go
Browse files Browse the repository at this point in the history
This refactoring will increase readability and
maintainability of the code. There are no
functional changes.
  • Loading branch information
trym-b committed Apr 8, 2024
1 parent df554c4 commit 0f8e0d8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 44 deletions.
41 changes: 2 additions & 39 deletions internal/filewalker/filewalker.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,46 +247,9 @@ func (walker *fileWalker) Walk(ctx context.Context, stats Stats) error {

allResults := &sync.WaitGroup{}
allResults.Add(1)
defer func() {
pool.CloseWait()
resultChan <- nil
allResults.Wait()
timeSpent := time.Since(startTime)
if walker.isLog(summaryLogType) {
walker.logSummary(fmt.Sprintf("Total time: %v, %s", timeSpent, stats))
} else if walker.isLog(progressLogType) {
fmt.Printf(" \r")
}
}()
go func() {
count := 0
for {
result := <-resultChan
if result == nil {
allResults.Done()
break
}
count++
if result.ErrorCount() > 0 && walker.isLog(errorLogType) {
walker.logError(result, count)
} else if walker.isLog(infoLogType) {
walker.logInfo(result, count)
}

stats.Merge(result.GetStats())
if result.Fatal() != nil {
fmt.Printf("ERROR: %s\n", result.Fatal())
}
defer closePool(walker, pool, resultChan, allResults, startTime, stats)
go printResultsAndProgress(walker, resultChan, allResults, stats)

if walker.isLog(progressLogType) {
fmt.Printf(" %s %s\r", string(anim[animPos]), stats.String())
animPos++
if animPos >= len(anim) {
animPos = 0
}
}
}
}()
for _, path := range walker.paths {
if !walker.processedPaths.Contains(path) {
if err := walker.walkDir(ctx, path, path, submitJobFunction); err != nil {
Expand Down
51 changes: 51 additions & 0 deletions internal/filewalker/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package filewalker

import (
"fmt"
"sync"
"time"

workerPool "github.com/nlnwa/warchaeology/internal/workerpool"
)

func closePool(walker *fileWalker, pool *workerPool.WorkerPool, resultChan chan Result, allResults *sync.WaitGroup, startTime time.Time, stats Stats) {
pool.CloseWait()
resultChan <- nil
allResults.Wait()
timeSpent := time.Since(startTime)
if walker.isLog(summaryLogType) {
walker.logSummary(fmt.Sprintf("Total time: %v, %s", timeSpent, stats))
} else if walker.isLog(progressLogType) {
fmt.Printf(" \r")
}
}

func printResultsAndProgress(walker *fileWalker, resultChan chan Result, allResults *sync.WaitGroup, stats Stats) {
count := 0
for {
result := <-resultChan
if result == nil {
allResults.Done()
break
}
count++
if result.ErrorCount() > 0 && walker.isLog(errorLogType) {
walker.logError(result, count)
} else if walker.isLog(infoLogType) {
walker.logInfo(result, count)
}

stats.Merge(result.GetStats())
if result.Fatal() != nil {
fmt.Printf("ERROR: %s\n", result.Fatal())
}

if walker.isLog(progressLogType) {
fmt.Printf(" %s %s\r", string(anim[animPos]), stats.String())
animPos++
if animPos >= len(anim) {
animPos = 0
}
}
}
}
10 changes: 5 additions & 5 deletions internal/workerpool/workerpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"sync"
)

type workerPool struct {
type WorkerPool struct {
numberOfWorkers int
jobs chan func()
waitGroup sync.WaitGroup
}

func New(ctx context.Context, numberOfWorkers int) *workerPool {
func New(ctx context.Context, numberOfWorkers int) *WorkerPool {
magicNumber := 4
pool := &workerPool{
pool := &WorkerPool{
numberOfWorkers: numberOfWorkers,
jobs: make(chan func(), numberOfWorkers*magicNumber),
waitGroup: sync.WaitGroup{},
Expand All @@ -33,12 +33,12 @@ func New(ctx context.Context, numberOfWorkers int) *workerPool {
return pool
}

func (pool *workerPool) CloseWait() {
func (pool *WorkerPool) CloseWait() {
close(pool.jobs)
pool.waitGroup.Wait()
}

func (pool *workerPool) Submit(job func()) {
func (pool *WorkerPool) Submit(job func()) {
pool.waitGroup.Add(1)
pool.jobs <- job
}

0 comments on commit 0f8e0d8

Please sign in to comment.