| package proxy |
|
|
| import ( |
| "log" |
| "sync" |
| ) |
|
|
| |
| type Pool struct { |
| mu sync.Mutex |
| cursor int |
| ipv4List []string |
| } |
|
|
| func NewPool(ipv4Proxies []string) *Pool { |
| if ipv4Proxies == nil { |
| ipv4Proxies = []string{} |
| } |
| return &Pool{ |
| ipv4List: ipv4Proxies, |
| } |
| } |
|
|
| |
| func (p *Pool) Allocate() string { |
| p.mu.Lock() |
| defer p.mu.Unlock() |
|
|
| if len(p.ipv4List) == 0 { |
| return "" |
| } |
|
|
| ip := p.ipv4List[p.cursor] |
| p.cursor = (p.cursor + 1) % len(p.ipv4List) |
| return ip |
| } |
|
|
| |
| func (p *Pool) Release(ip string) { |
| p.mu.Lock() |
| defer p.mu.Unlock() |
| if ip != "" { |
| log.Printf("[proxy] released %s", ip) |
| } |
| } |
|
|
| |
| func (p *Pool) Count() int { |
| p.mu.Lock() |
| defer p.mu.Unlock() |
| return len(p.ipv4List) |
| } |
|
|