Трансфер на дрцгой комп

pull/5/head
Andrey Ivanov 2020-07-19 11:25:01 +03:00
parent a99946ef12
commit 628c884543
2 changed files with 22 additions and 2 deletions

View File

@ -1,4 +1,4 @@
module github.com/fixme_my_friend/hw05_parallel_execution
module github.com/tiburon-777/HW_OTUS/hw05_parallel_execution
go 1.14

View File

@ -2,6 +2,8 @@ package hw05_parallel_execution //nolint:golint,stylecheck
import (
"errors"
"log"
"sync"
)
var ErrErrorsLimitExceeded = errors.New("errors limit exceeded")
@ -10,6 +12,24 @@ type Task func() error
// Run starts tasks in N goroutines and stops its work when receiving M errors from tasks
func Run(tasks []Task, N int, M int) error {
// Place your code here
log.Println("Tasks:", len(tasks), "| Goroutines:", N, "| Errors:", M)
errs := 0
for i := 0; i < len(tasks); i = i + N {
wg := sync.WaitGroup{}
for g := 1; g <= N && i+g < len(tasks); g++ {
wg.Add(1)
go func(rt Task, i int, g int, errs *int) {
if err := rt; err != nil {
*errs++
}
wg.Done()
}(tasks[i+g], i, g, &errs)
}
wg.Wait()
if errs > M {
log.Println("Produced", errs, "errors of", M)
return ErrErrorsLimitExceeded
}
}
return nil
}