@@ -34,22 +34,13 @@ type Searcher struct {
3434 doneCh chan empty
3535}
3636
37- // Wrapper around searcher - used for sending the searcher struct to channel once
38- // successfully returned from `newSearcher` function.
39- // We need this wrapper because we need to have the `name` of a repo which a
40- // particular searcher belongs to.
41- type searcherWrapper struct {
42- searcher * Searcher
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 {
4341 name string
44- }
45-
46- // Wrapper around error - used for sending error to channel once returned from
47- // `newSearcher` function.
48- // We need this wrapper because we need to have the `name` of a repo which a
49- // particular error belongs to.
50- type errorWrapper struct {
51- error error
52- name string
42+ searcher * Searcher
43+ err error
5344}
5445
5546type empty struct {}
@@ -295,49 +286,30 @@ func MakeAll(cfg *config.Config) (map[string]*Searcher, map[string]error, error)
295286
296287 lim := makeLimiter (cfg .MaxConcurrentIndexers )
297288
298- // Channel to receive the successfully created searchers.
299- searcherCh := make (chan searcherWrapper , 1 )
300- // Channel to receive the errors in creation of searchers.
301- errorCh := make (chan errorWrapper , 1 )
302- // Channel to quit the go routine listening for new successful searchers or errors.
303- quitCh := make (chan struct {}, 1 )
304-
305- // Create a wait group for total number of repos so that we can proceed once the
306- // go routines to create searchers for all repos have returned.
307- var wg sync.WaitGroup
308- wg .Add (len (cfg .Repos ))
289+ // Channel to receive the results from newSearcherConcurrent function.
290+ resultCh := make (chan searcherResult , 1 )
309291
292+ // Start new searchers for all repos in different go routines while
293+ // respecting cfg.MaxConcurrentIndexers.
310294 for name , repo := range cfg .Repos {
311- go newSearcherConcurrent (cfg .DbPath , name , repo , refs , lim , searcherCh , errorCh )
295+ go newSearcherConcurrent (cfg .DbPath , name , repo , refs , lim , resultCh )
312296 }
313297
314- // Create a listener on searcherCh and errorCh.
315- // It also listens on quitCh to decide when to return.
316- go func () {
317- for {
318- select {
319- case sw := <- searcherCh :
320- searchers [sw .name ] = sw .searcher
321- wg .Done ()
322- case ew := <- errorCh :
323- log .Print (ew .error )
324- errs [ew .name ] = ew .error
325- wg .Done ()
326- case <- quitCh :
327- return
328- }
298+ // Collect the results on resultCh channel for all repos.
299+ for range cfg .Repos {
300+ r := <- resultCh
301+ if r .err != nil {
302+ log .Print (r .err )
303+ errs [r .name ] = r .err
304+ continue
329305 }
330- }()
306+ searchers [r .name ] = r .searcher
307+ }
331308
332309 if err := refs .removeUnclaimed (); err != nil {
333310 return nil , nil , err
334311 }
335312
336- // Wait for all go routines to finish.
337- wg .Wait ()
338- // Send close signal to quitCh to return the listening go routine.
339- close (quitCh )
340-
341313 // after all the repos are in good shape, we start their polling
342314 for _ , s := range searchers {
343315 s .begin ()
@@ -519,24 +491,25 @@ func newSearcherConcurrent(
519491 repo * config.Repo ,
520492 refs * foundRefs ,
521493 lim limiter ,
522- searcherCh chan searcherWrapper ,
523- errorCh chan errorWrapper ) {
494+ resultCh chan searcherResult ) {
524495
525496 // acquire a token from the rate limiter
526497 lim .Acquire ()
527498 defer lim .Release ()
528499
529500 s , err := newSearcher (dbpath , name , repo , refs , lim )
530501 if err != nil {
531- errorCh <- errorWrapper {
532- error : err ,
533- name : name ,
502+ resultCh <- searcherResult {
503+ name : name ,
504+ searcher : nil ,
505+ err : err ,
534506 }
535507 return
536508 }
537509
538- searcherCh <- searcherWrapper {
510+ resultCh <- searcherResult {
511+ name : name ,
539512 searcher : s ,
540- name : name ,
513+ err : nil ,
541514 }
542515}
0 commit comments