Skip to content

Commit

Permalink
Update proxy.go
Browse files Browse the repository at this point in the history
  • Loading branch information
i25959341 authored May 7, 2020
1 parent 1e07255 commit f26c6da
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,20 @@ import (
"context"
"net/http"
"net/url"
"sync"
"sync/atomic"

"github.com/gocolly/colly/v2"
)

type roundRobinSwitcher struct {
proxyURLs []*url.URL
index uint32
mutex sync.Mutex
}

func (r *roundRobinSwitcher) GetProxy(pr *http.Request) (*url.URL, error) {
r.mutex.Lock()
u := r.proxyURLs[r.index%uint32(len(r.proxyURLs))]
r.index = r.index + 1
r.mutex.Unlock()
index := atomic.AddUint32(&r.index, 1) - 1
u := r.proxyURL[index%uint32(len(r.proxyURLs))]

ctx := context.WithValue(pr.Context(), colly.ProxyURLKey, u.String())
*pr = *pr.WithContext(ctx)
return u, nil
Expand All @@ -57,5 +55,5 @@ func RoundRobinProxySwitcher(ProxyURLs ...string) (colly.ProxyFunc, error) {
urls[i] = parsedU
}
var mutex sync.Mutex
return (&roundRobinSwitcher{urls, 0, mutex}).GetProxy, nil
return (&roundRobinSwitcher{urls, 0}).GetProxy, nil
}

0 comments on commit f26c6da

Please sign in to comment.