Решение на Concurrent Retry Executor от Ангел Ангелов

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

Към профила на Ангел Ангелов

Резултати

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

Код

package main
import "fmt"
func bufferLog(log chan string, logIndex int) chan string {
bufferedLog := make(chan string, 1024)
go func(bufferedLog chan string, log chan string) {
for message := range log {
bufferedLog <- fmt.Sprintf("%d\t%s", logIndex, message)
}
close(bufferedLog)
}(bufferedLog, log)
return bufferedLog
}
func bufferLogs(logs chan (chan string)) chan (chan string) {
bufferedLogs := make(chan (chan string), 1024)
go func(bufferedLogs chan (chan string), logs chan (chan string)) {
var logIndex int
for log := range logs {
logIndex++
bufferedLogs <- bufferLog(log, logIndex)
}
close(bufferedLogs)
}(bufferedLogs, logs)
return bufferedLogs
}
func actualLogDrainer(logs chan (chan string), result chan string) {
for log := range bufferLogs(logs) {
for message := range log {
result <- message
}
}
close(result)
}
func OrderedLogDrainer(logs chan (chan string)) chan string {
out := make(chan string)
go actualLogDrainer(logs, out)
return out
}

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

PASS
ok  	_/tmp/d20151110-19113-1wmpxvq	0.003s
PASS
ok  	_/tmp/d20151110-19113-1wmpxvq	0.003s
PASS
ok  	_/tmp/d20151110-19113-1wmpxvq	0.003s
PASS
ok  	_/tmp/d20151110-19113-1wmpxvq	0.003s
PASS
ok  	_/tmp/d20151110-19113-1wmpxvq	0.004s
PASS
ok  	_/tmp/d20151110-19113-1wmpxvq	0.703s
PASS
ok  	_/tmp/d20151110-19113-1wmpxvq	0.004s
PASS
ok  	_/tmp/d20151110-19113-1wmpxvq	0.008s

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

Ангел обнови решението на 09.11.2015 22:55 (преди над 2 години)

+package main
+
+import "fmt"
+
+func bufferLog(log chan string, logIndex int) chan string {
+ bufferedLog := make(chan string, 1024)
+ go func(bufferedLog chan string, log chan string) {
+ for message := range log {
+ bufferedLog <- fmt.Sprintf("%d\t%s", logIndex, message)
+ }
+ close(bufferedLog)
+ }(bufferedLog, log)
+ return bufferedLog
+}
+
+func bufferLogs(logs chan (chan string)) chan (chan string) {
+ bufferedLogs := make(chan (chan string), 1024)
+ go func(bufferedLogs chan (chan string), logs chan (chan string)) {
+ var logIndex int
+ for log := range logs {
+ logIndex++
+ bufferedLogs <- bufferLog(log, logIndex)
+ }
+ close(bufferedLogs)
+ }(bufferedLogs, logs)
+ return bufferedLogs
+}
+
+func actualLogDrainer(logs chan (chan string), result chan string) {
+ for log := range bufferLogs(logs) {
+ for message := range log {
+ result <- message
+ }
+ }
+ close(result)
+}
+
+func OrderedLogDrainer(logs chan (chan string)) chan string {
+ out := make(chan string)
+ go actualLogDrainer(logs, out)
+ return out
+}