Goroutine for a host's worker, processing requests for all its URLs.
(ch <-chan Command, hostKey string)
| 349 | |
| 350 | // Goroutine for a host's worker, processing requests for all its URLs. |
| 351 | func (f *Fetcher) processChan(ch <-chan Command, hostKey string) { |
| 352 | var ( |
| 353 | agent *robotstxt.Group |
| 354 | wait <-chan time.Time |
| 355 | ttl <-chan time.Time |
| 356 | delay = f.CrawlDelay |
| 357 | ) |
| 358 | |
| 359 | loop: |
| 360 | for { |
| 361 | select { |
| 362 | case <-f.q.cancelled: |
| 363 | break loop |
| 364 | case v, ok := <-ch: |
| 365 | if !ok { |
| 366 | // Terminate this goroutine, channel is closed |
| 367 | break loop |
| 368 | } |
| 369 | |
| 370 | // Wait for the prescribed delay |
| 371 | if wait != nil { |
| 372 | <-wait |
| 373 | } |
| 374 | |
| 375 | // was it cancelled during the wait? check again |
| 376 | select { |
| 377 | case <-f.q.cancelled: |
| 378 | break loop |
| 379 | default: |
| 380 | // go on |
| 381 | } |
| 382 | |
| 383 | switch r, ok := v.(robotCommand); { |
| 384 | case ok: |
| 385 | // This is the robots.txt request |
| 386 | agent = f.getRobotAgent(r) |
| 387 | // Initialize the crawl delay |
| 388 | if agent != nil && agent.CrawlDelay > 0 { |
| 389 | delay = agent.CrawlDelay |
| 390 | } |
| 391 | wait = time.After(delay) |
| 392 | |
| 393 | case agent == nil || agent.Test(v.URL().Path): |
| 394 | // Path allowed, process the request |
| 395 | res, err := f.doRequest(v) |
| 396 | f.visit(v, res, err) |
| 397 | // No delay on error - the remote host was not reached |
| 398 | if err == nil { |
| 399 | wait = time.After(delay) |
| 400 | } else { |
| 401 | wait = nil |
| 402 | } |
| 403 | |
| 404 | default: |
| 405 | // Path disallowed by robots.txt |
| 406 | f.visit(v, nil, ErrDisallowed) |
| 407 | wait = nil |
| 408 | } |
no test coverage detected