Решение на Concurrent Retry Executor от Емил Дудев

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

Към профила на Емил Дудев

Резултати

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

Код

package main
type taskDescription struct {
index int
function func() string
}
type compoundResult struct {
index int
result string
}
func ConcurrentRetryExecutor(tasks []func() string,
concurrentLimit int,
retryLimit int) <-chan compoundResult {
resultsChannel := make(chan compoundResult)
go func() {
tasksChannel := make(chan taskDescription)
workerChannel := make(chan int)
for i := 0; i < concurrentLimit; i++ {
go func() {
for taskToRun := range tasksChannel {
resultString := ""
retryCount := retryLimit
for resultString == "" && retryCount > 0 {
resultString = taskToRun.function()
result := compoundResult{
index: taskToRun.index,
result: resultString,
}
resultsChannel <- result
retryCount--
}
}
workerChannel <- 1
}()
}
for i, taskFunction := range tasks {
tasksChannel <- taskDescription{
index: i,
function: taskFunction,
}
}
close(tasksChannel)
for i := 0; i < concurrentLimit; i++ {
<-workerChannel
}
close(workerChannel)
close(resultsChannel)
}()
return resultsChannel
}

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

PASS
ok  	_/tmp/d20161115-21147-mxu0s2	0.003s
PASS
ok  	_/tmp/d20161115-21147-mxu0s2	0.024s
PASS
ok  	_/tmp/d20161115-21147-mxu0s2	0.022s
PASS
ok  	_/tmp/d20161115-21147-mxu0s2	0.003s
PASS
ok  	_/tmp/d20161115-21147-mxu0s2	0.003s
PASS
ok  	_/tmp/d20161115-21147-mxu0s2	0.062s
PASS
ok  	_/tmp/d20161115-21147-mxu0s2	0.159s
PASS
ok  	_/tmp/d20161115-21147-mxu0s2	0.219s
PASS
ok  	_/tmp/d20161115-21147-mxu0s2	0.203s

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

Емил обнови решението на 15.11.2016 11:17 (преди над 1 година)

+package main
+
+type taskDescription struct {
+ index int
+ function func() string
+}
+
+type compoundResult struct {
+ index int
+ result string
+}
+
+func ConcurrentRetryExecutor(tasks []func() string,
+ concurrentLimit int,
+ retryLimit int) <-chan compoundResult {
+ resultsChannel := make(chan compoundResult)
+
+ go func() {
+ tasksChannel := make(chan taskDescription)
+ workerChannel := make(chan int)
+
+ for i := 0; i < concurrentLimit; i++ {
+ go func() {
+ for taskToRun := range tasksChannel {
+ resultString := ""
+ retryCount := retryLimit
+
+ for resultString == "" && retryCount > 0 {
+ resultString = taskToRun.function()
+
+ result := compoundResult{
+ index: taskToRun.index,
+ result: resultString,
+ }
+
+ resultsChannel <- result
+
+ retryCount--
+ }
+ }
+ workerChannel <- 1
+ }()
+ }
+
+ for i, taskFunction := range tasks {
+ tasksChannel <- taskDescription{
+ index: i,
+ function: taskFunction,
+ }
+ }
+ close(tasksChannel)
+
+ for i := 0; i < concurrentLimit; i++ {
+ <-workerChannel
+ }
+ close(workerChannel)
+ close(resultsChannel)
+ }()
+
+ return resultsChannel
+}