FetchDescriptionsConcurrent fetches descriptions with bounded concurrency.
(client *api.Client, host, owner, repo string, skills []Skill, onProgress func(done, total int))
| 636 | |
| 637 | // FetchDescriptionsConcurrent fetches descriptions with bounded concurrency. |
| 638 | func FetchDescriptionsConcurrent(client *api.Client, host, owner, repo string, skills []Skill, onProgress func(done, total int)) { |
| 639 | total := 0 |
| 640 | for _, s := range skills { |
| 641 | if s.Description == "" { |
| 642 | total++ |
| 643 | } |
| 644 | } |
| 645 | if total == 0 { |
| 646 | return |
| 647 | } |
| 648 | |
| 649 | const maxWorkers = 10 |
| 650 | var wg sync.WaitGroup |
| 651 | var done atomic.Int32 |
| 652 | |
| 653 | jobs := make(chan *Skill) |
| 654 | |
| 655 | workers := min(maxWorkers, total) |
| 656 | for range workers { |
| 657 | wg.Go(func() { |
| 658 | for s := range jobs { |
| 659 | s.Description = fetchDescription(client, host, owner, repo, s) |
| 660 | |
| 661 | d := int(done.Add(1)) |
| 662 | if onProgress != nil { |
| 663 | onProgress(d, total) |
| 664 | } |
| 665 | } |
| 666 | }) |
| 667 | } |
| 668 | |
| 669 | for i := range skills { |
| 670 | if skills[i].Description == "" { |
| 671 | jobs <- &skills[i] |
| 672 | } |
| 673 | } |
| 674 | close(jobs) |
| 675 | wg.Wait() |
| 676 | } |
| 677 | |
| 678 | // DiscoverSkillByPathOptions controls optional behavior for DiscoverSkillByPathWithOptions. |
| 679 | type DiscoverSkillByPathOptions struct { |