-
Notifications
You must be signed in to change notification settings - Fork 20
/
conn.go
187 lines (148 loc) · 4.06 KB
/
conn.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
package nsq
import (
"bufio"
"net"
"sync"
"time"
"github.com/pkg/errors"
)
const (
DefaultUserAgent = "github.com/segmentio/nsq-go"
DefaultMaxConcurrency = 1
DefaultMaxInFlight = 1
DefaultDialTimeout = 5 * time.Second
DefaultReadTimeout = 1 * time.Minute
DefaultWriteTimeout = 10 * time.Second
DefaultLookupTimeout = 10 * time.Second
DefaultMaxRetryTimeout = 10 * time.Second
DefaultMinRetryTimeout = 10 * time.Millisecond
DefaultDrainTimeout = 10 * time.Second
NoTimeout = time.Duration(0)
)
type Conn struct {
conn net.Conn
rmtx sync.Mutex
rbuf *bufio.Reader
wmtx sync.Mutex
wbuf *bufio.Writer
}
func NewConn(conn net.Conn) *Conn {
const bufSize = 4096
return &Conn{
conn: conn,
rbuf: bufio.NewReaderSize(conn, bufSize),
wbuf: bufio.NewWriterSize(conn, bufSize),
}
}
func Dial(addr string) (c *Conn, err error) {
return DialTimeout(addr, DefaultDialTimeout)
}
func DialTimeout(addr string, timeout time.Duration) (c *Conn, err error) {
var conn net.Conn
var now = time.Now()
var end = now.Add(timeout)
var magic = [...]byte{' ', ' ', 'V', '2'}
if conn, err = net.DialTimeout("tcp", addr, timeout); err != nil {
err = errors.Wrap(err, "dialing tcp://"+addr)
return
}
if err = conn.SetWriteDeadline(end); err != nil {
conn.Close()
err = errors.Wrap(err, "setting write deadline on connection to tcp://"+addr)
return
}
if _, err = conn.Write(magic[:]); err != nil {
conn.Close()
err = errors.Wrap(err, "sending magic number to tcp://"+addr)
return
}
if err = conn.SetDeadline(time.Time{}); err != nil {
conn.Close()
err = errors.Wrap(err, "resetting deadlines on connection to tcp://"+addr)
return
}
c = NewConn(conn)
return
}
func (c *Conn) WriteCommand(cmd Command) (err error) {
c.wmtx.Lock()
if err = cmd.Write(c.wbuf); err == nil {
if err = c.wbuf.Flush(); err != nil {
err = errors.Wrap(err, "flushing "+cmd.Name()+" command to "+c.RemoteAddr().String())
}
}
c.wmtx.Unlock()
return
}
func (c *Conn) WriteFrame(frame Frame) (err error) {
c.wmtx.Lock()
if err = frame.Write(c.wbuf); err == nil {
if err = c.wbuf.Flush(); err != nil {
err = errors.Wrap(err, "flushing "+frame.FrameType().String()+" frame to "+c.RemoteAddr().String())
}
}
c.wmtx.Unlock()
return
}
func (c *Conn) Write(b []byte) (n int, err error) {
c.wmtx.Lock()
if n, err = c.conn.Write(b); err != nil {
err = errors.Wrap(err, "writing raw data to "+c.RemoteAddr().String())
}
c.wmtx.Unlock()
return
}
func (c *Conn) ReadCommand() (cmd Command, err error) {
c.rmtx.Lock()
if cmd, err = ReadCommand(c.rbuf); err != nil {
err = errors.WithMessage(err, "reading command from "+c.RemoteAddr().String())
}
c.rmtx.Unlock()
return
}
func (c *Conn) ReadFrame() (frame Frame, err error) {
c.rmtx.Lock()
if frame, err = ReadFrame(c.rbuf); err != nil {
err = errors.Wrap(err, "reading frame from "+c.RemoteAddr().String())
}
c.rmtx.Unlock()
return
}
func (c *Conn) Read(b []byte) (n int, err error) {
c.rmtx.Lock()
if n, err = c.conn.Read(b); err != nil {
err = errors.Wrap(err, "reading raw data from "+c.RemoteAddr().String())
}
c.rmtx.Unlock()
return
}
func (c *Conn) Close() (err error) {
if err = c.conn.Close(); err != nil {
err = errors.Wrap(err, "closing tcp connection to "+c.RemoteAddr().String())
}
return
}
func (c *Conn) SetReadDeadline(t time.Time) (err error) {
if err = c.conn.SetReadDeadline(t); err != nil {
err = errors.Wrap(err, "setting read deadline on tcp connection to "+c.RemoteAddr().String())
}
return
}
func (c *Conn) SetWriteDeadline(t time.Time) (err error) {
if err = c.conn.SetWriteDeadline(t); err != nil {
err = errors.Wrap(err, "setting write deadline on tcp connection to "+c.RemoteAddr().String())
}
return
}
func (c *Conn) SetDeadline(t time.Time) (err error) {
if err = c.conn.SetDeadline(t); err != nil {
err = errors.Wrap(err, "setting read/write deadline on tcp connection to "+c.RemoteAddr().String())
}
return
}
func (c *Conn) LocalAddr() net.Addr {
return c.conn.LocalAddr()
}
func (c *Conn) RemoteAddr() net.Addr {
return c.conn.RemoteAddr()
}