-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
168 lines (132 loc) · 3.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package pool
import (
"fmt"
"net"
"sync"
"time"
)
type TCPPool struct {
index uint
size uint
mu sync.RWMutex
clients []*TCPClient
network string
laddr *net.TCPAddr
raddr *net.TCPAddr
maxRetries int
retryInterval time.Duration
}
// Dial returns a new *TCPPool.
//
// The new client connects to the remote address `raddr` on the network `network`,
// which must be "tcp", "tcp4", or "tcp6".
func Dial(network, addr string) (*TCPPool, error) {
raddr, err := net.ResolveTCPAddr(network, addr)
if err != nil {
return nil, err
}
return DialTCP(network, nil, raddr)
}
// DialTCP returns a new *TCPPool.
//
// The new client connects to the remote address `raddr` on the network `network`,
// which must be "tcp", "tcp4", or "tcp6".
// If `laddr` is not nil, it is used as the local address for the connection.
func DialTCP(network string, laddr, raddr *net.TCPAddr) (*TCPPool, error) {
return &TCPPool{
index: 0,
size: 1,
mu: sync.RWMutex{},
clients: []*TCPClient{
newTCPClient(network, laddr, raddr),
},
network: network,
laddr: laddr,
raddr: raddr,
maxRetries: 1,
retryInterval: 1 * time.Millisecond,
}, nil
}
func (p *TCPPool) SetPoolSize(size uint) error {
p.mu.Lock()
defer p.mu.Unlock()
previousSize := p.size
if size > previousSize {
for i := previousSize; i < size; i++ {
client := newTCPClient(p.network, p.laddr, p.raddr)
client.SetMaxRetries(p.maxRetries)
client.SetRetryInterval(p.retryInterval)
p.clients = append(p.clients, client)
}
} else if size < previousSize {
for i := size; i < previousSize; i++ {
// error is ignored
_ = p.clients[i].Close()
}
p.clients = p.clients[:size]
}
p.size = size
return nil
}
func (p *TCPPool) SetMaxRetries(maxRetries int) {
p.mu.Lock()
defer p.mu.Unlock()
p.maxRetries = maxRetries
for i := range p.clients {
p.clients[i].SetMaxRetries(maxRetries)
}
}
func (p *TCPPool) SetRetryInterval(retryInterval time.Duration) {
p.mu.Lock()
defer p.mu.Unlock()
p.retryInterval = retryInterval
for i := range p.clients {
p.clients[i].SetRetryInterval(retryInterval)
}
}
// implements net.Conn
func (p *TCPPool) Read(b []byte) (int, error) {
return -1, fmt.Errorf("github.com/samber/go-tcp-pool is a write only connection")
}
// implements net.Conn
func (p *TCPPool) Write(b []byte) (int, error) {
p.mu.RLock()
index := p.index % p.size
client := p.clients[index]
p.index++
p.mu.RUnlock()
return client.Write(b)
}
// implements net.Conn
func (p *TCPPool) Close() error {
p.mu.Lock()
defer p.mu.Unlock()
for i := range p.clients {
_ = p.clients[i].Close()
}
p.size = 0
p.clients = []*TCPClient{}
return nil
}
// implements net.Conn
func (p *TCPPool) LocalAddr() net.Addr {
return p.laddr
}
// implements net.Conn
func (p *TCPPool) RemoteAddr() net.Addr {
return p.raddr
}
// implements net.Conn
func (p *TCPPool) SetDeadline(t time.Time) error {
// @TODO: implement
return nil
}
// implements net.Conn
func (p *TCPPool) SetReadDeadline(t time.Time) error {
return fmt.Errorf("github.com/samber/go-tcp-pool is a write only connection")
}
// implements net.Conn
func (p *TCPPool) SetWriteDeadline(t time.Time) error {
// @TODO: implement
return nil
}