forked from valyala/gorpc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
transport.go
229 lines (203 loc) · 6.01 KB
/
transport.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
package gorpc
import (
"crypto/tls"
"io"
"net"
"time"
)
var (
dialer = &net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 30 * time.Second,
}
)
// DialFunc is a function intended for setting to Client.Dial.
//
// It is expected that the returned conn immediately
// sends all the data passed via Write() to the server.
// Otherwise gorpc may hang.
// The conn implementation must call Flush() on underlying buffered
// streams before returning from Write().
type DialFunc func(addr string) (conn io.ReadWriteCloser, err error)
// Listener is an interface for custom listeners intended for the Server.
type Listener interface {
// Init is called on server start.
//
// addr contains the address set at Server.Addr.
Init(addr string) error
// Accept must return incoming connections from clients.
// clientAddr must contain client's address in user-readable view.
//
// It is expected that the returned conn immediately
// sends all the data passed via Write() to the client.
// Otherwise gorpc may hang.
// The conn implementation must call Flush() on underlying buffered
// streams before returning from Write().
Accept() (conn io.ReadWriteCloser, clientAddr string, err error)
// Close closes the listener.
// All pending calls to Accept() must immediately return errors after
// Close is called.
// All subsequent calls to Accept() must immediately return error.
Close() error
}
func defaultDial(addr string) (conn io.ReadWriteCloser, err error) {
return dialer.Dial("tcp", addr)
}
type defaultListener struct {
L net.Listener
}
func (ln *defaultListener) Init(addr string) (err error) {
ln.L, err = net.Listen("tcp", addr)
return
}
func (ln *defaultListener) Accept() (conn io.ReadWriteCloser, clientAddr string, err error) {
c, err := ln.L.Accept()
if err != nil {
return nil, "", err
}
if err = setupKeepalive(c); err != nil {
c.Close()
return nil, "", err
}
return c, c.RemoteAddr().String(), nil
}
func (ln *defaultListener) Close() error {
return ln.L.Close()
}
func setupKeepalive(conn net.Conn) error {
tcpConn := conn.(*net.TCPConn)
if err := tcpConn.SetKeepAlive(true); err != nil {
return err
}
if err := tcpConn.SetKeepAlivePeriod(30 * time.Second); err != nil {
return err
}
return nil
}
type netListener struct {
F func(addr string) (net.Listener, error)
L net.Listener
}
func (ln *netListener) Init(addr string) (err error) {
ln.L, err = ln.F(addr)
return
}
func (ln *netListener) Accept() (conn io.ReadWriteCloser, clientAddr string, err error) {
c, err := ln.L.Accept()
if err != nil {
return nil, "", err
}
return c, c.RemoteAddr().String(), nil
}
func (ln *netListener) Close() error {
return ln.L.Close()
}
func unixDial(addr string) (conn io.ReadWriteCloser, err error) {
c, err := net.Dial("unix", addr)
if err != nil {
return nil, err
}
return c, err
}
// NewTCPClient creates a client connecting over TCP to the server
// listening to the given addr.
//
// The returned client must be started after optional settings' adjustment.
//
// The corresponding server must be created with NewTCPServer().
func NewTCPClient(addr string) *Client {
return &Client{
Addr: addr,
Dial: defaultDial,
}
}
// NewTCPServer creates a server listening for TCP connections
// on the given addr and processing incoming requests
// with the given HandlerFunc.
//
// The returned server must be started after optional settings' adjustment.
//
// The corresponding client must be created with NewTCPClient().
func NewTCPServer(addr string, handler HandlerFunc) *Server {
return &Server{
Addr: addr,
Handler: handler,
Listener: &defaultListener{},
}
}
// NewUnixClient creates a client connecting over unix socket
// to the server listening to the given addr.
//
// The returned client must be started after optional settings' adjustment.
//
// The corresponding server must be created with NewUnixServer().
func NewUnixClient(addr string) *Client {
return &Client{
Addr: addr,
Dial: unixDial,
// There is little sense in compressing rpc data passed
// over local unix sockets.
DisableCompression: true,
// Sacrifice the number of Write() calls to the smallest
// possible latency, since it has higher priority in local IPC.
FlushDelay: -1,
}
}
// NewUnixServer creates a server listening for unix connections
// on the given addr and processing incoming requests
// with the given HandlerFunc.
//
// The returned server must be started after optional settings' adjustment.
//
// The corresponding client must be created with NewUnixClient().
func NewUnixServer(addr string, handler HandlerFunc) *Server {
return &Server{
Addr: addr,
Handler: handler,
Listener: &netListener{
F: func(addr string) (net.Listener, error) {
return net.Listen("unix", addr)
},
},
// Sacrifice the number of Write() calls to the smallest
// possible latency, since it has higher priority in local IPC.
FlushDelay: -1,
}
}
// NewTLSClient creates a client connecting over TLS (aka SSL) to the server
// listening to the given addr using the given TLS config.
//
// The returned client must be started after optional settings' adjustment.
//
// The corresponding server must be created with NewTLSServer().
func NewTLSClient(addr string, cfg *tls.Config) *Client {
return &Client{
Addr: addr,
Dial: func(addr string) (conn io.ReadWriteCloser, err error) {
c, err := tls.DialWithDialer(dialer, "tcp", addr, cfg)
if err != nil {
return nil, err
}
return c, err
},
}
}
// NewTLSServer creates a server listening for TLS (aka SSL) connections
// on the given addr and processing incoming requests
// with the given HandlerFunc.
// cfg must contain TLS settings for the server.
//
// The returned server must be started after optional settings' adjustment.
//
// The corresponding client must be created with NewTLSClient().
func NewTLSServer(addr string, handler HandlerFunc, cfg *tls.Config) *Server {
return &Server{
Addr: addr,
Handler: handler,
Listener: &netListener{
F: func(addr string) (net.Listener, error) {
return tls.Listen("tcp", addr, cfg)
},
},
}
}