Решение на 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
} {
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() {
}

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

PASS
ok  	_/tmp/d20161115-21147-1arlt2i	0.002s
PASS
ok  	_/tmp/d20161115-21147-1arlt2i	0.032s
PASS
ok  	_/tmp/d20161115-21147-1arlt2i	0.025s
PASS
ok  	_/tmp/d20161115-21147-1arlt2i	0.003s
PASS
ok  	_/tmp/d20161115-21147-1arlt2i	0.003s
PASS
ok  	_/tmp/d20161115-21147-1arlt2i	0.069s
PASS
ok  	_/tmp/d20161115-21147-1arlt2i	0.188s
PASS
ok  	_/tmp/d20161115-21147-1arlt2i	0.299s
PASS
ok  	_/tmp/d20161115-21147-1arlt2i	0.203s

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

Елица обнови решението на 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() {
+}