mirror of
https://github.com/miniflux/v2.git
synced 2025-08-06 17:41:00 +00:00
refactor(proxyrotator): simplify mutex handling
There is no need to use a mutex to check the length of the proxies list, as it's read-only during the whole lifetime of a ProxyRotator structure. Moreover, it's a bit clearer to explicitly wrap the 2 lines of mutex-needing operations between a Lock/Unlock instead of using a defer.
This commit is contained in:
parent
f864a2ed70
commit
a09129d220
1 changed files with 2 additions and 6 deletions
|
@ -38,23 +38,19 @@ func NewProxyRotator(proxyURLs []string) (*ProxyRotator, error) {
|
||||||
|
|
||||||
// GetNextProxy returns the next proxy in the rotation.
|
// GetNextProxy returns the next proxy in the rotation.
|
||||||
func (pr *ProxyRotator) GetNextProxy() *url.URL {
|
func (pr *ProxyRotator) GetNextProxy() *url.URL {
|
||||||
pr.mutex.Lock()
|
|
||||||
defer pr.mutex.Unlock()
|
|
||||||
|
|
||||||
if len(pr.proxies) == 0 {
|
if len(pr.proxies) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pr.mutex.Lock()
|
||||||
proxy := pr.proxies[pr.currentIndex]
|
proxy := pr.proxies[pr.currentIndex]
|
||||||
pr.currentIndex = (pr.currentIndex + 1) % len(pr.proxies)
|
pr.currentIndex = (pr.currentIndex + 1) % len(pr.proxies)
|
||||||
|
pr.mutex.Unlock()
|
||||||
|
|
||||||
return proxy
|
return proxy
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasProxies checks if there are any proxies available in the rotator.
|
// HasProxies checks if there are any proxies available in the rotator.
|
||||||
func (pr *ProxyRotator) HasProxies() bool {
|
func (pr *ProxyRotator) HasProxies() bool {
|
||||||
pr.mutex.Lock()
|
|
||||||
defer pr.mutex.Unlock()
|
|
||||||
|
|
||||||
return len(pr.proxies) > 0
|
return len(pr.proxies) > 0
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue