forked from smallnest/rpcx
/
connection.go
155 lines (130 loc) ยท 3.41 KB
/
connection.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
package client
import (
"bufio"
"crypto/tls"
"errors"
"io"
"net"
"net/http"
"time"
"github.com/zouyapeng/rpcx/log"
"github.com/zouyapeng/rpcx/share"
)
type ConnFactoryFn func(c *Client, network, address string) (net.Conn, error)
var ConnFactories = map[string]ConnFactoryFn{
"http": newDirectHTTPConn,
"kcp": newDirectKCPConn,
"quic": newDirectQuicConn,
"unix": newDirectConn,
}
// Connect connects the server via specified network.
func (c *Client) Connect(network, address string) error {
var conn net.Conn
var err error
switch network {
case "http":
conn, err = newDirectHTTPConn(c, network, address)
case "kcp":
conn, err = newDirectKCPConn(c, network, address)
case "quic":
conn, err = newDirectQuicConn(c, network, address)
case "unix":
conn, err = newDirectConn(c, network, address)
default:
fn := ConnFactories[network]
if fn != nil {
conn, err = fn(c, network, address)
} else {
conn, err = newDirectConn(c, network, address)
}
}
if err == nil && conn != nil {
if c.option.IdleTimeout != 0 {
conn.SetDeadline(time.Now().Add(c.option.IdleTimeout))
}
if c.Plugins != nil {
conn, err = c.Plugins.DoConnCreated(conn)
if err != nil {
return err
}
}
c.Conn = conn
c.r = bufio.NewReaderSize(conn, ReaderBuffsize)
//c.w = bufio.NewWriterSize(conn, WriterBuffsize)
// start reading and writing since connected
go c.input()
if c.option.Heartbeat && c.option.HeartbeatInterval > 0 {
go c.heartbeat()
}
}
return err
}
func newDirectConn(c *Client, network, address string) (net.Conn, error) {
var conn net.Conn
var tlsConn *tls.Conn
var err error
if c != nil && c.option.TLSConfig != nil {
dialer := &net.Dialer{
Timeout: c.option.ConnectTimeout,
}
tlsConn, err = tls.DialWithDialer(dialer, network, address, c.option.TLSConfig)
//or conn:= tls.Client(netConn, &config)
conn = net.Conn(tlsConn)
} else {
conn, err = net.DialTimeout(network, address, c.option.ConnectTimeout)
}
if err != nil {
log.Warnf("failed to dial server: %v", err)
return nil, err
}
if tc, ok := conn.(*net.TCPConn); ok {
tc.SetKeepAlive(true)
tc.SetKeepAlivePeriod(3 * time.Minute)
}
return conn, nil
}
var connected = "200 Connected to rpcx"
func newDirectHTTPConn(c *Client, network, address string) (net.Conn, error) {
if c == nil {
return nil, errors.New("empty client")
}
path := c.option.RPCPath
if path == "" {
path = share.DefaultRPCPath
}
var conn net.Conn
var tlsConn *tls.Conn
var err error
if c.option.TLSConfig != nil {
dialer := &net.Dialer{
Timeout: c.option.ConnectTimeout,
}
tlsConn, err = tls.DialWithDialer(dialer, "tcp", address, c.option.TLSConfig)
//or conn:= tls.Client(netConn, &config)
conn = net.Conn(tlsConn)
} else {
conn, err = net.DialTimeout("tcp", address, c.option.ConnectTimeout)
}
if err != nil {
log.Errorf("failed to dial server: %v", err)
return nil, err
}
io.WriteString(conn, "CONNECT "+path+" HTTP/1.0\n\n")
// Require successful HTTP response
// before switching to RPC protocol.
resp, err := http.ReadResponse(bufio.NewReader(conn), &http.Request{Method: "CONNECT"})
if err == nil && resp.Status == connected {
return conn, nil
}
if err == nil {
log.Errorf("unexpected HTTP response: %v", err)
err = errors.New("unexpected HTTP response: " + resp.Status)
}
conn.Close()
return nil, &net.OpError{
Op: "dial-http",
Net: network + " " + address,
Addr: nil,
Err: err,
}
}