File size: 900 Bytes
6bc074c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package proxy

import (
	"log"
	"sync"
)

// Pool 管理代理 IP 的分配与回收。IPv4 从代理列表 round-robin。
type Pool struct {
	mu       sync.Mutex
	cursor   int
	ipv4List []string
}

func NewPool(ipv4Proxies []string) *Pool {
	if ipv4Proxies == nil {
		ipv4Proxies = []string{}
	}
	return &Pool{
		ipv4List: ipv4Proxies,
	}
}

// Allocate 返回一个代理 IP,round-robin 轮询。
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
}

// Release 回收一个代理 IP。
func (p *Pool) Release(ip string) {
	p.mu.Lock()
	defer p.mu.Unlock()
	if ip != "" {
		log.Printf("[proxy] released %s", ip)
	}
}

// Count 返回可用 IPv4 代理数量。
func (p *Pool) Count() int {
	p.mu.Lock()
	defer p.mu.Unlock()
	return len(p.ipv4List)
}