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

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

PASS
ok  	_/tmp/d20161115-21147-o9ttxw	0.003s
PASS
ok  	_/tmp/d20161115-21147-o9ttxw	0.025s
PASS
ok  	_/tmp/d20161115-21147-o9ttxw	0.025s
PASS
ok  	_/tmp/d20161115-21147-o9ttxw	0.007s
PASS
ok  	_/tmp/d20161115-21147-o9ttxw	0.003s
PASS
ok  	_/tmp/d20161115-21147-o9ttxw	0.071s
PASS
ok  	_/tmp/d20161115-21147-o9ttxw	0.234s
PASS
ok  	_/tmp/d20161115-21147-o9ttxw	0.297s
PASS
ok  	_/tmp/d20161115-21147-o9ttxw	0.203s

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

Димитър обнови решението на 15.11.2016 00:04 (преди над 1 година)

+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
+ })
+ internalCh := make(chan struct{}, concurrentLimit)
+ count := 0
+
+ go func() {
+ for index, _ := range tasks {
+ internalCh <- struct{}{}
+
+ go func(index int) {
+ if index >= len(tasks) {
+ return
+ }
+ for i := 0; i < retryLimit; i++ {
+ result := tasks[index]()
+ var str struct {
+ index int
+ result string
+ }
+ str.index = index
+ str.result = result
+ ch <- str
+ if result != "" {
+ break
+ }
+ }
+ <-internalCh
+ }(index)
+ count++
+ }
+
+ for len(internalCh) != 0 {
+
+ }
+ close(internalCh)
+ close(ch)
+ }()
+
+ return ch
+}

Димитър обнови решението на 15.11.2016 00:06 (преди над 1 година)

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
})
internalCh := make(chan struct{}, concurrentLimit)
count := 0
go func() {
for index, _ := range tasks {
internalCh <- struct{}{}
go func(index int) {
- if index >= len(tasks) {
- return
- }
for i := 0; i < retryLimit; i++ {
result := tasks[index]()
var str struct {
index int
result string
}
str.index = index
str.result = result
ch <- str
if result != "" {
break
}
}
<-internalCh
}(index)
count++
}
for len(internalCh) != 0 {
}
close(internalCh)
close(ch)
}()
return ch
}

Димитър обнови решението на 15.11.2016 02:33 (преди над 1 година)

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
})
internalCh := make(chan struct{}, concurrentLimit)
- count := 0
go func() {
for index, _ := range tasks {
internalCh <- struct{}{}
go func(index int) {
for i := 0; i < retryLimit; i++ {
result := tasks[index]()
var str struct {
index int
result string
}
str.index = index
str.result = result
ch <- str
if result != "" {
break
}
}
<-internalCh
}(index)
- count++
}
- for len(internalCh) != 0 {
+ for i := 0; i < concurrentLimit; i++ {
+ internalCh <- struct{}{}
+ }
+ for i := 0; i < concurrentLimit; i++ {
+ <-internalCh
}
+
close(internalCh)
close(ch)
}()
return ch
}

Димитър обнови решението на 15.11.2016 03:02 (преди над 1 година)

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
})
internalCh := make(chan struct{}, concurrentLimit)
go func() {
for index, _ := range tasks {
internalCh <- struct{}{}
go func(index int) {
for i := 0; i < retryLimit; i++ {
result := tasks[index]()
var str struct {
index int
result string
}
str.index = index
str.result = result
ch <- str
if result != "" {
break
}
}
<-internalCh
}(index)
}
for i := 0; i < concurrentLimit; i++ {
internalCh <- struct{}{}
}
- for i := 0; i < concurrentLimit; i++ {
- <-internalCh
- }
-
close(internalCh)
close(ch)
}()
return ch
}