Skip to content

Commit

Permalink
remove errors and logger, close the results chan when done
Browse files Browse the repository at this point in the history
  • Loading branch information
kmulvey committed Sep 12, 2022
1 parent 7d1bc53 commit d5b18b8
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions internal/app/imagedup/hash/differ.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@ import (
"github.com/kmulvey/goutils"
"github.com/kmulvey/imagedup/pkg/types"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
)

// Differ
type Differ struct {
diffTime prometheus.Gauge
comparisonsCompleted prometheus.Gauge
inputImages chan types.Pair
errors chan error
cache *Cache
deleteLogger *logrus.Logger
numWorkers int
distanceThreshold int
}
Expand All @@ -32,17 +29,15 @@ type DiffResult struct {
TwoArea int
}

func NewDiffer(numWorkers, distanceThreshold int, inputImages chan types.Pair, cache *Cache, deleteLogger *logrus.Logger, promNamespace string) *Differ {
func NewDiffer(numWorkers, distanceThreshold int, inputImages chan types.Pair, cache *Cache, promNamespace string) *Differ {

if numWorkers <= 0 || numWorkers > runtime.GOMAXPROCS(0)-1 {
numWorkers = 1
}

var dp = &Differ{
inputImages: inputImages,
errors: make(chan error),
cache: cache,
deleteLogger: deleteLogger,
distanceThreshold: distanceThreshold,
numWorkers: numWorkers,
diffTime: prometheus.NewGauge(
Expand All @@ -63,9 +58,10 @@ func NewDiffer(numWorkers, distanceThreshold int, inputImages chan types.Pair, c
return dp
}

func (dp *Differ) Run(ctx context.Context) chan error {
func (dp *Differ) Run(ctx context.Context) (chan DiffResult, chan error) {
var errorChans = make([]chan error, dp.numWorkers)
var resultChans = make([]chan DiffResult, dp.numWorkers)

for i := 0; i < dp.numWorkers; i++ {
var errors = make(chan error)
var results = make(chan DiffResult)
Expand All @@ -74,8 +70,7 @@ func (dp *Differ) Run(ctx context.Context) chan error {
go dp.diffWorker(ctx, results, errors)
}

dp.errors = goutils.MergeChannels(errorChans...)
return dp.errors
return goutils.MergeChannels(resultChans...), goutils.MergeChannels(errorChans...)
}

func (dp *Differ) diffWorker(ctx context.Context, results chan DiffResult, errors chan error) {
Expand All @@ -96,6 +91,7 @@ func (dp *Differ) diffWorker(ctx context.Context, results chan DiffResult, error
p, open := <-dp.inputImages
if !open {
close(errors)
close(results)
return
}
start = time.Now()
Expand All @@ -118,7 +114,7 @@ func (dp *Differ) diffWorker(ctx context.Context, results chan DiffResult, error
continue
}

if distance < dp.distanceThreshold {
if distance >= dp.distanceThreshold {
results <- DiffResult{One: p.One, OneArea: imgCacheOne.Config.Height * imgCacheOne.Config.Width, Two: p.Two, TwoArea: imgCacheTwo.Config.Height * imgCacheTwo.Config.Width}
}

Expand Down

0 comments on commit d5b18b8

Please sign in to comment.