55 "math/rand"
66
77 "github.com/fsnotify/fsnotify"
8+ "github.com/kitabisa/mubeng/common/errors"
89 "github.com/kitabisa/mubeng/pkg/helper"
910)
1011
@@ -16,20 +17,36 @@ func (p *ProxyManager) Count() int {
1617}
1718
1819// NextProxy will navigate the next proxy to use
19- func (p * ProxyManager ) NextProxy () string {
20+ func (p * ProxyManager ) NextProxy () (string , error ) {
21+ var proxy string
22+
23+ count := p .Count ()
24+ if count <= 0 {
25+ return proxy , errors .ErrNoProxyLeft
26+ }
27+
2028 p .CurrentIndex ++
21- if p .CurrentIndex > p . Count () - 1 {
29+ if p .CurrentIndex > count - 1 {
2230 p .CurrentIndex = 0
2331 }
2432
25- proxy : = p .Proxies [p .CurrentIndex ]
33+ proxy = p .Proxies [p .CurrentIndex ]
2634
27- return proxy
35+ return proxy , nil
2836}
2937
3038// RandomProxy will choose a proxy randomly from the list
31- func (p * ProxyManager ) RandomProxy () string {
32- return p .Proxies [rand .Intn (p .Count ())]
39+ func (p * ProxyManager ) RandomProxy () (string , error ) {
40+ var proxy string
41+
42+ count := p .Count ()
43+ if count <= 0 {
44+ return proxy , errors .ErrNoProxyLeft
45+ }
46+
47+ proxy = p .Proxies [rand .Intn (count )]
48+
49+ return proxy , nil
3350}
3451
3552// RemoveProxy removes target proxy from proxy pool
@@ -48,21 +65,22 @@ func (p *ProxyManager) RemoveProxy(target string) error {
4865// Rotate proxy based on method
4966//
5067// Valid methods are "sequent" and "random", default return empty string.
51- func (p * ProxyManager ) Rotate (method string ) string {
68+ func (p * ProxyManager ) Rotate (method string ) ( string , error ) {
5269 var proxy string
70+ var err error
5371
5472 switch method {
5573 case "sequent" :
56- proxy = p .NextProxy ()
74+ proxy , err = p .NextProxy ()
5775 case "random" :
58- proxy = p .RandomProxy ()
76+ proxy , err = p .RandomProxy ()
5977 }
6078
6179 if proxy != "" {
6280 proxy = helper .EvalFunc (proxy )
6381 }
6482
65- return proxy
83+ return proxy , err
6684}
6785
6886// Watch proxy file from events
0 commit comments