Skip to content

Commit

Permalink
Merge pull request #331 from hearchco/as/fix/algo
Browse files Browse the repository at this point in the history
fix(search): forgot a case when no workers return for a origin
  • Loading branch information
aleksasiriski authored Jun 17, 2024
2 parents 1161711 + 736ec19 commit 128cebc
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/config/defaults_cat_general.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ func generalRanking() CategoryRanking {
}

var generalTimings = CategoryTimings{
PreferredTimeout: 700 * time.Millisecond,
HardTimeout: 3 * time.Second,
PreferredTimeout: 500 * time.Millisecond,
HardTimeout: 1500 * time.Millisecond,
}
4 changes: 2 additions & 2 deletions src/config/defaults_cat_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ func imagesRanking() CategoryRanking {
}

var imagesTimings = CategoryTimings{
PreferredTimeout: 700 * time.Millisecond,
HardTimeout: 3 * time.Second,
PreferredTimeout: 500 * time.Millisecond,
HardTimeout: 1500 * time.Millisecond,
}
36 changes: 36 additions & 0 deletions src/search/run_origins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package search

import (
"sync"
)

// Waits on either c.Wait() or wg.Wait() to do final.Done().
func waitForSuccessOrFinish(c *sync.Cond, wg *sync.WaitGroup, final *sync.WaitGroup) {
defer final.Done()
d := sync.Cond{L: &sync.Mutex{}}

// Wait for signal from any successful worker.
go func() {
c.L.Lock()
c.Wait()
c.L.Unlock()

d.L.Lock()
d.Signal()
d.L.Unlock()
}()

// Wait for all workers to finish (even if it's unsuccessful).
go func() {
wg.Wait()

d.L.Lock()
d.Signal()
d.L.Unlock()
}()

// Whichever of the above finishes first, signal the final wait group.
d.L.Lock()
d.Wait()
d.L.Unlock()
}
22 changes: 12 additions & 10 deletions src/search/run_preferred_origins.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,19 @@ func runPreferredByOriginEngines(enginers []scraper.Enginer, wgPreferredByOrigin
continue
}

c := sync.Cond{L: &sync.Mutex{}}
go func() {
c.L.Lock()
c.Wait()
c.L.Unlock()
wgPreferredByOriginEngines.Done()
}()
var wgWorkers sync.WaitGroup
wgWorkers.Add(len(workers))
successOrigin := sync.Cond{L: &sync.Mutex{}}
go waitForSuccessOrFinish(&successOrigin, &wgWorkers, wgPreferredByOriginEngines)

for _, engName := range workers {
enginer := enginers[engName]
resChan := make(chan result.ResultScraped, 100)
engChan <- resChan
go func() {
// Indicate that the worker is done, successful or not.
defer wgWorkers.Done()

searchOnce[engName].Do(func() {
log.Trace().
Str("engine", engName.String()).
Expand Down Expand Up @@ -80,10 +81,11 @@ func runPreferredByOriginEngines(enginers []scraper.Enginer, wgPreferredByOrigin
}
})

// Indicate that the worker was successful.
if searchOnce[engName].Success() {
c.L.Lock()
c.Signal()
c.L.Unlock()
successOrigin.L.Lock()
successOrigin.Signal()
successOrigin.L.Unlock()
}
}()
}
Expand Down
23 changes: 13 additions & 10 deletions src/search/run_required_origins.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,19 @@ func runRequiredByOriginEngines(enginers []scraper.Enginer, wgRequiredByOriginEn
continue
}

c := sync.Cond{L: &sync.Mutex{}}
go func() {
c.L.Lock()
c.Wait()
c.L.Unlock()
wgRequiredByOriginEngines.Done()
}()
var wgWorkers sync.WaitGroup
wgWorkers.Add(len(workers))
successOrigin := sync.Cond{L: &sync.Mutex{}}
go waitForSuccessOrFinish(&successOrigin, &wgWorkers, wgRequiredByOriginEngines)

for _, engName := range workers {
enginer := enginers[engName]
resChan := make(chan result.ResultScraped, 100)
engChan <- resChan
go func() {
// Indicate that the worker is done, successful or not.
defer wgWorkers.Done()

searchOnce[engName].Do(func() {
log.Trace().
Str("engine", engName.String()).
Expand Down Expand Up @@ -79,10 +80,12 @@ func runRequiredByOriginEngines(enginers []scraper.Enginer, wgRequiredByOriginEn
searchOnce[engName].Scraped()
}
})

// Indicate that the worker was successful.
if searchOnce[engName].Success() {
c.L.Lock()
c.Signal()
c.L.Unlock()
successOrigin.L.Lock()
successOrigin.Signal()
successOrigin.L.Unlock()
}
}()
}
Expand Down

0 comments on commit 128cebc

Please sign in to comment.