1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- // Proxy代理池时间轮盘切换
- package main
- import (
- "errors"
- "fmt"
- "sync"
- "sync/atomic"
- "time"
- )
- type (
- //IpItem
- IpItem struct {
- Ip string `json:"ip"`
- LifeTime int64 `json:"lifetime"`
- Ports []string `json:"ports"`
- IpType int `json:"iptype"` //proxy 服务器类型
- Raw string //ip:port
- Index int
- }
- //
- Roulette struct {
- Data []*IpItem
- Length int32 //数据长度
- Cursor int32
- lock *sync.RWMutex
- }
- )
- var (
- roulette = NewRoulette()
- )
- // NewRoulette
- func NewRoulette() *Roulette {
- r := Roulette{lock: new(sync.RWMutex)}
- return &r
- }
- // Init
- func (r *Roulette) Init(data []*IpItem) {
- r.lock.Lock()
- defer r.lock.Unlock()
- r.Data = data
- r.Length = int32(len(data))
- atomic.StoreInt32(&r.Cursor, 0)
- }
- func (r *Roulette) findOne() *IpItem {
- var pos int = 0
- if atomic.CompareAndSwapInt32(&r.Cursor, r.Length-1, 0) {
- pos = 0
- } else {
- pos = int(atomic.AddInt32(&r.Cursor, 1))
- }
- return r.Data[pos]
- }
- // 从时间轮中获取一个对象,不要近2分钟要过期的代理
- func (r *Roulette) Get() (*IpItem, error) {
- for j := 0; j < 10000000; j++ {
- for i := 0; i < int(r.Length); i++ {
- if ip := r.findOne(); ip.LifeTime >= time.Now().Unix()+2*60 {
- return ip, nil
- } else {
- fmt.Print("O")
- }
- }
- fmt.Print("W")
- time.Sleep(20 * time.Second)
- }
- return nil, errors.New("找不到合适的代理")
- }
|