MCPcopy Create free account
hub / github.com/PuerkitoBio/fetchbot / processChan

Method processChan

fetch.go:351–436  ·  view source on GitHub ↗

Goroutine for a host's worker, processing requests for all its URLs.

(ch <-chan Command, hostKey string)

Source from the content-addressed store, hash-verified

349
350// Goroutine for a host's worker, processing requests for all its URLs.
351func (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
359loop:
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 }

Callers 1

processQueueMethod · 0.95

Calls 6

getRobotAgentMethod · 0.95
doRequestMethod · 0.95
visitMethod · 0.95
CloseMethod · 0.80
DoneMethod · 0.80
URLMethod · 0.65

Tested by

no test coverage detected