/
goperconn.go
251 lines (230 loc) · 5.99 KB
/
goperconn.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package goperconn
import (
"fmt"
"io"
"net"
"time"
)
// DefaultJobQueueSize specifies the size of the job queue created to support IO
// operations on the Conn.
const DefaultJobQueueSize = 10
// DefaultRetryMin is the default minimum amount of time the client will wait to
// reconnect to a remote host if the connection drops.
const DefaultRetryMin = time.Second
// DefaultRetryMax is the default maximum amount of time the client will wait to
// reconnect to a remote host if the connection drops.
const DefaultRetryMax = time.Minute
// Configurator is a function that modifies a Conn structure during
// initialization.
type Configurator func(*Conn) error
// Address changes the network address used by a .
func Address(address string) Configurator {
return func(c *Conn) error {
c.address = address
return nil
}
}
// DialTimeout specifies the timeout to use when establishing the connection to
// the remote host.
func DialTimeout(duration time.Duration) Configurator {
return func(c *Conn) error {
c.dialTimeout = duration
return nil
}
}
// Printer is the interface implemented by an object that allows printing of
// arbitrary information. It is provided to allow goperconn to accept and use
// any logging library that implements this interface.
type Printer interface {
Print(...interface{})
}
// Logger specifies an optional logger to invoke to log warning messages.
func Logger(printer Printer) Configurator {
return func(c *Conn) error {
c.printer = printer
return nil
}
}
// RetryMin controls the minimum amount of time a Conn will wait between
// connection attempts to the remote host.
func RetryMin(duration time.Duration) Configurator {
return func(c *Conn) error {
c.retryMin = duration
return nil
}
}
// RetryMax controls the maximum amount of time a Conn will wait between
// connection attempts to the remote host.
func RetryMax(duration time.Duration) Configurator {
return func(c *Conn) error {
c.retryMax = duration
return nil
}
}
// Conn wraps a net.Conn, providing a pseudo-persistent network connection.
type Conn struct {
net.Conn
address string
dialTimeout time.Duration
jobs chan *rillJob
printer Printer
retryMax time.Duration
retryMin time.Duration
}
// New returns a Conn structure that wraps the net.Conn connection, and attempts
// to provide a pseudo-persistent connection to a remote host.
//
// package main
//
// import (
// "log"
// "os"
// "time"
//
// "github.com/karrick/goperconn"
// )
//
// func main() {
// printer := log.New(os.Stderr, "WARNING: ", 0)
//
// // NOTE: Address is required, but all other parameters have defaults.
// conn, err := goperconn.New(goperconn.Address("echo-server.example.com:7"),
// goperconn.DialTimeout(5*time.Second),
// goperconn.Logger(printer),
// goperconn.RetryMin(time.Second),
// goperconn.RetryMax(30*time.Second))
// if err != nil {
// log.Fatal(err)
// }
//
// // later ...
//
// _, err = conn.Write([]byte("hello, world"))
// if err != nil {
// log.Fatal(err)
// }
//
// buf := make([]byte, 512)
// _, err = conn.Read(buf)
// if err != nil {
// log.Fatal(err)
// }
// }
func New(setters ...Configurator) (*Conn, error) {
client := &Conn{
retryMin: DefaultRetryMin,
retryMax: DefaultRetryMax,
jobs: make(chan *rillJob, DefaultJobQueueSize),
}
for _, setter := range setters {
if err := setter(client); err != nil {
return nil, err
}
}
if client.retryMin == 0 {
return nil, fmt.Errorf("cannot create Conn with retry: %d", client.retryMin)
}
if client.retryMax == 0 {
return nil, fmt.Errorf("cannot create Conn with retry: %d", client.retryMax)
}
if client.retryMax < client.retryMin {
return nil, fmt.Errorf("cannot create Conn with retry max (%d) less than retry min (%d)", client.retryMax, client.retryMin)
}
if client.address == "" {
return nil, fmt.Errorf("cannot create Conn with address: %q", client.address)
}
go func(wrapper *Conn) {
var conn net.Conn
var err error
retry := client.retryMin
for {
if client.dialTimeout == 0 {
conn, err = net.Dial("tcp", client.address)
} else {
conn, err = net.DialTimeout("tcp", client.address, client.dialTimeout)
}
if err != nil {
if client.printer != nil {
client.printer.Print(ErrDialFailure{client.address, err})
}
time.Sleep(retry)
retry *= 2
if retry > client.retryMax {
retry = client.retryMax
}
continue
}
closed, err := wrapper.proxy(conn)
if err != nil && client.printer != nil {
client.printer.Print(err)
}
if closed {
return
}
retry = client.retryMin
time.Sleep(retry)
}
}(client)
return client, nil
}
func (client *Conn) proxy(rwc io.ReadWriteCloser) (bool, error) {
var closed bool
for job := range client.jobs {
if closed {
job.results <- rillResult{err: ErrClosedConnection{}}
continue
}
switch job.op {
case _read:
n, err := rwc.Read(job.data)
job.results <- rillResult{n, err}
if err != nil {
rwc.Close()
return false, err
}
case _write:
n, err := rwc.Write(job.data)
job.results <- rillResult{n, err}
if err != nil {
rwc.Close()
return false, err
}
case _close:
closed = true
err := rwc.Close()
job.results <- rillResult{err: err}
return true, err
}
}
return false, nil
}
// Read reads data from the connection.
func (client *Conn) Read(data []byte) (int, error) {
job := newRillJob(_read, data)
client.jobs <- job
result := <-job.results
if result.err != nil {
result.err = ErrIOError{_read, result.err}
}
return result.n, result.err
}
// Write writes data to the connection.
func (client *Conn) Write(data []byte) (int, error) {
job := newRillJob(_write, data)
client.jobs <- job
result := <-job.results
if result.err != nil {
result.err = ErrIOError{_write, result.err}
}
return result.n, result.err
}
// Close closes the connection.
func (client *Conn) Close() error {
job := newRillJob(_close, nil)
client.jobs <- job
result := <-job.results
if result.err != nil {
result.err = ErrIOError{_close, result.err}
}
return result.err
}