Димитър обнови решението на 15.11.2016 00:04 (преди над 1 година)
+package main
+
+func ConcurrentRetryExecutor(tasks []func() string, concurrentLimit int, retryLimit int) <-chan struct {
+ index int
+ result string
+} {
+ ch := make(chan struct {
+ index int
+ result string
+ })
+ internalCh := make(chan struct{}, concurrentLimit)
+ count := 0
+
+ go func() {
+ for index, _ := range tasks {
+ internalCh <- struct{}{}
+
+ go func(index int) {
+ if index >= len(tasks) {
+ return
+ }
+ for i := 0; i < retryLimit; i++ {
+ result := tasks[index]()
+ var str struct {
+ index int
+ result string
+ }
+ str.index = index
+ str.result = result
+ ch <- str
+ if result != "" {
+ break
+ }
+ }
+ <-internalCh
+ }(index)
+ count++
+ }
+
+ for len(internalCh) != 0 {
+
+ }
+ close(internalCh)
+ close(ch)
+ }()
+
+ return ch
+}