File tree Expand file tree Collapse file tree 1 file changed +7
-3
lines changed
Expand file tree Collapse file tree 1 file changed +7
-3
lines changed Original file line number Diff line number Diff 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
2626type roundRobinSwitcher struct {
2727 proxyURLs []* url.URL
2828 index uint32
29+ mutex sync.Mutex
2930}
3031
3132func (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}
You can’t perform that action at this time.
0 commit comments