Кирил обнови решението на 14.11.2016 15:08 (преди над 1 година)
+package main
+
+func ConcurrentRetryExecutor(tasks []func() string, concurrentLimit int, retryLimit int) <-chan struct {
+ index int
+ result string
+} {
+ returnChannel := make(chan struct {
+ index int
+ result string
+ })
+
+ limiter := make(chan struct{}, concurrentLimit)
+ done := make(chan struct{}, len(tasks))
+
+ go func() {
+ for i := 0; i < len(tasks); i++ {
+
+ limiter <- struct{}{}
+
+ go func(ind int, task func() string) {
+ for j := 0; j < retryLimit; j++ {
+ ret := task()
+ returnChannel <- struct {
+ index int
+ result string
+ }{index: ind, result: ret}
+
+ if ret != "" {
+ break
+ }
+ }
+
+ done <- struct{}{}
+ <-limiter
+
+ }(i, tasks[i])
+
+ }
+
+ for i := 0; i < len(tasks); i++ {
+ <-done
+ }
+
+ close(returnChannel)
+ }()
+
+ return returnChannel
+}