Skip to content

Commit

Permalink
Merge pull request #56 from yelizariev/fix-crashes
Browse files Browse the repository at this point in the history
show error message, when repos and results are inconsistent
  • Loading branch information
yelizariev authored Jul 14, 2020
2 parents 5625895 + 11e2cee commit eb96ae6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 42 deletions.
91 changes: 49 additions & 42 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,37 +139,74 @@ func searchAll(

n := len(repos)
searchersNum := min(n, cfg.MaxConcurrentSearchers)
// TODO: Syncronization scheme and the code in general looks a bit
// complicated. Can it be simplified somehow?
limiter := makeLimiter(searchersNum)
var wg sync.WaitGroup
defer limiter.Close()

// use a buffered channel to avoid routine leaks on errs.
ch := make(chan *preSearchResult, n)
enoughResults := makeWaiter()
var err error
firstUndone := 0
results := map[string]*preSearchResult{}
go func() {
defer limiter.Close()
defer enoughResults.Do()
resNum := 0
for i := 0; i < n; i++ {
r := <-ch
results[r.repo] = r
if r.err != nil {
err = r.err
return
}
if !r.res.Found {
r.cleanup.Do()
continue
}
firstUndone, resNum = checkResults(repos, results, firstUndone)
if resNum >= limitRepos {
break
}
}
if resNum < limitRepos {
firstUndone, resNum = checkResults(repos, results, firstUndone)
}
}()

var wg sync.WaitGroup
for _, repo := range repos {
if !limiter.Acquire() {
break
}
wg.Add(1)
go func(repo string) {
if !limiter.Acquire() {
return
}
wg.Add(1)
defer idx[repo].SearchCleanUp()
fms, err := idx[repo].PreSearch(query, opts)
cleanup := makeWaiter()
r := &preSearchResult{repo, fms, err, cleanup}
// send result
ch <- r
wg.Done()
// next worker can start the search
// next worker can make PreSearch
limiter.Release()
// wait
// wait before calling SearchCleanUp
cleanup.Wait()

}(repo)
}
enoughResults.Wait()
if err != nil {
return nil, 0, err
}

results := map[string]*preSearchResult{}
// cleanup excess repos right now
for i := firstUndone; i < n; i++ {
repo := repos[i]
r, processed := results[repo]
if !processed {
continue
}
r.cleanup.Do()
}
// cleanup other repos at the end
defer func() {
wg.Wait()
close(ch)
Expand All @@ -181,36 +218,6 @@ func searchAll(
res.cleanup.Do()
}
}()
firstUndone := 0
resNum := 0
for i := 0; i < n; i++ {
r := <-ch
results[r.repo] = r
if r.err != nil {
return nil, 0, r.err
}
if !r.res.Found {
r.cleanup.Do()
continue
}
firstUndone, resNum = checkResults(repos, results, firstUndone)
if resNum >= limitRepos {
break
}
}
if resNum < limitRepos {
firstUndone, resNum = checkResults(repos, results, firstUndone)
}
limiter.Close()
// cleanup excess repos
for i := firstUndone; i < n; i++ {
repo := repos[i]
r, processed := results[repo]
if !processed {
continue
}
r.cleanup.Do()
}

// Grep files
chFiles := make(chan *searchFilesResult, firstUndone)
Expand Down
7 changes: 7 additions & 0 deletions ui/assets/js/helpers/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ export const ExpandVars = (template, values) => {
};

export const UrlToRepo = (repo, path, line, rev) => {
if (!repo){
// panic
setTimeout(function(){
document.body.innerHTML = '<h1 style="text-align:center">Something went wrong. Try to refresh page.</h1>';
}, 100);
throw("repo is undefined");
}
let url = repo.url.replace(/\.git$/, '');
const pattern = repo['url-pattern'];
const filename = path.substring(path.lastIndexOf('/') + 1);
Expand Down

0 comments on commit eb96ae6

Please sign in to comment.