@@ -8,28 +8,13 @@ import (
8
8
9
9
"github.com/gorilla/websocket"
10
10
uuid "github.com/satori/go.uuid"
11
+ "github.com/vesoft-inc/nebula-studio/server/api/studio/internal/config"
11
12
"github.com/zeromicro/go-zero/core/logx"
12
13
)
13
14
14
15
const (
15
- // Time allowed to write a message to the peer.
16
- writeWait = 10 * time .Second
17
-
18
- // Time allowed to read the next pong message from the peer.
19
- pongWait = 60 * time .Second
20
-
21
- // Send pings to peer with this period. Must be less than pongWait.
22
- pingPeriod = (pongWait * 9 ) / 10
23
-
24
- // Maximum message size allowed from peer.
25
- // Max ngql length can be executed is 4MB
26
- maxMessageSize = 8 * 1024 * 1024
27
-
28
- // send buffer size
29
- bufSize = 512
30
-
31
- heartbeatRequest = "1"
32
-
16
+ bufSize = 512 // send buffer size
17
+ heartbeatRequest = "1"
33
18
heartbeatResponse = "2"
34
19
)
35
20
@@ -157,10 +142,15 @@ func (c *Client) Destroy() {
157
142
// reads from this goroutine.
158
143
func (c * Client ) readPump () {
159
144
defer c .Destroy ()
160
-
161
- c .Conn .SetReadLimit (maxMessageSize )
162
- c .Conn .SetReadDeadline (time .Now ().Add (pongWait ))
163
- c .Conn .SetPongHandler (func (string ) error { c .Conn .SetReadDeadline (time .Now ().Add (pongWait )); return nil })
145
+ wsConfig := config .GetConfig ().WebSocket
146
+ if wsConfig .ReadLimit > 0 {
147
+ c .Conn .SetReadLimit (wsConfig .ReadLimit )
148
+ }
149
+ if wsConfig .ReadDeadline > 0 {
150
+ pongWait := time .Duration (wsConfig .ReadDeadline ) * time .Second
151
+ c .Conn .SetReadDeadline (time .Now ().Add (pongWait ))
152
+ c .Conn .SetPongHandler (func (string ) error { c .Conn .SetReadDeadline (time .Now ().Add (pongWait )); return nil })
153
+ }
164
154
for {
165
155
_ , message , err := c .Conn .ReadMessage ()
166
156
if err != nil {
@@ -191,7 +181,15 @@ func (c *Client) readPump() {
191
181
// application ensures that there is at most one writer to a connection by
192
182
// executing all writes from this goroutine.
193
183
func (c * Client ) writePump () {
194
- ticker := time .NewTicker (pingPeriod )
184
+ wsConfig := config .GetConfig ().WebSocket
185
+
186
+ writeWait := 0 * time .Second
187
+ ticker := time .NewTicker (30 * time .Second )
188
+
189
+ if wsConfig .WriteDeadline > 0 {
190
+ writeWait = time .Duration (wsConfig .WriteDeadline ) * time .Second
191
+ }
192
+
195
193
defer func () {
196
194
ticker .Stop ()
197
195
c .Destroy ()
@@ -200,7 +198,10 @@ func (c *Client) writePump() {
200
198
for {
201
199
select {
202
200
case message , ok := <- c .send :
203
- c .Conn .SetWriteDeadline (time .Now ().Add (writeWait ))
201
+ if writeWait > 0 {
202
+ // no timeout limit if writeWait is 0
203
+ c .Conn .SetWriteDeadline (time .Now ().Add (writeWait ))
204
+ }
204
205
if ! ok {
205
206
// The hub closed the channel.
206
207
logx .Errorf ("[WebSocket writePump]: c.send length: %v" , len (c .send ))
@@ -220,7 +221,10 @@ func (c *Client) writePump() {
220
221
return
221
222
}
222
223
case <- ticker .C :
223
- c .Conn .SetWriteDeadline (time .Now ().Add (writeWait ))
224
+ if writeWait > 0 {
225
+ // no timeout limit if writeWait is 0
226
+ c .Conn .SetWriteDeadline (time .Now ().Add (writeWait ))
227
+ }
224
228
if err := c .Conn .WriteMessage (websocket .PingMessage , nil ); err != nil {
225
229
logx .Errorf ("[WebSocket ticker error]: %v" , err )
226
230
return
0 commit comments