-
Notifications
You must be signed in to change notification settings - Fork 0
/
netconn.go
218 lines (175 loc) · 4.07 KB
/
netconn.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
package netconn
import (
"bufio"
"io"
"net"
"time"
)
// FlushWriter ...
type FlushWriter interface {
io.Writer
Flush() error
}
// NetConn ...
type NetConn struct {
Writer FlushWriter
Reader io.Reader
Closer io.Closer
}
type connTimeout interface {
// SetReadDeadline sets the deadline for future Read calls
// and any currently-blocked Read call.
// A zero value for t means Read will not time out.
SetReadDeadline(t time.Time) error
// SetWriteDeadline sets the deadline for future Write calls
// and any currently-blocked Write call.
// Even if write times out, it may return n > 0, indicating that
// some data was successfully written.
// A zero value for t means Write will not time out.
SetWriteDeadline(t time.Time) error
}
type timeoutWriter struct {
writer io.Writer
timeout connTimeout
writeTimeout time.Duration
}
func (w *timeoutWriter) Write(p []byte) (n int, err error) {
err = w.timeout.SetWriteDeadline(time.Now().Add(w.writeTimeout))
if err != nil {
return 0, err
}
return w.writer.Write(p)
}
type timeoutReader struct {
io.ReadCloser
timeout connTimeout
readTimeout time.Duration
}
func (r *timeoutReader) Read(p []byte) (n int, err error) {
err = r.timeout.SetReadDeadline(time.Now().Add(r.readTimeout))
if err != nil {
return 0, err
}
return r.ReadCloser.Read(p)
}
// DialFunc ...
type DialFunc = func(network, address string, timeout time.Duration) (net.Conn, error)
type config struct {
dialFunc DialFunc
tcpKeepAliveDuration time.Duration
connectTimeout time.Duration
writeTimeout time.Duration
readTimeout time.Duration
bufferSize int
}
func computeConfig(options ...Option) config {
conf := config{
dialFunc: net.DialTimeout,
tcpKeepAliveDuration: 30 * time.Second,
connectTimeout: 10 * time.Second,
readTimeout: 1 * time.Second,
writeTimeout: 1 * time.Second,
bufferSize: 16 * 1024,
}
for _, fn := range options {
fn(&conf)
}
return conf
}
// DialNewConn ...
func DialNewConn(addr string, options ...Option) (NetConn, error) {
conf := computeConfig(options...)
nc, err := conf.dialFunc("tcp", addr, conf.connectTimeout)
if err != nil {
return NetConn{}, err
}
tcpNetConn, ok := nc.(*net.TCPConn)
if ok {
if err := tcpNetConn.SetKeepAlive(true); err != nil {
return NetConn{}, err
}
if err := tcpNetConn.SetKeepAlivePeriod(conf.tcpKeepAliveDuration); err != nil {
return NetConn{}, err
}
}
connWriter := &timeoutWriter{
writer: nc,
timeout: nc,
writeTimeout: conf.writeTimeout,
}
writer := bufio.NewWriterSize(connWriter, conf.bufferSize)
var reader io.ReadCloser = &timeoutReader{
ReadCloser: nc,
timeout: nc,
readTimeout: conf.readTimeout,
}
return NetConn{
Writer: writer,
Closer: reader,
Reader: reader,
}, nil
}
type noopFlusher struct {
io.Writer
}
func (f noopFlusher) Flush() error {
return nil
}
// NoopFlusher ...
func NoopFlusher(w io.Writer) FlushWriter {
return noopFlusher{Writer: w}
}
// Error Connection
type errorConn struct {
err error
}
func (w *errorConn) Write([]byte) (n int, err error) {
return 0, w.err
}
func (w *errorConn) Read([]byte) (n int, err error) {
return 0, w.err
}
// ErrorNetConn ...
func ErrorNetConn(err error) NetConn {
conn := &errorConn{
err: err,
}
reader := io.NopCloser(conn)
return NetConn{
Writer: NoopFlusher(conn),
Closer: reader,
Reader: reader,
}
}
// Option ...
type Option func(conf *config)
// WithBufferSize ...
func WithBufferSize(size int) Option {
return func(conf *config) {
conf.bufferSize = size
}
}
// WithTCPKeepAliveDuration sets the tcp keep alive duration
func WithTCPKeepAliveDuration(d time.Duration) Option {
return func(conf *config) {
conf.tcpKeepAliveDuration = d
}
}
// WithDialFunc ...
func WithDialFunc(dialFunc DialFunc) Option {
return func(conf *config) {
conf.dialFunc = dialFunc
}
}
// WithReadTimeout ...
func WithReadTimeout(d time.Duration) Option {
return func(conf *config) {
conf.readTimeout = d
}
}
// WithWriteTimeout ...
func WithWriteTimeout(d time.Duration) Option {
return func(conf *config) {
conf.writeTimeout = d
}
}