Решение на Concurrent Retry Executor от Кирил Костов

Обратно към всички решения

Към профила на Кирил Костов

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 9 успешни тест(а)
  • 0 неуспешни тест(а)

Код

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(done)
close(limiter)
close(returnChannel)
}()
return returnChannel
}

Лог от изпълнението

PASS
ok  	_/tmp/d20161115-21147-8b12a2	0.003s
PASS
ok  	_/tmp/d20161115-21147-8b12a2	0.026s
PASS
ok  	_/tmp/d20161115-21147-8b12a2	0.027s
PASS
ok  	_/tmp/d20161115-21147-8b12a2	0.004s
PASS
ok  	_/tmp/d20161115-21147-8b12a2	0.004s
PASS
ok  	_/tmp/d20161115-21147-8b12a2	0.066s
PASS
ok  	_/tmp/d20161115-21147-8b12a2	0.183s
PASS
ok  	_/tmp/d20161115-21147-8b12a2	0.301s
PASS
ok  	_/tmp/d20161115-21147-8b12a2	0.203s

История (2 версии и 0 коментара)

Кирил обнови решението на 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
+}

Кирил обнови решението на 14.11.2016 15:12 (преди над 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(done)
+ close(limiter)
close(returnChannel)
}()
return returnChannel
}