(paths []string)
| 503 | } |
| 504 | |
| 505 | func countLinesInFilesParallel(paths []string) map[string]int { |
| 506 | if len(paths) == 0 { |
| 507 | return make(map[string]int) |
| 508 | } |
| 509 | |
| 510 | workerCount := runtime.GOMAXPROCS(0) |
| 511 | if workerCount < 1 { |
| 512 | workerCount = 1 |
| 513 | } |
| 514 | if workerCount > len(paths) { |
| 515 | workerCount = len(paths) |
| 516 | } |
| 517 | |
| 518 | type lineCountResult struct { |
| 519 | path string |
| 520 | lines int |
| 521 | } |
| 522 | |
| 523 | jobs := make(chan string) |
| 524 | results := make(chan lineCountResult, len(paths)) |
| 525 | var wg sync.WaitGroup |
| 526 | |
| 527 | for range workerCount { |
| 528 | wg.Add(1) |
| 529 | go func() { |
| 530 | defer wg.Done() |
| 531 | for path := range jobs { |
| 532 | lines, err := countLinesInFile(path) |
| 533 | if err != nil { |
| 534 | continue |
| 535 | } |
| 536 | results <- lineCountResult{ |
| 537 | path: path, |
| 538 | lines: lines, |
| 539 | } |
| 540 | } |
| 541 | }() |
| 542 | } |
| 543 | |
| 544 | for _, path := range paths { |
| 545 | jobs <- path |
| 546 | } |
| 547 | close(jobs) |
| 548 | |
| 549 | wg.Wait() |
| 550 | close(results) |
| 551 | |
| 552 | lineCounts := make(map[string]int, len(paths)) |
| 553 | for result := range results { |
| 554 | lineCounts[result.path] = result.lines |
| 555 | } |
| 556 | |
| 557 | return lineCounts |
| 558 | } |
no test coverage detected