Елица обнови решението на 15.11.2016 02:32 (преди над 1 година)
+package main
+
+func ConcurrentRetryExecutor(tasks []func() string, concurrentLimit int, retryLimit int) <-chan struct {
+ index int
+ result string
+} {
+ cReturn := make(chan struct {
+ index int
+ result string
+ })
+ go func(tasks []func() string, concurrentLimit int, retryLimit int) {
+ c := make(chan struct{}, concurrentLimit)
+ cClose := make(chan struct{}, len(tasks))
+ for i := 0; i < concurrentLimit; i++ {
+ c <- struct{}{}
+ }
+ for index, value := range tasks {
+ <-c
+ go func(c chan struct{}, cReturn chan struct {
+ index int
+ result string
+ }, index int, task func() string, retryLimit int) {
+ for i := 0; i < retryLimit; i++ {
+ value := task()
+ cReturn <- struct {
+ index int
+ result string
+ }{
+ index,
+ value,
+ }
+ if value != "" {
+ break
+ }
+ }
+ c <- struct{}{}
+ cClose <- struct{}{}
+ }(c, cReturn, index, value, retryLimit)
+ }
+ for i := 0; i < len(tasks); i++ {
+ <-cClose
+ }
+ close(c)
+ close(cClose)
+ close(cReturn)
+ }(tasks, concurrentLimit, retryLimit)
+ return cReturn
+}
+
+func main() {
+}