forked from apache/dubbo-getty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
194 lines (171 loc) · 3.44 KB
/
client.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
/******************************************************
# DESC : getty client
# MAINTAINER : Alex Stocks
# LICENCE : Apache License 2.0
# EMAIL : alexstocks@foxmail.com
# MOD : 2016-09-01 21:32
# FILE : client.go
******************************************************/
package getty
import (
"net"
"sync"
"time"
)
import (
log "github.com/AlexStocks/log4go"
)
const (
defaultInterval = 3e9 // 3s
connectTimeout = 5e9
maxTimes = 10
)
type Client struct {
// net
sync.Mutex
number int
interval time.Duration
addr string
newSession NewSessionCallback
sessionMap map[*Session]empty
sync.Once
done chan empty
wg sync.WaitGroup
}
// NewClient function builds a client.
// @connNum is connection number.
// @connInterval is reconnect sleep interval when getty fails to connect the server.
// @serverAddr is server address.
func NewClient(connNum int, connInterval time.Duration, serverAddr string) *Client {
if connNum < 0 {
connNum = 1
}
if connInterval < defaultInterval {
connInterval = defaultInterval
}
return &Client{
number: connNum,
interval: connInterval,
addr: serverAddr,
sessionMap: make(map[*Session]empty, connNum),
done: make(chan empty),
}
}
func (this *Client) dial() net.Conn {
var (
err error
conn net.Conn
)
for {
if this.IsClosed() {
return nil
}
conn, err = net.DialTimeout("tcp", this.addr, connectTimeout)
if err == nil && conn.LocalAddr().String() == conn.RemoteAddr().String() {
err = errSelfConnect
}
if err == nil {
return conn
}
log.Info("net.Connect(addr:%s, timeout:%v) = error{%v}", this.addr, err)
time.Sleep(this.interval)
continue
}
}
func (this *Client) sessionNum() int {
var num int
this.Lock()
for s := range this.sessionMap {
if s.IsClosed() {
delete(this.sessionMap, s)
}
}
num = len(this.sessionMap)
this.Unlock()
return num
}
func (this *Client) connect() {
var (
err error
conn net.Conn
session *Session
)
for {
conn = this.dial()
if conn == nil {
// client has been closed
break
}
session = NewSession(conn)
err = this.newSession(session)
if err == nil {
session.RunEventLoop()
this.Lock()
this.sessionMap[session] = empty{}
this.Unlock()
break
}
conn.Close()
}
}
func (this *Client) RunEventLoop(newSession NewSessionCallback) {
this.Lock()
this.newSession = newSession
this.Unlock()
this.wg.Add(1)
go func() {
var num, max, times int
defer this.wg.Done()
this.Lock()
max = this.number
this.Unlock()
// log.Info("maximum client connection number:%d", max)
for {
if this.IsClosed() {
log.Warn("client{peer:%s} goroutine exit now.", this.addr)
break
}
num = this.sessionNum()
// log.Info("current client connction number:%d", num)
if max <= num {
times++
if maxTimes < times {
times = maxTimes
}
time.Sleep(time.Duration(times * defaultInterval))
continue
}
times = 0
this.connect()
// time.Sleep(this.interval) // build this.number connections asap
}
}()
}
func (this *Client) stop() {
select {
case <-this.done:
return
default:
this.Once.Do(func() {
close(this.done)
this.Lock()
for s := range this.sessionMap {
s.Close()
}
this.sessionMap = nil
this.Unlock()
})
}
}
func (this *Client) IsClosed() bool {
select {
case <-this.done:
return true
default:
return false
}
}
func (this *Client) Close() {
this.stop()
this.wg.Wait()
}