Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(search): forgot a case when no workers return for a origin #331

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading