Решение на 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
} {
ch := make(chan struct {
index int
result string
})
var limit = make(chan struct{}, concurrentLimit)
wg := make(chan struct{})
go func() {
for index, task := range tasks {
limit <- struct{}{}
go func(index int, task func() string) {
defer func() {
wg <- struct{}{}
}()
t := struct {
index int
result string
}{}
for i := 0; i < retryLimit; i++ {
t.index = index
t.result = task()
ch <- t
if t.result != "" {
break
}
}
<-limit
}(index, task)
}
}()
go func() {
for i := 0; i < len(tasks); i++ {
<-wg
}
close(ch)
}()
return ch
}

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

PASS
ok  	_/tmp/d20161115-21147-lzf0qu	0.003s
PASS
ok  	_/tmp/d20161115-21147-lzf0qu	0.028s
PASS
ok  	_/tmp/d20161115-21147-lzf0qu	0.028s
PASS
ok  	_/tmp/d20161115-21147-lzf0qu	0.003s
PASS
ok  	_/tmp/d20161115-21147-lzf0qu	0.003s
PASS
ok  	_/tmp/d20161115-21147-lzf0qu	0.075s
PASS
ok  	_/tmp/d20161115-21147-lzf0qu	0.293s
PASS
ok  	_/tmp/d20161115-21147-lzf0qu	0.352s
PASS
ok  	_/tmp/d20161115-21147-lzf0qu	0.203s

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

Пламен обнови решението на 13.11.2016 23:38 (преди над 1 година)

+package main
+
+type Task struct {
+ index int
+ result string
+}
+
+func main() {}
+
+func ConcurrentRetryExecutor(tasks []func() string, concurrentLimit int, retryLimit int) <-chan struct {
+ index int
+ result string
+} {
+ ch := make(chan struct {
+ index int
+ result string
+ })
+
+ var limit = make(chan struct{}, concurrentLimit)
+ wg := make(chan struct{})
+
+ var t Task
+
+ go func() {
+ for index, task := range tasks {
+ limit <- struct{}{}
+ go func(index int, task func() string) {
+ defer func() {
+ wg <- struct{}{}
+ }()
+ for i := 0; i < retryLimit; i++ {
+ t.result = task()
+ t.index = index
+ if t.result != "" {
+ ch <- t
+ break
+ }
+ ch <- t
+ }
+ <-limit
+ }(index, task)
+ }
+ }()
+
+ go func() {
+ for i := 0; i < len(tasks); i++ {
+ <-wg
+ }
+ close(ch)
+ }()
+ return ch
+}

Пламен обнови решението на 13.11.2016 23:41 (преди над 1 година)

package main
type Task struct {
index int
result string
}
func main() {}
func ConcurrentRetryExecutor(tasks []func() string, concurrentLimit int, retryLimit int) <-chan struct {
index int
result string
} {
ch := make(chan struct {
index int
result string
})
var limit = make(chan struct{}, concurrentLimit)
wg := make(chan struct{})
var t Task
go func() {
for index, task := range tasks {
limit <- struct{}{}
go func(index int, task func() string) {
defer func() {
wg <- struct{}{}
}()
for i := 0; i < retryLimit; i++ {
t.result = task()

Ами.. за момента си мисля за нещо от сорта на:

for i := 0; i < retryLimit; i++ {
    if task() != "" {
            ch <- struct {
                index  int
                result string
            }{index, task()}
            break
    }
    ch <- struct {
            index  int
            result string
    }{index, task()}
}

ноо.. не всички тестове минават:

~/code/go-homework/tasks/02(branch:master*) » go test
--- FAIL: TestSimpleScenario (0.20s)
    solution_test.go:38: Expected the second result to be an error from the lazy dog but received {index:1 result:woof}
FAIL
exit status 1
FAIL    _/Users/plamer/code/go-homework/tasks/02    5.839s
t.index = index
+ ch <- t
if t.result != "" {
- ch <- t
break
}
- ch <- t
}
<-limit
}(index, task)
}
}()
go func() {
for i := 0; i < len(tasks); i++ {
<-wg
}
close(ch)
}()
return ch
}

Пламен обнови решението на 14.11.2016 10:25 (преди над 1 година)

package main
-type Task struct {
- index int
- result string
-}
-
-func main() {}
-
func ConcurrentRetryExecutor(tasks []func() string, concurrentLimit int, retryLimit int) <-chan struct {
index int
result string
} {
ch := make(chan struct {
index int
result string
})
var limit = make(chan struct{}, concurrentLimit)
wg := make(chan struct{})
- var t Task
-
go func() {
for index, task := range tasks {
limit <- struct{}{}
go func(index int, task func() string) {
defer func() {
wg <- struct{}{}
}()
+
+ t := struct {
+ index int
+ result string
+ }{}
+
for i := 0; i < retryLimit; i++ {
- t.result = task()
t.index = index
+ t.result = task()
ch <- t
if t.result != "" {
break
}
}
<-limit
}(index, task)
}
}()
go func() {
for i := 0; i < len(tasks); i++ {
<-wg
}
close(ch)
}()
return ch
}