+
Skip to content
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
16 changes: 8 additions & 8 deletions colly.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ type Collector struct {
requestHeadersCallbacks []RequestCallback
errorCallbacks []ErrorCallback
scrapedCallbacks []ScrapedCallback
requestCount uint32
responseCount uint32
requestCount atomic.Uint32
responseCount atomic.Uint32
backend *httpBackend
wg *sync.WaitGroup
lock *sync.RWMutex
Expand Down Expand Up @@ -623,7 +623,7 @@ func (c *Collector) UnmarshalRequest(r []byte) (*Request, error) {
Depth: req.Depth,
Body: bytes.NewReader(req.Body),
Ctx: ctx,
ID: atomic.AddUint32(&c.requestCount, 1),
ID: c.requestCount.Add(1),
Headers: &req.Headers,
collector: c,
}, nil
Expand Down Expand Up @@ -698,7 +698,7 @@ func (c *Collector) fetch(u, method string, depth int, requestData io.Reader, ct
Method: method,
Body: requestData,
collector: c,
ID: atomic.AddUint32(&c.requestCount, 1),
ID: c.requestCount.Add(1),
}

if req.Header.Get("Accept") == "" {
Expand Down Expand Up @@ -740,7 +740,7 @@ func (c *Collector) fetch(u, method string, depth int, requestData io.Reader, ct
if err := c.handleOnError(response, err, request, ctx); err != nil {
return err
}
atomic.AddUint32(&c.responseCount, 1)
c.responseCount.Add(1)
response.Ctx = ctx
response.Request = request
response.Trace = hTrace
Expand Down Expand Up @@ -772,7 +772,7 @@ func (c *Collector) requestCheck(parsedURL *url.URL, method string, getBody func
if c.MaxDepth > 0 && c.MaxDepth < depth {
return ErrMaxDepth
}
if c.MaxRequests > 0 && c.requestCount >= c.MaxRequests {
if c.MaxRequests > 0 && c.requestCount.Load() >= c.MaxRequests {
return ErrMaxRequests
}
if err := c.checkFilters(u, parsedURL.Hostname()); err != nil {
Expand Down Expand Up @@ -906,8 +906,8 @@ func (c *Collector) checkRobots(u *url.URL) error {
func (c *Collector) String() string {
return fmt.Sprintf(
"Requests made: %d (%d responses) | Callbacks: OnRequest: %d, OnHTML: %d, OnResponse: %d, OnError: %d",
atomic.LoadUint32(&c.requestCount),
atomic.LoadUint32(&c.responseCount),
c.requestCount.Add(1),
Copy link
Member

Choose a reason for hiding this comment

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

There is no need to add anything, we just format the current value.

c.responseCount.Add(1),
len(c.requestCallbacks),
len(c.htmlCallbacks),
len(c.responseCallbacks),
Expand Down
3 changes: 1 addition & 2 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"net/http"
"net/url"
"strings"
"sync/atomic"
)

// Request is the representation of a HTTP request made by a Collector
Expand Down Expand Up @@ -81,7 +80,7 @@ func (r *Request) New(method, URL string, body io.Reader) (*Request, error) {
Ctx: r.Ctx,
Headers: &http.Header{},
Host: r.Host,
ID: atomic.AddUint32(&r.collector.requestCount, 1),
ID: r.collector.requestCount.Add(1),
collector: r.collector,
}, nil
}
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载