/
pool.go
117 lines (97 loc) · 2.18 KB
/
pool.go
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* Revision History:
* Initial: 2018/07/18 Li Zebang
*/
package github
import (
"log"
"sync"
)
// Pool provides Get and Put function.
type Pool interface {
Get(string) *Client
Put(*Client)
}
type pool struct {
locker *sync.Mutex
circles map[string]*circle
}
type circle struct {
clients []*Client
head int
rear int
}
func (c *circle) get() *Client {
if c.head == c.rear {
return nil
}
client := c.clients[c.head]
c.clients[c.head] = nil
c.head = (c.head + 1) % len(c.clients)
return client
}
func (c *circle) put(client *Client) {
if (c.rear+1)%len(c.clients) == c.head {
return
}
c.clients[c.rear] = client
c.rear = (c.rear + 1) % len(c.clients)
}
// NewPool returns a GitHub Client Pool. It returns nil if there is no available Token.
func NewPool(tokens ...*Token) Pool {
if len(tokens) == 0 {
return nil
}
ts := make(map[string][]*Token, 0)
for index := range tokens {
err := IsTokenValid(tokens[index])
if err != nil {
log.Printf("%s: index %d -- %v", err, index, tokens[index])
continue
}
if tokens[index].Tag == "" {
tokens[index].Tag = DefualtClientTag
}
if _, exist := ts[tokens[index].Tag]; !exist {
ts[tokens[index].Tag] = make([]*Token, 0, 1)
ts[tokens[index].Tag] = append(ts[tokens[index].Tag], tokens[index])
} else {
ts[tokens[index].Tag] = append(ts[tokens[index].Tag], tokens[index])
}
}
if len(ts) == 0 {
return nil
}
p := &pool{
locker: &sync.Mutex{},
circles: make(map[string]*circle, len(ts)),
}
for k, t := range ts {
p.circles[k] = &circle{clients: make([]*Client, len(t)+1)}
for _, token := range t {
client, _ := NewClient(token)
p.circles[token.Tag].put(client)
}
}
return p
}
// Get a Client from Pool, it will return nil if there is no available Client.
func (p *pool) Get(tag string) *Client {
p.locker.Lock()
defer p.locker.Unlock()
if tag == "" {
tag = DefualtClientTag
}
if _, exist := p.circles[tag]; !exist {
return nil
}
return p.circles[tag].get()
}
// Put the Client back to the Pool.
func (p *pool) Put(client *Client) {
p.locker.Lock()
defer p.locker.Unlock()
if _, exist := p.circles[client.Tag]; exist {
p.circles[client.Tag].put(client)
}
}