Skip to content

Commit 1e07255

Browse files
authored
Update proxy.go
1 parent 473feba commit 1e07255

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

proxy/proxy.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,22 @@ import (
1818
"context"
1919
"net/http"
2020
"net/url"
21-
"sync/atomic"
21+
"sync"
2222

2323
"github.com/gocolly/colly/v2"
2424
)
2525

2626
type roundRobinSwitcher struct {
2727
proxyURLs []*url.URL
2828
index uint32
29+
mutex sync.Mutex
2930
}
3031

3132
func (r *roundRobinSwitcher) GetProxy(pr *http.Request) (*url.URL, error) {
33+
r.mutex.Lock()
3234
u := r.proxyURLs[r.index%uint32(len(r.proxyURLs))]
33-
atomic.AddUint32(&r.index, 1)
35+
r.index = r.index + 1
36+
r.mutex.Unlock()
3437
ctx := context.WithValue(pr.Context(), colly.ProxyURLKey, u.String())
3538
*pr = *pr.WithContext(ctx)
3639
return u, nil
@@ -53,5 +56,6 @@ func RoundRobinProxySwitcher(ProxyURLs ...string) (colly.ProxyFunc, error) {
5356
}
5457
urls[i] = parsedU
5558
}
56-
return (&roundRobinSwitcher{urls, 0}).GetProxy, nil
59+
var mutex sync.Mutex
60+
return (&roundRobinSwitcher{urls, 0, mutex}).GetProxy, nil
5761
}

0 commit comments

Comments
 (0)