watcher/pkg/pools/proxy.go

82 lines
1.7 KiB
Go
Raw Normal View History

2024-04-10 17:36:56 +08:00
package pools
import (
"context"
"fmt"
"github.com/golang/glog"
"github.com/timerzz/proxypool/pkg/getter"
"github.com/timerzz/proxypool/pkg/proxy"
"github.com/timerzz/proxypool/pkg/tool"
"log/slog"
"math/rand"
"sync"
"time"
)
type ProxyPool struct {
m sync.Mutex
2024-04-12 15:29:43 +08:00
proxies proxy.ProxyList
2024-04-10 17:36:56 +08:00
subscribes []string //订阅url
2024-04-12 15:29:43 +08:00
updated time.Time
2024-04-10 17:36:56 +08:00
}
func NewProxyPool(subscribes []string) *ProxyPool {
var p = &ProxyPool{}
p.subscribes = subscribes
p.Update()
return p
}
2024-04-12 15:29:43 +08:00
func (p *ProxyPool) Status() (proxy.ProxyList, time.Time) {
return p.proxies, p.updated
}
2024-04-10 17:36:56 +08:00
// Update 更新代理池
func (p *ProxyPool) Update() {
2024-04-12 15:29:43 +08:00
var list = make(proxy.ProxyList, 0, len(p.proxies))
2024-04-10 17:36:56 +08:00
for _, url := range p.subscribes {
subscribeGetter, err := getter.NewSubscribe(tool.Options{"url": url})
if err != nil {
slog.Warn(fmt.Sprintf("创建Subscribe Getter失败%v", err))
continue
}
list = list.UniqAppendProxyList(subscribeGetter.Get())
}
glog.Infof("代理源共 %d 个: %v", len(p.subscribes), p.subscribes)
glog.Infof("获取代理共 %d 个", len(list))
p.m.Lock()
2024-04-12 15:29:43 +08:00
p.proxies = list
2024-04-10 17:36:56 +08:00
p.m.Unlock()
2024-04-12 15:29:43 +08:00
p.updated = time.Now()
2024-04-10 17:36:56 +08:00
}
// CronUpdate 定时更新
func (p *ProxyPool) CronUpdate(ctx context.Context, interval time.Duration) {
2024-04-12 15:29:43 +08:00
if interval == 0 {
interval = time.Minute * 30
}
2024-04-10 17:36:56 +08:00
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
p.Update()
}
}
}
// RandomIterator 获取随机代理的迭代器
func (p *ProxyPool) RandomIterator() func() proxy.Proxy {
return func() (proxy proxy.Proxy) {
2024-04-12 15:29:43 +08:00
if len(p.proxies) == 0 {
2024-04-10 17:36:56 +08:00
return nil
}
p.m.Lock()
defer p.m.Unlock()
2024-04-12 15:29:43 +08:00
curIndex := rand.Intn(len(p.proxies))
return p.proxies[curIndex]
2024-04-10 17:36:56 +08:00
}
}