Решение на 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
} {
chanLessThanLimitGoroutines := make(chan struct{}, concurrentLimit)
chanWaitGoroutinesToFinish := make(chan struct{}, len(tasks))
results := make(chan struct {
index int
result string
}, len(tasks)*retryLimit)
go func() {
for i := range tasks {
chanLessThanLimitGoroutines <- struct{}{}
go func(task func() string, indexFunc int) {
for j := 0; j < retryLimit; j++ {
strResult := task()
results <- struct {
index int
result string
}{indexFunc, strResult}
if strResult != "" {
break
}
}
<-chanLessThanLimitGoroutines
chanWaitGoroutinesToFinish <- struct{}{}
}(tasks[i], i)
}
for i := 0; i < len(tasks); i++ {
<-chanWaitGoroutinesToFinish
}
close(results)
}()
return results
}

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

PASS
ok  	_/tmp/d20161115-21147-9hj8nk	0.003s
PASS
ok  	_/tmp/d20161115-21147-9hj8nk	0.027s
PASS
ok  	_/tmp/d20161115-21147-9hj8nk	0.023s
PASS
ok  	_/tmp/d20161115-21147-9hj8nk	0.003s
PASS
ok  	_/tmp/d20161115-21147-9hj8nk	0.014s
PASS
ok  	_/tmp/d20161115-21147-9hj8nk	0.093s
PASS
ok  	_/tmp/d20161115-21147-9hj8nk	0.565s
PASS
ok  	_/tmp/d20161115-21147-9hj8nk	0.252s
PASS
ok  	_/tmp/d20161115-21147-9hj8nk	0.203s

История (3 версии и 5 коментара)

Живко обнови решението на 13.11.2016 20:57 (преди над 1 година)

+package main
+
+func ConcurrentRetryExecutor(tasks []func() string, concurrentLimit int, retryLimit int) <-chan struct {
+ index int
+ result string
+} {
+ chanLessThanLimitGoroutines := make(chan struct{}, concurrentLimit)
+ chanWaitGoroutinesToFinish := make(chan struct{}, len(tasks))
+ results := make(chan struct {
+ index int
+ result string
+ }, len(tasks)*retryLimit)
+ for i := range tasks {
+ chanLessThanLimitGoroutines <- struct{}{}
+ go func(task func() string, indexFunc int) {
+ for j := 0; j < retryLimit; j++ {
+ strResult := task()
+ results <- struct {
+ index int
+ result string
+ }{indexFunc, strResult}
+ if strResult != "" {
+ break
+ }
+ }
+ <-chanLessThanLimitGoroutines
+ chanWaitGoroutinesToFinish <- struct{}{}
+ }(tasks[i], i)
+ }
+ for i := 0; i < len(tasks); i++ {
+ <-chanWaitGoroutinesToFinish
+ }
+ close(results)
+ return results
+}

Живко обнови решението на 14.11.2016 01:28 (преди над 1 година)

package main
func ConcurrentRetryExecutor(tasks []func() string, concurrentLimit int, retryLimit int) <-chan struct {
index int
result string
} {
chanLessThanLimitGoroutines := make(chan struct{}, concurrentLimit)
chanWaitGoroutinesToFinish := make(chan struct{}, len(tasks))
results := make(chan struct {
index int
result string
}, len(tasks)*retryLimit)
for i := range tasks {
chanLessThanLimitGoroutines <- struct{}{}
go func(task func() string, indexFunc int) {
for j := 0; j < retryLimit; j++ {
strResult := task()
results <- struct {
index int
result string
}{indexFunc, strResult}
if strResult != "" {
break
}
}
<-chanLessThanLimitGoroutines
chanWaitGoroutinesToFinish <- struct{}{}
}(tasks[i], i)
}
- for i := 0; i < len(tasks); i++ {
- <-chanWaitGoroutinesToFinish
- }
- close(results)
+ go func() {
+ for i := 0; i < len(tasks); i++ {
+ <-chanWaitGoroutinesToFinish
+ }
+ close(results)
+ }()
return results
-}
+}

Живко обнови решението на 14.11.2016 22:51 (преди над 1 година)

package main
func ConcurrentRetryExecutor(tasks []func() string, concurrentLimit int, retryLimit int) <-chan struct {
index int
result string
} {
chanLessThanLimitGoroutines := make(chan struct{}, concurrentLimit)
chanWaitGoroutinesToFinish := make(chan struct{}, len(tasks))
results := make(chan struct {
index int
result string
}, len(tasks)*retryLimit)
- for i := range tasks {
- chanLessThanLimitGoroutines <- struct{}{}
- go func(task func() string, indexFunc int) {
- for j := 0; j < retryLimit; j++ {
- strResult := task()
- results <- struct {
- index int
- result string
- }{indexFunc, strResult}
- if strResult != "" {
- break
- }
- }
- <-chanLessThanLimitGoroutines
- chanWaitGoroutinesToFinish <- struct{}{}
- }(tasks[i], i)
- }
go func() {
+ for i := range tasks {
+ chanLessThanLimitGoroutines <- struct{}{}
+ go func(task func() string, indexFunc int) {
+ for j := 0; j < retryLimit; j++ {
+ strResult := task()
+ results <- struct {
+ index int
+ result string
+ }{indexFunc, strResult}
+ if strResult != "" {
+ break
+ }
+ }
+ <-chanLessThanLimitGoroutines
+ chanWaitGoroutinesToFinish <- struct{}{}
+ }(tasks[i], i)
+ }
for i := 0; i < len(tasks); i++ {
<-chanWaitGoroutinesToFinish
}
close(results)
}()
return results
}
+