Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Queue Depth functionality + Panic with Async #774

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func (q *Queue) AddURL(URL string) error {
r := &colly.Request{
URL: u2,
Method: "GET",
Depth: 1,
}
d, err := r.Marshal()
if err != nil {
Expand Down Expand Up @@ -131,6 +132,10 @@ func (q *Queue) Size() (int, error) {
// to perform requests. Run blocks while the queue has active requests
// The given Storage must not be used directly while Run blocks.
func (q *Queue) Run(c *colly.Collector) error {
if c.Async {
// Async causes Queue crawling to silently fail, since all requests finish "Instantly"
panic("Cannot run Async collector in Queue!")
}
q.mut.Lock()
if q.wake != nil && q.running == true {
q.mut.Unlock()
Expand Down
54 changes: 54 additions & 0 deletions queue/queue_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package queue

import (
"fmt"
"math/rand"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -75,6 +76,59 @@ func TestQueue(t *testing.T) {
}
}

func TestCollectorDepth(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(serverHandler))
defer server.Close()
maxDepth := 4
c := colly.NewCollector(
colly.MaxDepth(maxDepth),
colly.AllowURLRevisit(),
)

storage := &InMemoryQueueStorage{MaxSize: 100000}
q, err := New(10, storage)
if err != nil {
panic(err)
}

c.OnRequest(func(req *colly.Request) {
if req.Depth > maxDepth {
errMsg := fmt.Sprintf("Invalid depth value! Expected %d, got %d", maxDepth, req.Depth)
panic(errMsg)
}
req.Visit(server.URL)
})

q.AddURL(server.URL)

err = q.Run(c)
if err != nil {
t.Fatalf("Queue.Run() return an error: %v", err)
}
}

func TestAsyncPanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()

c := colly.NewCollector(
colly.MaxDepth(5),
colly.AllowURLRevisit(),
colly.Async(true),
)

storage := &InMemoryQueueStorage{MaxSize: 100000}
q, err := New(10, storage)
if err != nil {
panic(err)
}

q.Run(c)
}

func serverHandler(w http.ResponseWriter, req *http.Request) {
if !serverRoute(w, req) {
shutdown(w)
Expand Down
2 changes: 1 addition & 1 deletion request.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (r *Request) Retry() error {

// Do submits the request
func (r *Request) Do() error {
return r.collector.scrape(r.URL.String(), r.Method, r.Depth, r.Body, r.Ctx, *r.Headers, !r.collector.AllowURLRevisit)
return r.collector.scrape(r.URL.String(), r.Method, r.Depth+1, r.Body, r.Ctx, *r.Headers, !r.collector.AllowURLRevisit)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this change. Could you please clarify? The test you added doesn't catch it if I revert this change.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Odd, the TestCollectorDepth test should catch that, it's so that the depth increases on each request, so that you can use MaxDepth in Queue-based crawlers. I'll take a look at it.

}

// Marshal serializes the Request
Expand Down