Решение на Concurrent Retry Executor от Катя Спасова

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

Към профила на Катя Спасова

Резултати

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

Код

package main
import (
"strconv"
"strings"
"time"
)
func OrderedLogDrainer(logs chan (chan string)) (result chan string) {
result = make(chan string, 100)
m := make(map[int]chan string)
go func() {
var i = 0
for {
log, logsOpened := <-logs
if !logsOpened {
break
}
i += 1
m[i] = make(chan string, 100)
go func(num int) {
for {
logItem, logOpened := <-log
if !logOpened {
close(m[num])
break
}
lineContents := []string{strconv.Itoa(num), logItem}
var modifiedItem string = strings.Join(lineContents, "\t")
m[num] <- modifiedItem
}
}(i)
}
}()
go func() {
num := 1
maxTries := 3
for {
ch, ok := m[num]
if ok {
for {
logItem, logOpened := <-ch
if logOpened {
result <- logItem
} else {
num += 1
break
}
}
} else {
if maxTries > 0 {
time.Sleep(10 * time.Millisecond)
maxTries -= 1
} else {
break
}
}
}
close(result)
}()
return result
}

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

PASS
ok  	_/tmp/d20151110-19113-101z62j	0.033s
PASS
ok  	_/tmp/d20151110-19113-101z62j	0.033s
PASS
ok  	_/tmp/d20151110-19113-101z62j	0.033s
PASS
ok  	_/tmp/d20151110-19113-101z62j	0.033s
PASS
ok  	_/tmp/d20151110-19113-101z62j	0.033s
PASS
ok  	_/tmp/d20151110-19113-101z62j	0.723s
PASS
ok  	_/tmp/d20151110-19113-101z62j	0.034s
PASS
ok  	_/tmp/d20151110-19113-101z62j	0.033s

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

Катя обнови решението на 10.11.2015 02:37 (преди над 2 години)

+package main
+
+import (
+ "strconv"
+ "strings"
+ "time"
+)
+
+func OrderedLogDrainer(logs chan (chan string)) (result chan string) {
+ result = make(chan string, 100)
+ m := make(map[int]chan string)
+ go func() {
+ var i = 0
+ for {
+ log, logsOpened := <-logs
+ if !logsOpened {
+ break
+ }
+ i += 1
+ m[i] = make(chan string, 100)
+ go func(num int) {
+ for {
+ logItem, logOpened := <-log
+ if !logOpened {
+ close(m[num])
+ break
+ }
+ lineContents := []string{strconv.Itoa(num), logItem}
+ var modifiedItem string = strings.Join(lineContents, "\t")
+ m[num] <- modifiedItem
+ }
+ }(i)
+ }
+ }()
+
+ go func() {
+ num := 1
+ maxTries := 3
+ for {
+ ch, ok := m[num]
+ if ok {
+ for {
+ logItem, logOpened := <-ch
+ if logOpened {
+ result <- logItem
+ } else {
+ num += 1
+ break
+ }
+ }
+ } else {
+ if maxTries > 0 {
+ time.Sleep(10 * time.Millisecond)
+ maxTries -= 1
+ } else {
+ break
+ }
+ }
+ }
+ close(result)
+ }()
+ return result
+}