-
Notifications
You must be signed in to change notification settings - Fork 4
/
client_conn.go
118 lines (98 loc) · 2.57 KB
/
client_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
package ws
import (
"context"
"io/ioutil"
"net/http"
"time"
"github.com/gorilla/websocket"
"github.com/liumingmin/goutils/log"
)
var defaultDialer = &websocket.Dialer{
Proxy: http.ProxyFromEnvironment,
HandshakeTimeout: 10 * time.Second,
ReadBufferSize: 4096,
WriteBufferSize: 4096,
}
func (c *Connection) KickServer() {
if c.typ != CONN_KIND_CLIENT {
return
}
ServerConnHub.unregisterConn(c)
}
// ctx only use for dial phase and stop auto redial
func AutoReDialConnect(ctx context.Context, sUrl string, header http.Header, connInterval time.Duration, opts ...ConnOption) {
if connInterval == 0 {
connInterval = time.Second * 5
}
for {
conn, err := DialConnect(ctx, sUrl, header, opts...)
if err != nil || conn == nil {
select {
case <-ctx.Done():
return
default:
}
time.Sleep(connInterval)
continue
}
select {
case <-ctx.Done():
return
case <-conn.(*Connection).readDone:
<-conn.(*Connection).writeDone
<-conn.(*Connection).writeStop
}
time.Sleep(connInterval)
}
}
// ctx only use for dial phase
func DialConnect(ctx context.Context, sUrl string, header http.Header, opts ...ConnOption) (IConnection, error) {
connection := &Connection{}
connection.init()
connection.typ = CONN_KIND_CLIENT
connection.dialer = defaultDialer
connection.snCounter = 0
if len(opts) > 0 {
for _, opt := range opts {
opt(connection)
}
}
var conn *websocket.Conn
for retry := 1; retry <= connection.dialRetryNum; retry++ {
var err error
var resp *http.Response
conn, resp, err = connection.dialer.DialContext(ctx, sUrl, header)
if err != nil {
if retry < connection.dialRetryNum {
log.Warn(ctx, "Failed to connect to server, sleep and try again. retry: %v, error: %v, url: %v", retry, err, sUrl)
time.Sleep(connection.dialRetryInterval)
continue
} else {
log.Error(ctx, "Failed to connect to server, leave it. retry: %v, error: %v", retry, err)
if connection.dialConnFailedHandler != nil {
connection.dialConnFailedHandler(ctx, connection)
}
return nil, err
}
}
func() {
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
ioutil.ReadAll(resp.Body)
}
}()
log.Debug(ctx, "Success connect to server. retry: %v", retry)
break
}
connection.conn = conn
connection.conn.SetCompressionLevel(connection.compressionLevel)
if connection.sendBuffer == nil {
SendBufferOption(8)(connection)
}
ServerConnHub.registerConn(connection)
conn.SetCloseHandler(func(code int, text string) error {
connection.KickServer()
return nil
})
return connection, nil
}