-
Notifications
You must be signed in to change notification settings - Fork 4
/
conn.go
199 lines (181 loc) · 4.37 KB
/
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
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
package k8s_exec_pod
import (
"bytes"
"context"
"fmt"
"github.com/Shanghai-Lunara/pkg/zaplogger"
"github.com/gorilla/websocket"
"net/http"
"sync"
"time"
)
var upGrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024 * 1024 * 10,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
type Proxy interface {
ReadPump()
WritePump()
Close()
Recv() (*message, error)
KeepAlive()
HandlePing()
Send(messageType int, data []byte) error
LoadBuffers(buf []byte) (n int, err error)
HandleInput(buf []byte, appendBuf []byte) (n int, err error)
}
func NewProxy(ctx context.Context, w http.ResponseWriter, r *http.Request) (Proxy, error) {
conn, err := upGrader.Upgrade(w, r, nil)
if err != nil {
zaplogger.Sugar().Error(err)
return nil, err
}
subCtx, cancel := context.WithCancel(ctx)
p := &proxy{
conn: conn,
status: proxyAlive,
readChan: make(chan *message, 4096),
writeChan: make(chan *message, 4096),
lastPingTime: time.Now(),
keepAliveTimeout: 10,
ctx: subCtx,
cancel: cancel,
}
go p.ReadPump()
go p.WritePump()
go p.KeepAlive()
return p, nil
}
type proxyStatus int
const (
proxyAlive proxyStatus = iota
proxyClose proxyStatus = 1
)
type proxy struct {
conn *websocket.Conn
status proxyStatus
readChan chan *message
writeChan chan *message
inputBuffers bytes.Buffer
lastPingTime time.Time
keepAliveTimeout int64
closeOnce sync.Once
ctx context.Context
cancel context.CancelFunc
}
type message struct {
messageType int
data []byte
}
func (p *proxy) KeepAlive() {
defer p.Close()
tick := time.NewTicker(time.Second * time.Duration(1+p.keepAliveTimeout))
defer tick.Stop()
for {
select {
case <-tick.C:
if time.Now().Sub(p.lastPingTime) > time.Second*time.Duration(p.keepAliveTimeout) {
zaplogger.Sugar().Info("Proxy KeepAlive timeout")
return
}
}
}
}
func (p *proxy) HandlePing() {
p.lastPingTime = time.Now()
}
func (p *proxy) ReadPump() {
defer p.Close()
for {
messageType, data, err := p.conn.ReadMessage()
//zaplogger.Sugar().Info("data:", data)
//zaplogger.Sugar().Infof("messageType: %d message: %v err: %s\n", messageType, data, err)
if err != nil {
zaplogger.Sugar().Error(err)
return
}
msg := &message{messageType: messageType, data: data}
select {
case p.readChan <- msg:
case <-p.ctx.Done():
zaplogger.Sugar().Info("ReadPump: proxy ctx cancel")
return
case <-time.After(time.Second * 5):
zaplogger.Sugar().Info("ReadPump: write into readChan timeout 5s")
return
}
}
}
func (p *proxy) WritePump() {
defer p.Close()
for {
select {
case msg, isClose := <-p.writeChan:
if !isClose {
return
}
//zaplogger.Sugar().Info("proxy WritePump msg-data:", string(msg.data))
if err := p.conn.WriteMessage(msg.messageType, msg.data); err != nil {
zaplogger.Sugar().Error(err)
return
}
case <-p.ctx.Done():
return
}
}
}
func (p *proxy) Close() {
zaplogger.Sugar().Info("proxy close")
p.closeOnce.Do(func() {
if p.status == proxyClose {
return
}
p.cancel()
p.status = proxyClose
//close(p.readChan)
//close(p.writeChan)
if err := p.conn.Close(); err != nil {
zaplogger.Sugar().Error(err)
}
})
}
func (p *proxy) Recv() (*message, error) {
//zaplogger.Sugar().Info("proxy Recv message")
select {
case msg, isClose := <-p.readChan:
if !isClose {
return nil, fmt.Errorf("readChan closed")
}
//zaplogger.Sugar().Info("proxy recv-msg:", msg)
//zaplogger.Sugar().Info("proxy recv-data-string:", string(msg.data))
return msg, nil
case <-p.ctx.Done():
return nil, fmt.Errorf("proxy ctx cancel")
}
}
func (p *proxy) Send(messageType int, data []byte) error {
//zaplogger.Sugar().Infof("proxy send messageType:%v data:%v", messageType, string(data))
if p.status == proxyClose {
return fmt.Errorf("err: proxy has been closed")
}
select {
case p.writeChan <- &message{messageType: messageType, data: data}:
return nil
case <-p.ctx.Done():
return fmt.Errorf("proxy ctx cancel")
}
}
func (p *proxy) LoadBuffers(buf []byte) (n int, err error) {
if p.inputBuffers.Len() > 0 {
n = copy(buf, p.inputBuffers.Bytes())
p.inputBuffers.Next(n)
}
return n, nil
}
func (p *proxy) HandleInput(buf []byte, appendBuf []byte) (n int, err error) {
p.inputBuffers.Write(appendBuf)
return p.LoadBuffers(buf)
}