Documentation
¶
Index ¶
- Variables
- func BuildHttpClient(dialer *fastdialer.Dialer, options *types.Options, ...) (*retryablehttp.Client, *fastdialer.Dialer, error)
- func DetectBlock(resp *http.Response) string
- func IsThrottled(statusCode int) bool
- func ReportBlock(warned *sync.Map, host, protection string, alreadyHeadless bool)
- func RequestHost(rawURL, fallback string) string
- func RespondingHost(resp *http.Response, requested string) string
- func ResponseHost(resp *navigation.Response, fallback string) string
- type CrawlSession
- type DoRequestFunc
- type RedirectCallback
- type ScopeValidator
- type Shared
- func (s *Shared) AcceptRequest(crawlSession *CrawlSession, item interface{}) (req *navigation.Request, inScope, ok bool)
- func (s *Shared) ApplyBackoff(host string)
- func (s *Shared) CloseIdleConnections()
- func (s *Shared) Do(crawlSession *CrawlSession, doRequest DoRequestFunc) error
- func (s *Shared) DomainCounter(domain string) *atomic.Int64
- func (s *Shared) Enqueue(queue *queue.Queue, navigationRequests ...*navigation.Request)
- func (s *Shared) NewCrawlSessionWithURL(URL string) (*CrawlSession, error)
- func (s *Shared) Output(navigationRequest *navigation.Request, navigationResponse *navigation.Response, ...)
- func (s *Shared) RecordSuccess(host string)
- func (s *Shared) RecordThrottle(host string, statusCode int)
- func (s *Shared) StopForBlock(resp *http.Response, requested string) bool
- func (s *Shared) TakeHostRateLimit(host string)
- func (s *Shared) ValidateScope(URL string, root string) bool
Constants ¶
This section is empty.
Variables ¶
var ErrMaxDepthReached = errors.New("max depth reached")
var ErrOutOfScope = errors.New("out of scope")
Functions ¶
func BuildHttpClient ¶ added in v1.0.0
func BuildHttpClient(dialer *fastdialer.Dialer, options *types.Options, redirectCallback RedirectCallback, scopeValidator ...ScopeValidator) (*retryablehttp.Client, *fastdialer.Dialer, error)
BuildHttpClient builds a http client based on a profile
func DetectBlock ¶ added in v1.9.0
DetectBlock returns the bot protection that rejected resp, or "" if none. These reject the client itself rather than its request rate, so backing off does not help and the response has nothing to crawl.
func IsThrottled ¶ added in v1.6.0
IsThrottled returns true if the status code indicates rate limiting.
func ReportBlock ¶ added in v1.9.0
ReportBlock tells the user once per host that bot protection rejected it. alreadyHeadless suppresses the -headless hint, which only helps a crawl that is not already running a browser.
func RequestHost ¶ added in v1.9.0
RequestHost returns the normalized lowercase hostname for rawURL, falling back to fallback when rawURL is empty or has no hostname.
func RespondingHost ¶ added in v1.9.0
RespondingHost is the host that sent resp, or the host of requested when the response has no request URL. A crawl can visit more than the seed host.
func ResponseHost ¶ added in v1.9.0
func ResponseHost(resp *navigation.Response, fallback string) string
ResponseHost returns the normalized lowercase hostname that sent resp (after any redirects), falling back to fallback when unavailable.
Types ¶
type CrawlSession ¶ added in v1.0.1
type CrawlSession struct {
Ctx context.Context
CancelFunc context.CancelFunc
URL *url.URL
Hostname string
Queue *queue.Queue
HttpClient *retryablehttp.Client
Browser *rod.Browser
}
CrawlSession represents an active crawling session for a specific target URL. It maintains the session context, cancellation function, parsed URL information, the request queue, and HTTP/browser clients needed for the crawl operation.
func (*CrawlSession) Close ¶ added in v1.9.0
func (c *CrawlSession) Close()
Close cancels the crawl session and closes the idle keep-alive connections of its http client, so that a finished crawl does not leave connections open in a long-running process.
type DoRequestFunc ¶ added in v1.0.1
type DoRequestFunc func(crawlSession *CrawlSession, req *navigation.Request) (*navigation.Response, error)
DoRequestFunc is a function type for executing navigation requests. Implementations should perform the actual HTTP request or browser navigation and return the response or an error. This allows different crawling strategies (standard HTTP vs. headless browser) to provide their own request logic.
type RedirectCallback ¶ added in v1.0.0
type ScopeValidator ¶ added in v1.9.0
type Shared ¶ added in v1.0.1
type Shared struct {
// contains filtered or unexported fields
}
func NewShared ¶ added in v1.0.1
func NewShared(options *types.CrawlerOptions) (*Shared, error)
NewShared creates a new Shared instance with the provided crawler options. It initializes the HTTP headers, known files database (if configured), and an empty cookie jar. Returns an error if the HTTP client or cookie jar creation fails.
func (*Shared) AcceptRequest ¶ added in v1.9.0
func (s *Shared) AcceptRequest(crawlSession *CrawlSession, item interface{}) (req *navigation.Request, inScope, ok bool)
AcceptRequest returns the request in a popped queue item when it is a URL that passes the path and scope filters.
func (*Shared) ApplyBackoff ¶ added in v1.6.0
ApplyBackoff sleeps if the host has accumulated throttle signals.
func (*Shared) CloseIdleConnections ¶ added in v1.9.0
func (s *Shared) CloseIdleConnections()
CloseIdleConnections closes the idle keep-alive connections of the http clients owned by the crawler, such as the known files client.
func (*Shared) Do ¶ added in v1.0.1
func (s *Shared) Do(crawlSession *CrawlSession, doRequest DoRequestFunc) error
Do executes the main crawling loop for the given crawl session. It processes items from the queue concurrently (respecting the Concurrency limit), validates each request (URL format, path filters, scope), applies rate limiting and delays, executes the request using the provided doRequest function, writes results to output, and enqueues any newly discovered URLs from responses.
The method returns when the queue is empty or the session context is cancelled (due to timeout or manual cancellation). Returns an error if the context is cancelled.
func (*Shared) DomainCounter ¶ added in v1.6.0
func (*Shared) Enqueue ¶ added in v1.0.1
func (s *Shared) Enqueue(queue *queue.Queue, navigationRequests ...*navigation.Request)
Enqueue adds one or more navigation requests to the crawl queue after applying validation checks. The method performs the following checks in order:
- URL format validation
- Query parameter handling (if IgnoreQueryParams is enabled)
- Depth filtering - skips URLs exceeding MaxDepth before uniqueness check to prevent caching URLs that would be rejected, allowing them to be processed if discovered later at valid depths via different paths
- Uniqueness filtering - prevents duplicate URL crawling
- Cycle detection - identifies URLs stuck in redirect loops
- Scope validation - ensures URLs belong to the allowed crawl scope
For in-scope URLs, the method also handles path climbing when enabled, extracting and enqueuing parent directory paths. Out-of-scope URLs are sent to output if DisplayOutScope is enabled.
func (*Shared) NewCrawlSessionWithURL ¶ added in v1.0.1
func (s *Shared) NewCrawlSessionWithURL(URL string) (*CrawlSession, error)
NewCrawlSessionWithURL creates and initializes a new crawl session for the specified URL. It performs the following initialization steps:
- Creates a context with optional timeout based on CrawlDuration setting
- Parses the target URL and extracts the hostname
- Initializes the request queue with the configured strategy
- Enqueues the initial URL and any known files for the target
- Sets up the HTTP client with response parsing callbacks
Returns the initialized CrawlSession or an error if initialization fails.
func (*Shared) Output ¶ added in v1.0.1
func (s *Shared) Output(navigationRequest *navigation.Request, navigationResponse *navigation.Response, err error)
Output writes a crawl result to the configured output writer. It creates a Result object containing the navigation request, response (if any), and error information (if any), then writes it to the output writer. If an OnResult callback is configured and output writing succeeds, the callback is invoked.
func (*Shared) RecordSuccess ¶ added in v1.6.0
RecordSuccess decrements backoff state on a successful response.
func (*Shared) RecordThrottle ¶ added in v1.6.0
RecordThrottle increments backoff state for a throttled host.
func (*Shared) StopForBlock ¶ added in v1.9.0
StopForBlock reports resp when bot protection rejected it. The caller skips parsing: the body is a challenge page, not something to crawl.
func (*Shared) TakeHostRateLimit ¶ added in v1.9.0
TakeHostRateLimit takes one token for host, serializing initial bucket creation so concurrent first requests to an unregistered host share one AutoLimiter bucket.
func (*Shared) ValidateScope ¶ added in v1.0.1
ValidateScope checks whether a given URL is within the allowed crawling scope based on the configured scope rules and the root hostname. Returns true if the URL passes scope validation, false otherwise.