@@ -34,6 +34,15 @@ type Searcher struct {
3434 doneCh chan empty
3535}
3636
37+ // Struct used to send the results from newSearcherConcurrent function.
38+ // This struct can either have a non-nil searcher or a non-nil error
39+ // depending on what newSearcher function returns.
40+ type searcherResult struct {
41+ name string
42+ searcher * Searcher
43+ err error
44+ }
45+
3746type empty struct {}
3847type limiter chan bool
3948
@@ -277,15 +286,25 @@ func MakeAll(cfg *config.Config) (map[string]*Searcher, map[string]error, error)
277286
278287 lim := makeLimiter (cfg .MaxConcurrentIndexers )
279288
289+ n := len (cfg .Repos )
290+ // Channel to receive the results from newSearcherConcurrent function.
291+ resultCh := make (chan searcherResult , n )
292+
293+ // Start new searchers for all repos in different go routines while
294+ // respecting cfg.MaxConcurrentIndexers.
280295 for name , repo := range cfg .Repos {
281- s , err := newSearcher (cfg .DbPath , name , repo , refs , lim )
282- if err != nil {
283- log .Print (err )
284- errs [name ] = err
296+ go newSearcherConcurrent (cfg .DbPath , name , repo , refs , lim , resultCh )
297+ }
298+
299+ // Collect the results on resultCh channel for all repos.
300+ for i := 0 ; i < n ; i ++ {
301+ r := <- resultCh
302+ if r .err != nil {
303+ log .Print (r .err )
304+ errs [r .name ] = r .err
285305 continue
286306 }
287-
288- searchers [name ] = s
307+ searchers [r .name ] = r .searcher
289308 }
290309
291310 if err := refs .removeUnclaimed (); err != nil {
@@ -464,3 +483,32 @@ func newSearcher(
464483
465484 return s , nil
466485}
486+
487+ // This function is a wrapper around `newSearcher` function.
488+ // It respects the parameter `cfg.MaxConcurrentIndexers` while making the
489+ // creation of searchers for various repositories concurrent.
490+ func newSearcherConcurrent (
491+ dbpath , name string ,
492+ repo * config.Repo ,
493+ refs * foundRefs ,
494+ lim limiter ,
495+ resultCh chan searcherResult ) {
496+
497+ // acquire a token from the rate limiter
498+ lim .Acquire ()
499+ defer lim .Release ()
500+
501+ s , err := newSearcher (dbpath , name , repo , refs , lim )
502+ if err != nil {
503+ resultCh <- searcherResult {
504+ name : name ,
505+ err : err ,
506+ }
507+ return
508+ }
509+
510+ resultCh <- searcherResult {
511+ name : name ,
512+ searcher : s ,
513+ }
514+ }
0 commit comments