Skip to content

Commit 66bbc9a

Browse files
authored
Merge pull request gocolly#862 from cuiweixie/atomic.Uint32
Refactor to use atomic.Uint32
2 parents c0c7a4c + 468c427 commit 66bbc9a

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

colly.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ type Collector struct {
131131
requestHeadersCallbacks []RequestCallback
132132
errorCallbacks []ErrorCallback
133133
scrapedCallbacks []ScrapedCallback
134-
requestCount uint32
135-
responseCount uint32
134+
requestCount atomic.Uint32
135+
responseCount atomic.Uint32
136136
backend *httpBackend
137137
wg *sync.WaitGroup
138138
lock *sync.RWMutex
@@ -623,7 +623,7 @@ func (c *Collector) UnmarshalRequest(r []byte) (*Request, error) {
623623
Depth: req.Depth,
624624
Body: bytes.NewReader(req.Body),
625625
Ctx: ctx,
626-
ID: atomic.AddUint32(&c.requestCount, 1),
626+
ID: c.requestCount.Add(1),
627627
Headers: &req.Headers,
628628
collector: c,
629629
}, nil
@@ -698,7 +698,7 @@ func (c *Collector) fetch(u, method string, depth int, requestData io.Reader, ct
698698
Method: method,
699699
Body: requestData,
700700
collector: c,
701-
ID: atomic.AddUint32(&c.requestCount, 1),
701+
ID: c.requestCount.Add(1),
702702
}
703703

704704
if req.Header.Get("Accept") == "" {
@@ -740,7 +740,7 @@ func (c *Collector) fetch(u, method string, depth int, requestData io.Reader, ct
740740
if err := c.handleOnError(response, err, request, ctx); err != nil {
741741
return err
742742
}
743-
atomic.AddUint32(&c.responseCount, 1)
743+
c.responseCount.Add(1)
744744
response.Ctx = ctx
745745
response.Request = request
746746
response.Trace = hTrace
@@ -772,7 +772,7 @@ func (c *Collector) requestCheck(parsedURL *url.URL, method string, getBody func
772772
if c.MaxDepth > 0 && c.MaxDepth < depth {
773773
return ErrMaxDepth
774774
}
775-
if c.MaxRequests > 0 && c.requestCount >= c.MaxRequests {
775+
if c.MaxRequests > 0 && c.requestCount.Load() >= c.MaxRequests {
776776
return ErrMaxRequests
777777
}
778778
if err := c.checkFilters(u, parsedURL.Hostname()); err != nil {
@@ -906,8 +906,8 @@ func (c *Collector) checkRobots(u *url.URL) error {
906906
func (c *Collector) String() string {
907907
return fmt.Sprintf(
908908
"Requests made: %d (%d responses) | Callbacks: OnRequest: %d, OnHTML: %d, OnResponse: %d, OnError: %d",
909-
atomic.LoadUint32(&c.requestCount),
910-
atomic.LoadUint32(&c.responseCount),
909+
c.requestCount.Load(),
910+
c.responseCount.Load(),
911911
len(c.requestCallbacks),
912912
len(c.htmlCallbacks),
913913
len(c.responseCallbacks),

request.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"net/http"
2222
"net/url"
2323
"strings"
24-
"sync/atomic"
2524
)
2625

2726
// Request is the representation of a HTTP request made by a Collector
@@ -81,7 +80,7 @@ func (r *Request) New(method, URL string, body io.Reader) (*Request, error) {
8180
Ctx: r.Ctx,
8281
Headers: &http.Header{},
8382
Host: r.Host,
84-
ID: atomic.AddUint32(&r.collector.requestCount, 1),
83+
ID: r.collector.requestCount.Add(1),
8584
collector: r.collector,
8685
}, nil
8786
}

0 commit comments

Comments
 (0)