forked from hprose/hprose-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream_client.go
185 lines (163 loc) · 4.32 KB
/
stream_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
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* hprose/stream_client.go *
* *
* hprose stream client for Go. *
* *
* LastModified: Jan 28, 2015 *
* Authors: Ma Bingyao <andot@hprose.com> *
* Ore_Ash <nanohugh@gmail.com> *
* *
\**********************************************************/
package hprose
import (
"net"
"sync"
"time"
)
type StreamClient struct {
*BaseClient
timeout interface{}
readBuffer interface{}
readTimeout interface{}
writeBuffer interface{}
writeTimeout interface{}
}
func newStreamClient(trans Transporter) *StreamClient {
return &StreamClient{
BaseClient: NewBaseClient(trans),
}
}
type streamConnStatus int
const (
free = streamConnStatus(iota)
using
closing
)
type StreamConnEntry struct {
uri string
conn net.Conn
status streamConnStatus
lastUsedTime time.Time
}
func (connEntry *StreamConnEntry) Get() net.Conn {
return connEntry.conn
}
func (connEntry *StreamConnEntry) Set(conn net.Conn) {
if conn != nil {
connEntry.conn = conn
}
}
func (connEntry *StreamConnEntry) Close() {
connEntry.status = closing
}
type StreamConnPool struct {
sync.Mutex
pool []*StreamConnEntry
timer *time.Ticker
timeout time.Duration
}
func freeConns(conns []net.Conn) {
for _, conn := range conns {
conn.Close()
}
}
func (connPool *StreamConnPool) Timeout() time.Duration {
return connPool.timeout
}
func (connPool *StreamConnPool) SetTimeout(d time.Duration) {
if connPool.timer != nil {
connPool.timer.Stop()
connPool.timer = nil
}
connPool.timeout = d
if d > 0 {
connPool.timer = time.NewTicker(d)
go connPool.closeTimeoutConns()
}
}
func (connPool *StreamConnPool) closeTimeoutConns() {
for t := range connPool.timer.C {
connPool.Lock()
defer connPool.Unlock()
conns := make([]net.Conn, 0, len(connPool.pool))
for _, entry := range connPool.pool {
if entry.uri != "" &&
entry.status == free &&
entry.conn != nil &&
t.After(entry.lastUsedTime.Add(connPool.timeout)) {
conns = append(conns, entry.conn)
entry.conn = nil
entry.uri = ""
}
}
go freeConns(conns)
}
}
func (connPool *StreamConnPool) Get(uri string) *StreamConnEntry {
connPool.Lock()
defer connPool.Unlock()
for _, entry := range connPool.pool {
if entry.status == free {
if entry.uri == uri {
entry.status = using
return entry
} else if entry.uri == "" {
entry.status = using
entry.uri = uri
return entry
}
}
}
entry := &StreamConnEntry{uri, nil, using, time.Now()}
connPool.pool = append(connPool.pool, entry)
return entry
}
func (connPool *StreamConnPool) Close(uri string) {
connPool.Lock()
defer connPool.Unlock()
conns := make([]net.Conn, 0, len(connPool.pool))
for _, entry := range connPool.pool {
if entry.uri == uri {
if entry.status == free {
conns = append(conns, entry.conn)
entry.conn = nil
entry.uri = ""
} else {
entry.Close()
}
}
}
go freeConns(conns)
}
func (connPool *StreamConnPool) Free(entry *StreamConnEntry) {
if entry.status == closing {
if entry.conn != nil {
go entry.conn.Close()
entry.conn = nil
}
entry.uri = ""
}
entry.lastUsedTime = time.Now()
entry.status = free
}
func (client *StreamClient) SetReadBuffer(bytes int) {
client.readBuffer = bytes
}
func (client *StreamClient) SetReadTimeout(d time.Duration) {
client.readTimeout = d
}
func (client *StreamClient) SetWriteBuffer(bytes int) {
client.writeBuffer = bytes
}
func (client *StreamClient) SetWriteTimeout(d time.Duration) {
client.writeTimeout = d
}