Решение на Concurrent Crawling от Любомир Коев

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

Към профила на Любомир Коев

Резултати

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

Код

package main
import "errors"
import "net/http"
import "io/ioutil"
import "time"
func SeekAndDestroy(callback func(string) bool, chunkedUrlsToCheck chan []string, workersCount int) (target string, err error) {
if workersCount < 1 {
return "", errors.New("Invalid workers count")
}
if chunkedUrlsToCheck == nil {
return "", errors.New("Uninitialized chanel")
}
items := make(chan string, 1024)
done := make(chan struct{})
for c := 0; c < workersCount; c++ {
go func() {
for {
select {
case url := <-items:
res := http.Client{Timeout: time.Duration(3 * time.Second)}
if result, err := res.Get(url); err == nil {
content, _ := ioutil.ReadAll(result.Body)
result.Body.Close()
if callback(string(content)) {
target = url
for s := 0; s < workersCount; s++ {
done <- struct{}{}
}
}
}
case <-done:
return
}
}
}()
}
for {
select {
case <-done:
return target, nil
case chunk, ok := <-chunkedUrlsToCheck:
if !ok {
return "", errors.New("Channel closed")
}
for _, url := range chunk {
items <- url
}
case <-time.After(15 * time.Second):
return "", errors.New("Timeout")
}
}
}

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

# _/tmp/d20150111-16649-1ynubd2
./solution_test.go:147: cannot use urls (type <-chan []string) as type chan []string in argument to SeekAndDestroy
FAIL	_/tmp/d20150111-16649-1ynubd2 [build failed]
# _/tmp/d20150111-16649-1ynubd2
./solution_test.go:147: cannot use urls (type <-chan []string) as type chan []string in argument to SeekAndDestroy
FAIL	_/tmp/d20150111-16649-1ynubd2 [build failed]
# _/tmp/d20150111-16649-1ynubd2
./solution_test.go:147: cannot use urls (type <-chan []string) as type chan []string in argument to SeekAndDestroy
FAIL	_/tmp/d20150111-16649-1ynubd2 [build failed]
# _/tmp/d20150111-16649-1ynubd2
./solution_test.go:147: cannot use urls (type <-chan []string) as type chan []string in argument to SeekAndDestroy
FAIL	_/tmp/d20150111-16649-1ynubd2 [build failed]
# _/tmp/d20150111-16649-1ynubd2
./solution_test.go:147: cannot use urls (type <-chan []string) as type chan []string in argument to SeekAndDestroy
FAIL	_/tmp/d20150111-16649-1ynubd2 [build failed]
# _/tmp/d20150111-16649-1ynubd2
./solution_test.go:147: cannot use urls (type <-chan []string) as type chan []string in argument to SeekAndDestroy
FAIL	_/tmp/d20150111-16649-1ynubd2 [build failed]
# _/tmp/d20150111-16649-1ynubd2
./solution_test.go:147: cannot use urls (type <-chan []string) as type chan []string in argument to SeekAndDestroy
FAIL	_/tmp/d20150111-16649-1ynubd2 [build failed]
# _/tmp/d20150111-16649-1ynubd2
./solution_test.go:147: cannot use urls (type <-chan []string) as type chan []string in argument to SeekAndDestroy
FAIL	_/tmp/d20150111-16649-1ynubd2 [build failed]
# _/tmp/d20150111-16649-1ynubd2
./solution_test.go:147: cannot use urls (type <-chan []string) as type chan []string in argument to SeekAndDestroy
FAIL	_/tmp/d20150111-16649-1ynubd2 [build failed]
# _/tmp/d20150111-16649-1ynubd2
./solution_test.go:147: cannot use urls (type <-chan []string) as type chan []string in argument to SeekAndDestroy
FAIL	_/tmp/d20150111-16649-1ynubd2 [build failed]
# _/tmp/d20150111-16649-1ynubd2
./solution_test.go:147: cannot use urls (type <-chan []string) as type chan []string in argument to SeekAndDestroy
FAIL	_/tmp/d20150111-16649-1ynubd2 [build failed]

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

Любомир обнови решението на 11.12.2014 16:48 (преди над 3 години)

+package main
+
+import "errors"
+import "net/http"
+import "io/ioutil"
+import "time"
+
+func SeekAndDestroy(callback func(string) bool, chunkedUrlsToCheck chan []string, workersCount int) (target string, err error) {
+ if workersCount < 1 {
+ return "", errors.New("Invalid workers count")
+ }
+ if chunkedUrlsToCheck == nil {
+ return "", errors.New("Uninitialized chanel")
+ }
+
+ items := make(chan string)
+ done := make(chan struct{})
+
+ for c := 0; c < workersCount; c++ {
+ go func() {
+ for {
+ select {
+ case url := <-items:
+ res := http.Client{Timeout: time.Duration(3 * time.Second)}
+ if result, err := res.Get(url); err == nil {
+ content, _ := ioutil.ReadAll(result.Body)
+ result.Body.Close()
+ if callback(string(content)) {
+ target = url
+ for s := 0; s < workersCount; s++ {
+ done <- struct{}{}
+ }
+ }
+ }
+ case <-done:
+ return
+ }
+ }
+ }()
+ }
+
+ for {
+ select {
+ case <-done:
+ return
+ case chunk := <-chunkedUrlsToCheck:
+ for _, url := range chunk {
+ items <- url
+ }
+ case <-time.After(15 * time.Second):
+ return "", errors.New("Timeout")
+ }
+ }
+
+}

Любомир обнови решението на 11.12.2014 16:55 (преди над 3 години)

package main
import "errors"
import "net/http"
import "io/ioutil"
import "time"
func SeekAndDestroy(callback func(string) bool, chunkedUrlsToCheck chan []string, workersCount int) (target string, err error) {
if workersCount < 1 {
return "", errors.New("Invalid workers count")
}
if chunkedUrlsToCheck == nil {
return "", errors.New("Uninitialized chanel")
}
- items := make(chan string)
+ items := make(chan string, 1024)
done := make(chan struct{})
for c := 0; c < workersCount; c++ {
go func() {
for {
select {
case url := <-items:
res := http.Client{Timeout: time.Duration(3 * time.Second)}
if result, err := res.Get(url); err == nil {
content, _ := ioutil.ReadAll(result.Body)
result.Body.Close()
if callback(string(content)) {
target = url
for s := 0; s < workersCount; s++ {
done <- struct{}{}
}
}
}
case <-done:
return
}
}
}()
}
for {
select {
case <-done:
- return
- case chunk := <-chunkedUrlsToCheck:
+ return target, nil
+ case chunk, ok := <-chunkedUrlsToCheck:
+ if !ok {
+ return "", errors.New("Channel closed")
+ }
for _, url := range chunk {
items <- url
}
case <-time.After(15 * time.Second):
return "", errors.New("Timeout")
}
}
-}
+}