Skip to content

Commit

Permalink
Merge pull request hound-search#272 from sahildua2305/concurrent-inde…
Browse files Browse the repository at this point in the history
…xing

searcher: make indexing of repos concurrent
  • Loading branch information
kellegous committed Jan 18, 2018
2 parents 1b9c3e5 + 302e597 commit 92e228e
Showing 1 changed file with 54 additions and 6 deletions.
60 changes: 54 additions & 6 deletions searcher/searcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ type Searcher struct {
doneCh chan empty
}

// Struct used to send the results from newSearcherConcurrent function.
// This struct can either have a non-nil searcher or a non-nil error
// depending on what newSearcher function returns.
type searcherResult struct {
name string
searcher *Searcher
err error
}

type empty struct{}
type limiter chan bool

Expand Down Expand Up @@ -277,15 +286,25 @@ func MakeAll(cfg *config.Config) (map[string]*Searcher, map[string]error, error)

lim := makeLimiter(cfg.MaxConcurrentIndexers)

n := len(cfg.Repos)
// Channel to receive the results from newSearcherConcurrent function.
resultCh := make(chan searcherResult, n)

// Start new searchers for all repos in different go routines while
// respecting cfg.MaxConcurrentIndexers.
for name, repo := range cfg.Repos {
s, err := newSearcher(cfg.DbPath, name, repo, refs, lim)
if err != nil {
log.Print(err)
errs[name] = err
go newSearcherConcurrent(cfg.DbPath, name, repo, refs, lim, resultCh)
}

// Collect the results on resultCh channel for all repos.
for i := 0; i < n; i++ {
r := <-resultCh
if r.err != nil {
log.Print(r.err)
errs[r.name] = r.err
continue
}

searchers[name] = s
searchers[r.name] = r.searcher
}

if err := refs.removeUnclaimed(); err != nil {
Expand Down Expand Up @@ -464,3 +483,32 @@ func newSearcher(

return s, nil
}

// This function is a wrapper around `newSearcher` function.
// It respects the parameter `cfg.MaxConcurrentIndexers` while making the
// creation of searchers for various repositories concurrent.
func newSearcherConcurrent(
dbpath, name string,
repo *config.Repo,
refs *foundRefs,
lim limiter,
resultCh chan searcherResult) {

// acquire a token from the rate limiter
lim.Acquire()
defer lim.Release()

s, err := newSearcher(dbpath, name, repo, refs, lim)
if err != nil {
resultCh <- searcherResult{
name: name,
err: err,
}
return
}

resultCh <- searcherResult{
name: name,
searcher: s,
}
}

0 comments on commit 92e228e

Please sign in to comment.